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.159 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.158 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.157 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.156 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.155 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.154 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.153 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.152 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.151 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.150 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.149 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.148 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.147 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.146 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.145 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.144 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.143 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.142 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.141 tracks/c/exercises/wordy/src/example.c
trackler-2.2.1.140 tracks/c/exercises/wordy/src/example.c