-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.cpp
152 lines (145 loc) · 4.16 KB
/
clean.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
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <cstddef>
#include <iostream>
#include <string>
#include "clean.h"
// For Debugging
#define DEBUG 0
#if DEBUG == 1
#define LOG(x) std::cout<<x<<std::endl
#else
#define LOG(x)
#endif
int clean_string(std::string &str) {
// No spaces, and make shore all the chars are valid
// The clean string
std::string newStr = "";
for (size_t i = 0; i < str.length(); i++) {
if (str[i] == ' ') {
// We don't want to include space in the clean string
continue;
} else if ((str[i] >= '0' && str[i] <= '9')||(str[i]=='/' ||str[i]=='+' ||str[i]=='-' ||str[i]=='*' ||str[i]=='(' ||str[i]==')')) {
newStr += str[i];
} else {
std::cout << "Error: Invalid character '" << str[i] << "'" << std::endl;
return 1;
}
}
str = newStr; // Assign the cleaned string back to the original string.
return 0;
}
int closer(std::string &str){
if(str[0] == '-') {
str = '0'+str;
} else if(str[0] == '+') {
str.erase(str.begin()); // Erase the first character
} else if(str[0] == '*' || str[0] == '/'){
std::cout<<"Error: Can't start a math exasperation with '"<<str[0]<<"'"<<std::endl;
return 1;
}
if (!((str.back() >= '0' && str.back() <= '9')||(str.back() == ')'))){
std::cout<<"Error: Can't end a math exasperation with '"<<str.back()<<"'"<<std::endl;
return 1;
}
return 0;
}
int closed_brackets(std::string &str){
// Make shore all the brackets are closed
// A counter will be added 1 on every '(' and subtracted 1 on ')'. If counter < 0 at the operation we have more ')' then '(', and if at the end we don't have counter == 0 not all the brackets are closed
int counter = 0;
LOG("Counter Log:");
for(size_t i=0;i<str.length();i++){
// Update counter
LOG(counter);
if(str[i] == '('){
counter++;
} else if (str[i] == ')') {
counter--;
}
// Check if counter < 0
if (counter < 0) {
std::cout<<"Error: Check your brackets in the exasperation"<<std::endl;
return 1;
}
}
if (counter == 0) {
// All good
return 0;
}
// Not good
std::cout<<"Error: Check your brackets in the exasperation"<<std::endl;
return 1;
}
int num_size_chack(std::string &str){
// Checking there isn't a number above the MAX_DIG number of digits
int digit_counter = 0;
for(size_t i = 0; i<str.length();i++){
if (str[i]<='9' && str[i] >='0') {
// One more digit
digit_counter++;
} else if (digit_counter > MAX_DIG) {
// Too big
std::cout<<"Error: Number with more then "<<MAX_DIG<<" digits"<<std::endl;
return 1;
} else {
// Not a digit
digit_counter = 0;
}
}
// All good
return 0;
}
void edge_case(std::string &str) {
// Fix the string after he pass throw the clean_string function
bool did_a_fix_happend = 0;
do {
LOG("In the loop");
did_a_fix_happend = 0;
std::string newStr = "";
// Fix `-` and no number buffer it
for (size_t i = 0; i <str.length()-1; i++) {
if(str[i]=='(' && str[i+1] == '-'){
LOG("(- -> (0-");
newStr+= "(0";
did_a_fix_happend = 1;
} else if((str[i]=='-' && str[i+1] == '+')||(str[i]=='+' && str[i+1] == '-')){
LOG("+- -> -");
did_a_fix_happend = 1;
newStr+='-';
i++;
} else if((str[i]=='-' && str[i+1] == '(')){
LOG("-( -> -1*(");
did_a_fix_happend = 1;
newStr+= "-1*(";
i++;
} else if((str[i]=='-' && str[i+1] == '-')||(str[i]=='+' && str[i+1] == '+')){
LOG("-- -> + / ++ -> +");
did_a_fix_happend = 1;
newStr+= '+';
i++;
} else if(str[i]=='*' && str[i+1] == '-') {
LOG("*- ->*(0-1)*");
did_a_fix_happend = 1;
newStr+="*(0-1)*";
i++;
} else if(str[i]=='/' && str[i+1] == '-' ) {
LOG("/- -> *(0-1)/");
did_a_fix_happend = 1;
newStr+="*(0-1)/";
i++;
} else if(( str[i] >= '0' && str[i] <='9') && str[i+1] == '(' ){
LOG("number( -> number*(");
did_a_fix_happend = 1;
newStr+= str[i];
newStr+= '*';
} else {
newStr += str[i];
}
LOG(newStr);
}
if (did_a_fix_happend) {
newStr += str.back(); // Append the last character
str = newStr; // Assign the cleaned string back to the original string.
}
}while (did_a_fix_happend);
// If fix happened we way need to do anther one
}