Pagini recente » Monitorul de evaluare | Cod sursa (job #1520372) | Profil EdgeLordXD | Monitorul de evaluare | Cod sursa (job #2229076)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("evaluare.in");
ofstream fout ("evaluare.out");
stack <char> rat;
stack <int> tori;
char infix[100005];
int n;
int resh(int v1,int v2, char op){
if(op=='+') return v1+v2;
if(op=='-') return v2-v1;
if(op=='*') return v2*v1;
if(op=='/') return v2/v1;
}
int prio (char c){
if(c=='(') return 0;
if(c=='+'|| c=='-') return 1;
return 2;
}
int main()
{
fin.get(infix,100005);
n=strlen(infix);
int i=0;
while(i<n){
char curent = infix[i];
if(curent<58 && curent>47 ){
int num = 0;
while(curent<58 && curent>47){
num*=10; num+=curent-'0';
i++; curent = infix[i];
}
i--;
tori.push(num);
}
else if (curent=='(') rat.push(curent);
else if (curent==')')
{
while(rat.top()!= '(')
{
int v1=tori.top();
tori.pop();
int v2=tori.top();
tori.pop();
char op=rat.top();
rat.pop();
tori.push(resh(v1,v2,op));
}
rat.pop();
}
else
{
while(!rat.empty() && (prio(rat.top())>=prio(curent)))
{
int v1=tori.top();
tori.pop();
int v2=tori.top();
tori.pop();
char op=rat.top();
rat.pop();
tori.push(resh(v1,v2,op));
}
rat.push(curent);
}
i++;
}
while(!rat.empty())
{
int v1=tori.top();
tori.pop();
int v2=tori.top();
tori.pop();
char op=rat.top();
rat.pop();
tori.push(resh(v1,v2,op));
}
fout<<tori.top();
return 0;
}