-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1935.cpp
74 lines (68 loc) · 1.28 KB
/
P1935.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
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
using namespace std;
int N;
int nums[100] = { 0 };
vector<string> v;
bool isCalc(string s) {
char c = s[0];
return c == '*' || c == '/' || c == '+' || c == '-';
}
double calculate(double x, double y, char calc) {
char switch_on = calc;
switch (switch_on)
{
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
default:
break;
}
return 0;
}
string processData(char* s) {
int i = 0;
while (s[i] != '\0' || s[i] != NULL || s[i] != 0) {
if (s[i] >= 'A'&&s[i] <= 'Z') {
v.push_back(to_string(nums[s[i] - 'A']));
}
else {
v.push_back(to_string(s[i]));
if (isCalc(v.back())) {
char calc = v.back()[0];
v.pop_back();
double B = stod(v.back()); v.pop_back();
double A = stod(v.back()); v.pop_back();
double RET = calculate(A, B, calc);
v.push_back(to_string(RET));
}
else {
printf("%f, ", stod(v.back()));
}
}
i++;
}
return v.back();
}
// 1 2 3 *+4 5 /-
// 1 6 +4 5 /-
// "2 3 *"->"6"
int main() {
char syntax[101];
scanf("%d", &N);
scanf("%s",syntax);
for (int i = 0; i < N; i++)
scanf("%d",nums+i);
string s = processData(syntax);
cout << s;
return 0;
}