-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindDiffWaysOfParanthesis.cpp
70 lines (61 loc) · 1.31 KB
/
findDiffWaysOfParanthesis.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
#include "headers.h"
int calc(int d1,char o, int d2)
{
int n = 0;
if(o == '+')
{
n = d1+d2;
}
else if(o == '-')
{
n = d1 - d2;
}
else
{
n = d1*d2;
}
return n;
}
vector<int> diffWaysToCompute(string expression)
{
vector<int> r;
if(std::all_of(expression.begin(),expression.end(),[](char c)->bool{return std::isdigit(c);}))
{
int n = std::stoi(expression);
r.push_back(n);
return r;
}
vector<pair<int,char>> os;
int i = 0;
for(char c : expression)
{
if(!isdigit(c))
{
os.push_back(make_pair(i, c));
}
i++;
}
for (int i = 0; i < os.size();i++)
{
string sLeft = expression.substr(0, os[i].first);
string sRight = expression.substr(os[i].first + 1);
vector<int> left = diffWaysToCompute(sLeft);
vector<int> right = diffWaysToCompute(sRight);
for (int j = 0; j<left.size(); j++)
{
for (int k = 0; k < right.size();k++)
{
r.push_back(calc(left[j], os[i].second, right[k]));
}
}
}
return r;
}
void diffWaysToComputeMain()
{
vector<int> r = diffWaysToCompute("1+2*3+4");
for(int a: r)
{
cout << a << endl;
}
}