Untitled
unknown
plain_text
3 years ago
1.1 kB
13
Indexable
#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
string s;
stack<string> st1;
stack<char> st2;
getline(cin, s);
int n = s.length();
for (int i = 0; i < n; i++)
{
if (s[i] == '(')
{
string ss = "(";
st1.push(ss);
i++;
}
else if (s[i] == ')')
{
char op = st2.top();
st2.pop();
if (op == '+')
{
int res = 0;
while (st1.top() != "(")
{
res += stoi(st1.top());
st1.pop();
}
st1.pop();
st1.push(to_string(res));
}
else
{
int res = 1;
while (st1.top() != "(")
{
res *= stoi(st1.top());
st1.pop();
}
st1.pop();
st1.push(to_string(res));
}
i++;
}
else if (isdigit(s[i]))
{
int j = i + 1;
while (isdigit(s[j]))
j++;
st1.push(s.substr(i, j - 1));
i = j;
}
else
{
st2.push(s[i]);
i++;
}
}
cout << st1.top() << endl;
}Editor is loading...