Pagini recente » Cod sursa (job #1365561) | Cod sursa (job #2623330) | Cod sursa (job #1938518) | Cod sursa (job #963024) | Cod sursa (job #2867010)
#include <bits/stdc++.h>
#define pb push_back
#define MAX 100000
#define mp make_pair
#define mod 1000000007
#define ll long long
#define ull unsigned long long
#define fastio ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
#define FILES freopen("evaluare.in","r",stdin);\
freopen("evaluare.out","w",stdout);
#define int ll
using namespace std;
string a;
int eval(int a,int b,char c)
{
if(c == '+') return a + b;
if(c == '-') return a - b;
if(c == '*') return a * b;
if(c == '/') return a / b;
}
int rang(char c)
{
if(c == '+' || c == '-')
return 1;
if(c == '*' || c == '/')
return 2;
return 0;
}
signed main()
{
fastio
FILES
cin >> a;
stack<char>s;
stack<int> nr;
for(int i = 0; i < a.size(); ++i)
{
if(a[i] == '(')
{
s.push('(');
continue;
}
if(a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/')
{
while(!s.empty() && rang(s.top()) >= rang(a[i]))
{
int x = nr.top();
nr.pop();
int y = nr.top();
nr.pop();
char semn = s.top();
s.pop();
nr.push(eval(y,x,semn));
}
s.push(a[i]);
continue;
}
if(a[i] == ')')
{
while(s.top() != '(')
{
int x = nr.top();
nr.pop();
int y = nr.top();
nr.pop();
char semn = s.top();
s.pop();
nr.push(eval(y,x,semn));
}
s.pop();
}
if(a[i] >= '0' && a[i] <= '9')
{
int nu = 0;
while(i < a.size() && a[i] >= '0' && a[i] <= '9')
nu = nu * 10 + a[i] - '0',i++;
nr.push(nu);
i--;
continue;
}
}
while(!s.empty())
{
int x = nr.top();
nr.pop();
int y = nr.top();
nr.pop();
char semn = s.top();
s.pop();
nr.push(eval(y,x,semn));
}
cout << nr.top();
}