-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (98 loc) · 3.24 KB
/
Copy pathmain.cpp
File metadata and controls
112 lines (98 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <string>
#include <iostream>
#include "operation_handlers/define_handler.hh"
#include "operation_handlers/solve_handler.hh"
#include "operation_handlers/truth_table_handler.hh"
#include "operation_handlers/find_handler.hh"
#include "data_types/queue.hh"
#include "data_types/function.hh"
#include "utils/file_utils.hh"
#include "utils/string_utils.hh"
#include "enumerations/command.hh"
using namespace std;
Queue<Function> get_functions_from_file(string filename) {
Queue<Function> functions;
// get commands from file
Vector<string> lines = read_lines_from_file(filename);
// generate functions from lines
string line;
for (int i = 0; i < lines.size(); i++) {
// get line
line = lines[i];
// check for commas count in line
if (count_chars(',', line) != 2) {
cerr << "Invalid entry in \"" << filename << "\": \"" << line << "\"" << endl;
continue;
}
// split line at commas
Vector<string> function_definition = split_string_to_strings(',', line);
// extract function data
string function_name = function_definition[0];
Vector<char> function_args;
for (int j = 0; j < get_length(function_definition[1]); j++) {
function_args.push_back(function_definition[1][j]);
}
string function_expression = function_definition[2];
// add function to queue of functions
functions.enqueue(Function(function_name, function_expression, function_args));
cout << "Added function: name - " << function_name << ", args - "
<< function_args.to_string() << ", expression - " << function_expression << endl;
}
return functions;
}
int main(int argc, char *argv[]) {
Queue<Function> functions;
// check for file name in arguments
if (argc > 1) {
try {
functions = get_functions_from_file(argv[1]);
} catch (const exception &e) {
cerr << e.what() << endl;
}
}
string line;
while (true) {
// get line from console
getline(cin, line);
// trim spaces from line
trim_spaces(line);
// get command
Command command;
try {
command = get_command(line);
} catch (const exception &e) {
cerr << e.what() << endl;
continue;
}
try {
switch (command) {
// DEFINE
case DEFINE: {
Function function = process_define(line, functions);
functions.enqueue(function);
break;
}
// SOLVE
case SOLVE: {
process_solve(line, functions);
break;
}
// FIND
case FIND: {
process_find(line);
break;
}
// ALL
case ALL: {
process_all(line, functions);
break;
}
// QUIT
case QUIT:
return 0;
}
} catch (const exception &e) {
cerr << "Exception caught: " << e.what() << endl;
}
}
}