Computer Programming Contest Preparation

ToolBox - Source for: 3/325/b.c



/home/toolbox/public_html/solutions/3/325/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 <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 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2021-12-8
   20  * Purpose: fun
   21  * Problem: 325 - Identifying Legal Pascal Real Constants
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 
   29 
   30 /* 1   2      34       5 6  7      8
   31  * [+-]digits[.digits][e[+-]digits]whitespace
   32  *
   33  *            | start | num1  | num2  | num3  | num4  | num5  | num6  | num7  | num8
   34  * whitespace | start | error | num8  | error | num8  | error | error | num8  | num8
   35  * sign       | num1  | error | error | error | error | num6  | error | error | error
   36  * digit      | num2  | num2  | num2  | num4  | num4  | num7  | num7  | num7  | error
   37  * period     | error | error | num3  | error | error | error | error | error | error
   38  * exp        | error | error | num5  | error | num5  | error | error | error | error
   39  * StringEnd  | error | error | error | error | valid | error | error | valid | valid
   40  * other      | error | error | error | error | error | error | error | error | error
   41  */
   42 
   43 
   44 #define MAX_LINE 2049
   45 #define START  0
   46 #define NUM1   1
   47 #define NUM2   2
   48 #define NUM3   3
   49 #define NUM4   4
   50 #define NUM5   5
   51 #define NUM6   6
   52 #define NUM7   7
   53 #define NUM8   8
   54 #define ERROR -1
   55 #define VALID  100
   56 #define WHITESPACE 0
   57 #define SIGN       1
   58 #define DIGIT      2
   59 #define PERIOD     3
   60 #define EXP        4
   61 #define STRINGEND  5
   62 #define OTHER      6
   63 char line[MAX_LINE];
   64 int slen;
   65 int ptr;
   66 int state;
   67 int table[7][9] =
   68 {
   69     { START, ERROR, NUM8,  ERROR, NUM8,  ERROR, ERROR, NUM8,  NUM8  },
   70     { NUM1, ERROR, ERROR, ERROR, ERROR, NUM6, ERROR, ERROR, ERROR },
   71     { NUM2, NUM2, NUM2, NUM4, NUM4, NUM7, NUM7, NUM7,  ERROR },
   72     { ERROR, ERROR, NUM3, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR },
   73     { ERROR, ERROR, NUM5, ERROR, NUM5, ERROR, ERROR, ERROR, ERROR },
   74     { ERROR, ERROR, ERROR, ERROR, VALID, ERROR, ERROR, VALID, VALID },
   75     { ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR }
   76 };
   77 
   78 void init()
   79 {
   80     /* FUNCTION init */
   81 } /* FUNCTION init */
   82 
   83 void dump()
   84 {
   85     /* FUNCTION dump */
   86 } /* FUNCTION dump */
   87 
   88 int getInput()
   89 {
   90     /* FUNCTION getInput */
   91     int dataReadFlag;
   92     int strt;
   93     int i;
   94     int j;
   95 
   96     fgets(line, MAX_LINE, stdin);
   97     line[strlen(line)-1] = 0;
   98     dataReadFlag = (0 != strcmp("*", line));
   99     if (dataReadFlag)
  100         {
  101             /* clean up line */
  102             printf("line raw: [%s]\n", line);
  103             slen = strlen(line);
  104             strt=0;
  105             for (i=0; (slen>i) && (isspace(line[i])); i++)
  106                 {
  107                     strt++;
  108                 }
  109             for (i=strt,j=0; slen>=i; i++,j++)
  110                 {
  111                     /* shift line to left  */
  112                     line[j] = line[i];
  113                 } /* shift line to left  */
  114             slen = strlen(line);
  115             printf("line clean: [%s]\n", line);
  116         } /* clean up line */
  117     return (dataReadFlag);
  118 } /* FUNCTION getInput */
  119 
  120 int what(char x)
  121 {
  122     /* FUNCTION what */
  123     int toReturn;
  124 
  125     switch (x)
  126         {
  127         /* switch */
  128         case 0:
  129             toReturn = STRINGEND;
  130             break;
  131         case '0':
  132         case '1':
  133         case '2':
  134         case '3':
  135         case '4':
  136         case '5':
  137         case '6':
  138         case '7':
  139         case '8':
  140         case '9':
  141             toReturn = DIGIT;
  142             break;
  143         case '+':
  144         case '-':
  145             toReturn = SIGN;
  146             break;
  147         case 'E':
  148         case 'e':
  149             toReturn = EXP;
  150             break;
  151         case '.':
  152             toReturn = PERIOD;
  153             break;
  154         case ' ':
  155             toReturn = WHITESPACE;
  156             break;
  157         default:
  158             toReturn = OTHER;
  159         } /* switch */
  160     return toReturn;
  161 } /* FUNCTION what */
  162 
  163 void process()
  164 {
  165     /* FUNCTION process */
  166     int tmp;
  167     char result[3] ="";
  168 
  169     DEBUG printf("[%s]\n", line);
  170     state = START;
  171     ptr = 0;
  172     while ((ERROR != state) && (VALID != state))
  173         {
  174             /* process line */
  175             tmp = what(line[ptr]);
  176             DEBUG printf("(%c %d) (state %d) => %d\n", line[ptr], tmp, state, table[tmp][state]);
  177             state = table[tmp][state];
  178             ptr++;
  179         } /* process line */
  180     if (ERROR == state)
  181         {
  182             result[0]='i';
  183             result[1]='l';
  184             result[2]=0;
  185         }
  186     printf("%s is %slegal.\n", line, result);
  187 } /* FUNCTION process */
  188 
  189 int main()
  190 {
  191     /* main */
  192     int moreToDo;
  193 
  194     init();
  195     moreToDo = getInput();
  196     while (moreToDo)
  197         {
  198             /* while */
  199             process();
  200             moreToDo = getInput();
  201         } /* while */
  202 
  203     return EXIT_SUCCESS;
  204 } /* main */
  205