-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
140 lines (123 loc) · 2.77 KB
/
main.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
// Cleaning the string
#include "clean.h"
// Calculating
#include "math.h"
// Range of calculating
#include "braces.h"
// For Debugging
#define DEBUG 0
#if DEBUG == 1
#define LOG(x, y) std::cout << y << " -> " << x << std::endl
#define LOG_V(x, y) std::cout << y << " -> "; \
for(auto i = x.begin(); i != x.end(); i++) { \
std::cout << *i << " "; \
}\
std::cout<<std::endl;
#else
#define LOG(x, y)
#define LOG_V(x, y)
#endif
int main(){
// Input
std::string expre = "";
std::cout<<"Math exasperation: ";
std::getline(std::cin >> std::ws,expre);
LOG(expre,"Input");
// Checks on the string
if (expre == "") {
std::cout<<"Error: Must put an math exasperation"<<std::endl;
return 1;
}
if (clean_string(expre) == 1) {
return 1;
}
LOG(expre,"Cleaning the string");
if (closed_brackets(expre) == 1){
return 1;
}
LOG(expre,"Brackets as it should");
if (closer(expre) == 1) {
return 1;
}
LOG(expre,"Closer");
edge_case(expre);
LOG(expre, "More then 3 digits");
if (expre.length()<3) {
std::cout<<"Error: Exasperation must be more then 3 digits"<<std::endl;
return 1;
}
LOG(expre,"Fix spelling");
if (num_size_chack(expre) == 1) {
return 1;
}
LOG(expre, "Check numbers size");
// String to Vector
std::vector<double> expre_v = str_to_vector(expre);
std::vector<double> calc;
LOG_V(expre_v, "Str to Vec");
// Calculating
// Vector of braces
std::vector<int> brace_loc;
// By the order of math
do {
// Clearing the Vector
calc.clear();
// Indexes to operate in
brace_loc = start_and_finish_ind(expre_v);
// What part do we calculate
if (brace_loc.size() == 0) {
calc = expre_v;
} else {
for(int i = brace_loc[0]+1 ; i<brace_loc[1]; i++){
calc.push_back(expre_v[i]);
}
}
bool finished_calc = false;
frac_do(calc);
LOG_V(calc, "Frac");
if (size_vector(calc) == 0) {
LOG(calc[0], ">>>");
finished_calc = true;
}
if (!finished_calc) {
mul_do(calc);
LOG_V(calc, "Mul");
if (size_vector(calc) == 0) {
LOG(calc[0], ">>>");
finished_calc = true;
}
}
if (!finished_calc) {
add_do(calc);
LOG_V(calc, "Add");
if (size_vector(calc) == 0) {
LOG(calc[0], ">>>");
finished_calc = true;
}
}
if (brace_loc.size() == 0) {
// No braces
expre_v = calc;
} else {
std::vector<double> temp;
// Before the braces
for(int i=0; i<brace_loc[0];i++){
temp.push_back(expre_v[i]);
}
// The result of the braces
temp.push_back(calc[0]);
// After the braces
for(size_t i=brace_loc[1]+1; i<expre_v.size();i++){
temp.push_back(expre_v[i]);
}
expre_v = temp;
}
}while ( brace_loc.size() != 0);
// Printing the result
std::cout<<">> "<<expre_v[0]<<std::endl;
return 0;
}