Computer Programming Contest Preparation

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



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