Cod sursa(job #2147682)
Utilizator | Data | 28 februarie 2018 21:45:42 | |
---|---|---|---|
Problema | Evaluarea unei expresii | Scor | 40 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 2.53 kb |
#include <fstream>
#include <iostream>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
queue <int> Q;
stack <int> v;
int a,w,q,r,b,k,e,t;
char p[100001];
int main()
{
f>>p;
w=strlen(p);
for(int i=0; i<w; i++)
{
if(isdigit(p[i]))
{
q=p[i]-'0';
r=r*10+q;
k=1;
}
else
{
if(k==1)
{
Q.push(r);
r=0;
k=0;
}
if(p[i]=='+' || p[i]=='-')
{
t=1;
while(t && !v.empty())
{
if(v.top()==1000000001 || v.top()==1000000002)
{
Q.push(v.top());
v.pop();
}
else
t=0;
}
if(p[i]=='+')
v.push(1000000003);
if(p[i]=='-')
v.push(1000000004);
}
if(p[i]=='*')
{
v.push(1000000001);
}
if(p[i]=='/')
{
v.push(1000000002);
}
if(p[i]=='(')
{
v.push(1000000005);
}
if(p[i]==')')
{
t=1;
while(t)
{
if(v.top()==1000000005)
{
t=0;
break;
}
else
{
Q.push(v.top());
v.pop();
}
}
v.pop();
}
}
}
if(k==1)
Q.push(r);
while(!v.empty())
{
Q.push(v.top());
v.pop();
}
while(!Q.empty())
{
if(Q.front()>1000000000)
{
a=v.top();
v.pop();
b=v.top();
v.pop();
if(Q.front()==1000000001)
e=a*b;
if(Q.front()==1000000002)
e=b/a;
if(Q.front()==1000000003)
e=a+b;
if(Q.front()==1000000004)
e=b-a;
v.push(e);
}
else
v.push(Q.front());
Q.pop();
}
g<<v.top();
return 0;
}