Computer Programming Contest Preparation

ToolBox - Source for: 108/10854/a.c



/home/toolbox/public_html/solutions/108/10854/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (TRUE)
   14 #define MAX_LINE 133
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2012-08-23
   21  * Purpose: fun
   22  * Problem: 10854 - Number of Paths
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 int numberOfTimes;
   30 char line[MAX_LINE];
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35     scanf("%d ", &numberOfTimes);
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 int getInput()
   44 {
   45     /* FUNCTION getInput */
   46     fgets(line, MAX_LINE, stdin);
   47     while ('S' == line[0])
   48         {
   49             /* while */
   50             fgets(line, MAX_LINE, stdin);
   51         } /* while */
   52     DEBUG printf("GetInput: %s", line);
   53     return ('E' != line[0]) || ('P' != line[3]);
   54 } /* FUNCTION getInput */
   55 
   56 int foundIf()
   57 {
   58     /* FUNCTION foundIf */
   59     return (('I' == line[0]) && ('F' == line[1]));
   60 } /* FUNCTION foundIf */
   61 
   62 int foundElse()
   63 {
   64     /* FUNCTION foundElse */
   65     return (('E' == line[0]) && ('L' == line[1]));
   66 } /* FUNCTION foundElse */
   67 
   68 int foundEndIf()
   69 {
   70     /* FUNCTION foundEndIf */
   71     /* when looking for an END_IF -- a ENDPROGRAM is not possible */
   72     return (('E' == line[0]) && ('N' == line[1]));
   73 } /* FUNCTION foundEndIf */
   74 
   75 int processElse()
   76 {
   77     /* FUNCTION processElse */
   78     int threads;
   79 
   80     DEBUG printf("enter process ELSE\n");
   81     getInput();
   82     if (foundIf())
   83         {
   84             /* found an if */
   85             threads = processIf();
   86         } /* found an if */
   87     else
   88         {
   89             /* not an if -- so must be an endif */
   90             threads = 1;
   91             getInput();
   92         } /* not an if -- so must be an endif */
   93     DEBUG printf("else yielded: %d\n", threads);
   94     DEBUG printf("exit process ELSE\n");
   95     return threads;
   96 } /* FUNCTION processElse */
   97 
   98 int processIf()
   99 {
  100     /* FUNCTION processIf */
  101     int threads;
  102 
  103     DEBUG printf("enter process IF\n");
  104     getInput();
  105     if (foundIf())
  106         {
  107             /* found a nested if */
  108             DEBUG printf("in processIf - line:%s", line);
  109             threads = processIf();
  110         } /* found a nested if */
  111     else
  112         {
  113             /* must be an else (only legal value) -- must be an if or an endif next */
  114             /* no if found -- so add 1 for then part of thread */
  115             threads = 1;
  116         } /* must be an else (only legal value) -- must be an if or an endif next */
  117     DEBUG printf("then yielded: %d\n", threads);
  118     threads = threads + processElse();
  119     DEBUG printf("if yielded: %d\n", threads);
  120     DEBUG printf("exit process IF\n");
  121     return threads;
  122 } /* FUNCTION processIf */
  123 
  124 void process()
  125 {
  126     /* FUNCTION process */
  127     int moreToDo;
  128     int threads = 1;
  129 
  130     moreToDo = getInput();
  131     while (moreToDo)
  132         {
  133             /* while */
  134             /* only way to be hear is with an if statement */
  135             threads = threads * processIf();
  136             DEBUG printf("threads: %d\n", threads);
  137             moreToDo = getInput();
  138         } /* while */
  139     DEBUG printf("Answer: %d\n", threads);
  140     printf("%d\n", threads);
  141 } /* FUNCTION process */
  142 
  143 int main ()
  144 {
  145     /* main */
  146     int i;
  147 
  148     init();
  149     for (i=0; i<numberOfTimes; i++)
  150         {
  151             /* while */
  152             DEBUG printf("\nCase %d\n", i);
  153             process();
  154         } /* while */
  155 
  156     return EXIT_SUCCESS;
  157 } /* main */
  158 
  159