+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanProject 76241299

master
2.4 KiB95 linesraw
1#include <stdio.h>
2#include <string>
3#include <stdio.h> // declarations of FILE and printf
4#include <stdlib.h> // includes declaration of exit()
5#include "String.h"
6// #include "MemHeap.h"
7
8#include "CustomerDB.h"
9/*
10struct CheckMemoryLeaks {
11	~CheckMemoryLeaks(void) {
12		if (! isSaneHeap()) {
13			printf("oh goodness! you've corrupted the heap, naughty naughty\n");
14			return;
15		}
16		if (! isEmptyHeap()) {
17			printf("Uh Oh! you have a memory leak somewhere, better find it\n");
18			return;
19		}
20		printf("The heap is all clean, good work!\n");
21	}
22} MemoryChecker;
23*/
24FILE* input_stream;
25String inventory_cmd_name = String("Inventory");
26String purchase_cmd_name = String("Purchase");
27String summarize_cmd_name = String("Summarize");
28String quit_cmd_name = String("Quit");
29
30void readNum(int &x) {
31    int val;
32    fscanf(input_stream, "%d", &val);
33    x = val;
34}
35
36void readString(String &x) {
37    char buff[BUFSIZ];
38    fscanf(input_stream, "%s", buff);
39    x = String(buff);
40}
41
42void displayTailAndClose() {
43    char* rval;
44    char buff[BUFSIZ];
45
46    rval = fgets(buff, BUFSIZ, input_stream);
47    while (rval != 0) {
48        printf(buff);
49        rval = fgets(buff, BUFSIZ, input_stream);
50    }
51    fclose(input_stream);
52}
53
54void readInput(const char* file_name) {
55    String next_command;
56    bool all_done = false;
57
58    /* open the file */
59    input_stream = fopen(file_name, "r");
60    if (input_stream == 0) {
61        printf("sorry, I could not open %s for reading\n", file_name);
62        exit(-1);
63    }
64
65    /* main loop, read commands from input file */
66    do {
67        readString(next_command);
68        if (next_command == inventory_cmd_name) {
69            processInventory();
70        } else if (next_command == purchase_cmd_name) {
71            processPurchase();
72        } else if (next_command == summarize_cmd_name) {
73            processSummarize();
74        } else if (next_command == quit_cmd_name) {
75            all_done = true;
76        } else {
77            printf("Encountered an error in input file\n");
78            printf("The offending line has been ignored\n");
79        }
80    } while (! all_done);
81
82    displayTailAndClose();
83}
84
85int main(void) {
86    readInput("TheMotherOfAllTestCases.txt");
87    reset();
88    readInput("test1.txt");
89    reset();
90    readInput("test2.txt");
91    reset();
92    readInput("test3.txt");
93    reset();
94    return 0;
95}