forked from BalgopalPatro/MyCodes
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.cpp
58 lines (55 loc) · 1.62 KB
/
test.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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> diffWaysToCompute(string input)
{
vector<int> ans;
bool flag = true;
int size = input.length();
for (size_t i = 0; i < size; i++)
{
char ch = input[i];
if (ch == '+' || ch == '-' || ch == '*')
{
flag = false;
vector<int> ans_left = diffWaysToCompute(input.substr(0, i));
vector<int> ans_right = diffWaysToCompute(input.substr(i + 1));
for (int n = 0; n < ans_left.size(); n++)
{
for (int m = 0; m < ans_right.size(); m++)
{
switch (ch)
{
case '+':
ans.push_back(ans_left[n] + ans_right[m]);
break;
case '-':
ans.push_back(ans_left[n] - ans_right[m]);
break;
case '*':
ans.push_back(ans_left[n] * ans_right[m]);
break;
}
}
}
}
}
if (flag)
{
ans.push_back(stoi(input));
cout<<"Conveted "<<stoi(input)<<endl;
return ans;
}
return ans;
}
};
int main()
{
Solution sl ;
vector<int> a = sl.diffWaysToCompute("2*3-4*5");
for(int v:a){
cout<<v<<" ";
}
}