Pagini recente » Cod sursa (job #3000285) | Cod sursa (job #1946061) | Cod sursa (job #2895754) | Cod sursa (job #1965735) | Cod sursa (job #180264)
Cod sursa(job #180264)
#include<fstream.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char x[100001],polo[100001],st[100001],op[]="+-*/()",c;
long s[100001],l=-1;
int n,i,k,vf=-1;
char pop()
{
if (vf>=0) return st[vf--];
return 0;
}
void push(char c)
{
st[++vf]=c;
}
int main()
{
fin.getline(x,100000);
for(i=0;x[i];i++)
{
if (!strchr(op,x[i]))
{
while(!strchr(op,x[i]))
polo[k++]=x[i++];
polo[k++]='.';
}
switch (x[i])
{
case '(': push(x[i]);break;
case ')': while((c=pop())!='(') polo[k++]=c;break;
case '*': case '/':
if( strchr("(+-",st[vf]) || (vf==-1)) push(x[i]);
else
if( strchr("*/",st[vf]) ) {polo[k++]=pop();i--;}break;
case '+': case '-':
if( st[vf]=='('|| (vf==-1) ) push(x[i]);
else
{polo[k++]=pop();i--;}
}
}
while((c=pop())!=0) polo[k++]=c;
polo[k++]=0;
//Evaluarea formei poloneze
for(i=0;polo[i];i++)
{
k=0;
if (isdigit(polo[i]))
{
while(isdigit(polo[i]))
x[k++]=polo[i++];
x[k++]=0;
s[++l]=atoi(x);
}
else
switch (polo[i])
{
case '+': s[l-1]=s[l-1]+s[l--];break;
case '-': s[l-1]=s[l-1]-s[l--];break;
case '*': s[l-1]=s[l-1]*s[l--];break;
case '/': s[l-1]=s[l-1]/s[l--];break;
}
}
fout<<s[l];
return 0;
}