Mai intai trebuie sa te autentifici.
Cod sursa(job #805507)
Utilizator | Data | 31 octombrie 2012 16:22:10 | |
---|---|---|---|
Problema | Evaluarea unei expresii | Scor | 50 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.36 kb |
using namespace std;
#include<fstream>
#include<ctype.h>
#include<cstring>
#include<stdlib.h>
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char x[200001],polo[200001],st[200001],op[]="+-*/()",c;
int n,i,k,vf=-1,l;
int y,st2[100001];
char pop()
{
if (vf>=0) return st[vf--];
return 0;
}
void push(char c)
{
st[++vf]=c;
}
int main()
{
fin.getline(x,100000);
n=strlen(x);
for(i=0;i<n;i++)
{
if(!strchr(op,x[i]))
{
while(!strchr(op,x[i+1]))
{
polo[k++]=x[i];
i++;
}
polo[k++]=x[i];
polo[k++]='>';
}
else
switch (x[i])
{
case '(': push(x[i]);break;
case ')': while((c=pop())!='(') polo[k++]=c;break;
case '*': case '/':
while( strchr("*/",st[vf]) ) {polo[k++]=pop();}
push(x[i]);
break;
case '+': case '-':
while( st[vf]!='('&& (vf!=-1) )
{
polo[k++]=pop();
}
push(x[i]);
}
}
while((c=pop())!=0) polo[k++]=c;
polo[k++]=0;
n=strlen(polo);
l=-1;
for(i=0;i<n;i++)
{
vf=0;
st[0]=0;
while(isdigit(polo[i]))
st[vf++]=polo[i++];
if (polo[i]=='>')
{
st[vf]=0;
st2[++l]=atoi(st);
}
switch(polo[i])
{ case '*':st2[l-1]=st2[l-1]*st2[l--];break;
case '+':st2[l-1]=st2[l-1]+st2[l--]; break;
case '-':st2[l-1]=st2[l-1]-st2[l--]; break;
case '/':st2[l-1]=st2[l-1]/st2[l--];break;
}
}
fout<<st2[0];
return 0;
}