-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.h
More file actions
139 lines (120 loc) · 2.78 KB
/
math.h
File metadata and controls
139 lines (120 loc) · 2.78 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
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
#pragma once
#include <string>
#include <vector>
using namespace std;
string trim(std::string src)
{
auto start = src.find_first_not_of(" ");
auto end = src.find_last_not_of(" ");
if (start == string::npos && end != string::npos)
return src.substr(0, end + 1);
else if(start != string::npos && end == string::npos)
return src.substr(start);
else if (start != string::npos && end != string::npos)
return src.substr(start, end - start + 1);
else
return src;
return src;
}
int splite(string str, vector<string>& vecTmp, char& ch)
{
int nret = -1;
ch = 0;
int pos = 0;
for (; pos < str.length(); ++pos)
{
if (str.at(pos) == '+' || str.at(pos) == '-' || str.at(pos) == '*' || str.at(pos) == '/')
{
ch = str.at(pos);
break;
}
}
//
if (ch == 0)
return nret;
string val1 = str.substr(0, pos - 1);
vecTmp.push_back(trim(val1));
if (val1.find("x") != std::string::npos)
nret = 0;
auto pos1 = str.find("=");
if (std::string::npos == pos1)
return -1;
string val2 = str.substr(pos + 1, pos1 - pos - 1);
vecTmp.push_back(trim(val2));
if (val2.find("x") != std::string::npos)
nret = 1;
string res = str.substr(pos1 + 1);
vecTmp.push_back(trim(res));
if (res.find("x") != std::string::npos)
nret = 2;
return nret;
}
std::string add(std::string val1, std::string val2)
{
int val = atoi(val1.c_str()) + atoi(val2.c_str());
return to_string(val);
}
std::string sub(std::string val1, std::string val2)
{
int val = atoi(val1.c_str()) - atoi(val2.c_str());
return to_string(val);
}
std::string multi(std::string val1, std::string val2)
{
int val = atoi(val1.c_str()) * atoi(val2.c_str());
return to_string(val);
}
std::string desp(std::string val1, std::string val2)
{
int val = atoi(val1.c_str()) / atoi(val2.c_str());
return to_string(val);
}
std::string match(std::string dst, std::string src)
{
auto pos = dst.find("x");
if (pos == string::npos)
return "";
int left = 0;
int right = dst.length() - 1;
//for (; left < right && src.at(left) != 'x'; ++left);
for (; right > pos && dst.at(right) != 'x'; --right);
return src.substr(pos, right - pos + 1);
}
string MathChallenge(string str) {
vector<string> vecTmp;
char ch;
int ret = splite(str, vecTmp, ch);
if(ret < 0)
return "";
std::string res;
switch (ch)
{
case '+':
if (ret == 2)
res = add(vecTmp[0], vecTmp[1]);
else
res = sub(vecTmp[2], vecTmp[1 - ret]);
break;
case '-':
if (ret == 1)
res = add(vecTmp[2], vecTmp[1]);
else
res = sub(vecTmp[0], vecTmp[3 - ret]);
break;
case '*':
if (ret == 2)
res = multi(vecTmp[0], vecTmp[1]);
else
res = desp(vecTmp[2], vecTmp[1 - ret]);
break;
case '/':
if (ret == 0)
res = multi(vecTmp[1], vecTmp[2]);
else
res = desp(vecTmp[0], vecTmp[3 - ret]);
break;
default:
break;
}
return match(vecTmp[ret], res);
}