Pagini recente » Cod sursa (job #1981113) | Cod sursa (job #2660646) | Cod sursa (job #634973) | Cod sursa (job #78347) | Cod sursa (job #2867003)
#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;
}
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] == '/')
{
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);
while(!s.empty() && (s.top() == '*' || s.top() == '/'))
{
int x = nr.top();
nr.pop();
int y = nr.top();
nr.pop();
char semn = s.top();
s.pop();
int cal = eval(y,x,semn);
nr.push(cal);
}
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();
}