This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMath.cpp
234 lines (221 loc) · 4.48 KB
/
Math.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "Math.h"
#include "Tree.h"
#define ADD_FUNC(func) \
if (strcmp(f.c_str(), func) == 0)\
{\
tmp = new CNode(func, GetExp());\
}\
else\
Math::Math()
{
str = string();
}
void Math::PutStr(string st)
{
str = st;
S = str.begin();
}
string Math::GetEquation()
{
ifstream File;
File.open ("exp.txt");
if (File.is_open()) printf("File is open\n");
getline(File, str);
cout << str << endl;
File.close();
return str;
}
string Math::PrepareEquation()
{
string tmp;
int count = 0;
try
{
for(S = str.begin(); S != str.end(); S++)
{
if(((*S >= '0') && (*S <= '9')) || ((*S >= 'a') && (*S <= 'z')) || (*S == '+') || (*S == '-') || (*S == '*') || (*S == '/') || (*S == '^'))
{
tmp += *S;
}
else if ((*S == '(') || (*S == ')'))
{
tmp += *S;
if (*S == '(')
{
count += 1;
}
if (*S == ')')
{
count -= 1;
}
if (count < 0)
{
break;
}
}
else if(*S == '\0')
{
break;
}
else if((*S != ' ') && (*S != 10))
{
throw string("Incorrect expression. Please correct it\n") ;
}
}
if(count != 0)
{
throw string("Incorrect expression. Check '(' and ')'. Please correct it\n") ;
}
}
catch(string ex)
{
cout << ex << endl;
exit(-1);
}
return tmp;
}
CNode* Math::GetExp()
{
CNode* tmp = GetMulDiv();
while(*S == '-' || *S =='+')
{
if (*S == '+')
{
S++;
CNode* tmp1 = new CNode('+', tmp, GetMulDiv());
tmp = tmp1;
}
else if (*S == '-')
{
S++;
CNode* tmp1 = new CNode('-', tmp, GetMulDiv());
tmp = tmp1;
}
}
return tmp;
}
CNode* Math::GetMulDiv()
{
CNode* tmp= GetPow();
while(*S == '*' || *S == '/')
{
if (*S == '*')
{
S++;
CNode* tmp1 = new CNode('*', tmp, GetPow());
tmp = tmp1;
}
else if (*S == '/')
{
S++;
CNode* tmp1 = new CNode('/', tmp, GetPow());
tmp = tmp1;
}
}
return tmp;
}
CNode* Math::GetPow()
{
CNode* tmp = GetPas();
if(*S == '^')
{
S++;
CNode* tmp1 = new CNode('^', tmp, GetPas());
tmp = tmp1;
}
return tmp;
}
CNode* Math::GetPas()
{
CNode* tmp;
if (*S == '(')
{
S++;
tmp = GetExp();
try
{
if (*S != ')')
throw string ("Expected ')'. Please correct expression\n");
}
catch(string ex)
{
cout << ex << endl;
exit(1);
}
S++;
}
else if (*S == 'x') //пока считаем, что переменная только x( потом добавить все переменные)
{
S++;
tmp = new CNode('x');
}
else if (((*S >= 'a') && (*S <= 'z')))
{
tmp = GetFunc();
}
else
{
tmp = GetNum();
}
return tmp;
}
CNode* Math::GetFunc()
{
CNode* tmp;
string f;
while(*S != '(')
{
f += *S;
S++;
}
S++;
try
{
ADD_FUNC("sin")
ADD_FUNC("cos")
ADD_FUNC("tg")
ADD_FUNC("ctg")
ADD_FUNC("arcsin")
ADD_FUNC("arccos")
ADD_FUNC("arctg")
ADD_FUNC("arcctg")
ADD_FUNC("ln")
ADD_FUNC("lg")
ADD_FUNC("sh")
ADD_FUNC("ch")
ADD_FUNC("th")
ADD_FUNC("cth")
{
throw string ("This function not exist. Please correct expression\n");
}
S++;
}
catch (string ex)
{
cout << ex << endl;
exit(1);
}
return tmp;
}
CNode* Math::GetNum()
{
double val = 0.0;
string::iterator debug = S;
while('0' <= *S && *S <= '9')
{
val = val*10 + *S - '0';
S++;
}
try
{
if(debug == S)
throw string("Expected number. Please correct expression\n");
}
catch(string ex)
{
cout << ex << endl;
exit(1);
}
CNode* tmp = new CNode(val);
return tmp;
}