Initial commit

This commit is contained in:
xixu-me committed 2024-03-17 22:30:49 +08:00
commit bdf71aef37
120 files changed
+8284

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
#include <bits/stdc++.h>
using namespace std;
string s;
int change(int l, int r) {
int sum = 0;
for (int i = l; i <= r; i++)
sum = sum * 10 + s[i] - '0';
return sum;
}
int calc(int l, int r) {
int cnt = 0, pos1 = -1, pos2 = -1, pos3 = -1;
for (int i = l; i <= r; i++) {
if (s[i] == '(')
cnt++;
if (s[i] == ')')
cnt--;
if (cnt <= 0 && (s[i] == '+' || s[i] == '-'))
pos1 = i;
if (cnt <= 0 && (s[i] == '*' || s[i] == '/'))
pos2 = i;
if (cnt <= 0 && s[i] == '^')
pos3 = i;
}
if (pos1 == -1 && pos2 == -1 && pos3 == -1 && cnt == 0) {
if (s[l] == '(')
return calc(l + 1, r - 1);
else
return change(l, r);
}
if (pos1 != -1) {
if (s[pos1] == '+')
return calc(l, pos1 - 1) + calc(pos1 + 1, r);
else
return calc(l, pos1 - 1) - calc(pos1 + 1, r);
}
if (pos2 != -1) {
if (s[pos2] == '*')
return calc(l, pos2 - 1) * calc(pos2 + 1, r);
else
return calc(l, pos2 - 1) / calc(pos2 + 1, r);
}
if (pos3 != -1)
return pow(calc(l, pos3 - 1), calc(pos3 + 1, r));
if (cnt > 0 && s[l] == '(')
return calc(l + 1, r);
if (cnt < 0 && s[l] == ')')
return calc(l, r - 1);
return 0;
}
int main() {
cin >> s;
cout << calc(0, s.length() - 1);
return 0;
}