+/EE312
software design
git clone https://git.julia.blue/+/EE312
0c27270
master
1#include <stdio.h> // declarations of FILE and printf 2#include <stdlib.h> // includes declaration of exit() 3#include "MyString.h" // String ADT case study from class 4#include "Invent.h" 5 6FILE * input_stream ; 7String inventory ; 8String purchase ; 9String summarize ; 10String quit ; 11 12void displayTailAndClose () { 13char * rval ; 14char buff [BUFSIZ ]; 15 16rval = fgets (buff ,BUFSIZ ,input_stream ); 17while (rval != 0 ) { 18printf ("%s" ,buff ); 19rval = fgets (buff ,BUFSIZ ,input_stream ); 20 } 21fclose (input_stream ); 22} 23 24void readInput (const char * file_name ) { 25String next_command ; 26bool all_done = false; 27 28/* open the file */ 29input_stream = fopen (file_name ,"r" ); 30if (input_stream == 0 ) { 31printf ("sorry, I could not open %s for reading\n" ,file_name ); 32exit (-1 ); 33 } 34 35/* main loop, read commands from input file */ 36do { 37readString (& next_command ); 38if (StringIsEqualTo (& next_command ,& inventory )) { 39processInventory (); 40 }else if (StringIsEqualTo (& next_command ,& purchase )) { 41processPurchase (); 42 }else if (StringIsEqualTo (& next_command ,& summarize )) { 43processSummarize (); 44 }else if (StringIsEqualTo (& next_command ,& quit )) { 45all_done = true; 46 }else { 47printf ("Encountered an error in input file\n" ); 48printf ("The offending line has been ignored\n" ); 49 } 50StringDestroy (& next_command ); 51 }while (!all_done ); 52 53displayTailAndClose (); 54} 55 56 57int main (void ) { 58/* we make four "constant" strings for the four comamnds */ 59inventory = StringCreate ("Inventory" ); 60purchase = StringCreate ("Purchase" ); 61summarize = StringCreate ("Summarize" ); 62quit = StringCreate ("Quit" ); 63 64readInput ("test1.txt" ); 65reset (); 66readInput ("test2.txt" ); 67reset (); 68readInput ("test3.txt" ); 69reset (); 70readInput ("test4.txt" ); 71reset (); 72 73/* all the test are done, time to clean up, let's deallocate our "constant strings" */ 74StringDestroy (& inventory ); 75StringDestroy (& purchase ); 76StringDestroy (& summarize ); 77StringDestroy (& quit ); 78} 79 80void readNum (int * p ) { 81fscanf (input_stream ,"%d" ,p ); 82} 83 84void readString (String * p ) { 85char buff [BUFSIZ ]; 86fscanf (input_stream ,"%s" ,buff ); 87* p = StringCreate (buff ); 88}