Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/108/10854/b.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 (FALSE)
   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     printf("%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 processIf */
   78     int threads;
   79 
   80     printf("- 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         } /* not an if -- so must be an endif */
   92     printf("else yielded: %d\n", threads);
   93     return threads;
   94 } /* FUNCTION processIf */
   95 
   96 int processIf()
   97 {
   98     /* FUNCTION processIf */
   99     int threads;
  100 
  101     printf("- process IF\n");
  102     getInput();
  103     if (foundIf())
  104         {
  105             /* found a nested if */
  106             threads = processIf();
  107             threads = threads + processElse();
  108         } /* found a nested if */
  109     else
  110         {
  111             /* must be an else (only legal value) -- must be an if or an endif next */
  112             /* no if found -- so add 1 for then part of thread */
  113             threads = 1 + processElse();
  114         } /* must be an else (only legal value) -- must be an if or an endif next */
  115     printf("if yielded: %d\n", threads);
  116     return threads;
  117 } /* FUNCTION processIf */
  118 
  119 void process()
  120 {
  121     /* FUNCTION process */
  122     int moreToDo;
  123     int threads = 1;
  124 
  125     moreToDo = getInput();
  126     while (moreToDo)
  127         {
  128             /* while */
  129             /* only way to be hear is with an if statement */
  130             threads = threads * processIf();
  131             printf("threads: %d\n", threads);
  132             moreToDo = getInput();
  133         } /* while */
  134     printf("Answer: %d\n", threads);
  135 } /* FUNCTION process */
  136 
  137 int main ()
  138 {
  139     /* main */
  140     int i;
  141 
  142     init();
  143     for (i=0; i<numberOfTimes; i++)
  144         {
  145             /* while */
  146             printf("\nCase %d\n", i);
  147             process();
  148         } /* while */
  149 
  150     return EXIT_SUCCESS;
  151 } /* main */
  152 
  153