Computer Programming Contest Preparation

ToolBox - Source for: 118/11878/a.c



/home/toolbox/public_html/solutions/118/11878/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 #define MAX_LINE 257
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2021-12-6
   21  * Purpose: fun
   22  * Problem: 11878 - Homework Checker
   23  */
   24 
   25 /*
   26  * This template reads lines of data at a time until end of file.
   27  */
   28 
   29 #define QUESTION_MARK '?'
   30 #define ADD '+'
   31 #define SUBTRACT '-'
   32 #define MULTIPLY '*'
   33 #define DIVIDE '/'
   34 
   35 char line[MAX_LINE];
   36 int position;
   37 int right;
   38 
   39 void init()
   40 {
   41     /* FUNCTION init */
   42     right = 0;
   43 } /* FUNCTION init */
   44 
   45 void dump()
   46 {
   47     /* FUNCTION dump */
   48 } /* FUNCTION dump */
   49 
   50 int getInput()
   51 {
   52     /* FUNCTION getInput */
   53     int dataReadFlag;
   54 
   55     fgets(line, MAX_LINE, stdin);
   56     if (feof(stdin))
   57         dataReadFlag = FALSE;
   58     else
   59         {
   60             /* something to read */
   61             dataReadFlag = TRUE;
   62             line[strlen(line)-1] = 0;
   63         } /* something to read */
   64     return (dataReadFlag);
   65 } /* FUNCTION getInput */
   66 
   67 void skipWhitespace()
   68 {
   69     /* FUNCTION skipWhitespace */
   70     while (isspace(line[position]))
   71         {
   72             /* while */
   73             position++;
   74         } /* while */
   75 } /* FUNCTION skipWhitespace */
   76 
   77 char getOper()
   78 {
   79     /* FUNCTION getOper */
   80     char toReturn;
   81 
   82     skipWhitespace();
   83     toReturn = line[position];
   84     position++;
   85     return toReturn;
   86 } /* FUNCTION getOper */
   87 
   88 int getNumb()
   89 {
   90     /* FUNCTION getNumb */
   91     int toReturn = 0;
   92 
   93     skipWhitespace();
   94     while (isdigit(line[position]))
   95         {
   96             /* while */
   97             toReturn = (toReturn * 10) + (line[position] - '0');
   98             position++;
   99         } /* while */
  100     return toReturn;
  101 } /* FUNCTION getNumb */
  102 
  103 void process()
  104 {
  105     /* FUNCTION process */
  106     /* a oper b = c  */
  107     int a;
  108     char oper;
  109     char equal;
  110     int b;
  111     int c;
  112     int tmp;
  113 
  114     if (NULL == strchr(line, QUESTION_MARK))
  115         {
  116             /* have an answer */
  117             position = 0;
  118             /* get a */
  119             a = getNumb();
  120             /* get operator */
  121             oper = getOper();
  122             /* get b */
  123             b = getNumb();
  124             /* skip equal sign */
  125             equal = getOper();
  126             /* get c */
  127             c = getNumb();
  128             /* does c == a OPER b? */
  129             switch (oper)
  130                 {
  131                     /* switch */
  132                 case ADD:
  133                     tmp = a + b;
  134                     break;
  135                 case SUBTRACT:
  136                     tmp = a - b;
  137                     break;
  138                 case MULTIPLY:
  139                     tmp = a * b;
  140                     break;
  141                 case DIVIDE:
  142                     tmp = a / b;
  143                     break;
  144                 } /* switch */
  145             if (c == tmp)
  146                 {
  147                     right++;
  148                 }
  149         } /* have an answer */
  150 } /* FUNCTION process */
  151 
  152 int main()
  153 {
  154     /* main */
  155     int moreToDo;
  156 
  157     init();
  158     moreToDo = getInput();
  159     while (moreToDo)
  160         {
  161             /* while */
  162             process();
  163             moreToDo = getInput();
  164         } /* while */
  165     printf("%d\n", right);
  166     return EXIT_SUCCESS;
  167 } /* main */
  168