Sha256: ea607f49845a6c240c486fa08ae036189b75be7e809837cad15beb850decdb73

Contents?: true

Size: 1.05 KB

Versions: 66

Compression:

Stored size: 1.05 KB

Contents

#include "wordy.h"
#include <string.h>
#include <ctype.h>
#include <stdio.h>

typedef struct {
   const char *str;
   const size_t len;
} map_t;

static const map_t q_init = { "What is", 7 };

#define COMMANDS \
        CMD(+, "plus", 4) \
        CMD(-, "minus", 5) \
        CMD(*, "multiplied by", 13) \
        CMD(/, "divided by", 10)

bool answer(const char *question, int *result)
{
   if (question == NULL)
      return false;

   if (strncmp(q_init.str, question, q_init.len))
      return false;
   question += q_init.len;

   int chars_read;
   if (sscanf(question, " %d %n", result, &chars_read) != 1)
      return false;

   question += chars_read;

   while (*question != '?') {
      int tmp;

#define CMD(a, b, c) \
      if (strncmp(b, question, c) == 0) { \
         question += c; \
         if (sscanf(question, " %d %n", &tmp, &chars_read) != 1) \
            return false; \
         question += chars_read; \
         *result a##= tmp; \
      } else
      COMMANDS
#undef CMD
      {
         return false;
      }
   }

   return *++question == '\0';
}

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.179 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.178 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.177 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.176 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.175 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.174 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.173 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.172 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.171 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.170 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.169 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.167 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.166 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.165 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.164 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.163 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.162 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.161 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.160 tracks/c/exercises/wordy/src/example.c