Cod sursa(job #1313265)

Utilizator badea_adi1999Badea Adrian Catalin badea_adi1999 Data 10 ianuarie 2015 14:48:57
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.39 kb
#include <cstring>
#include <cstdio>
#include <fstream>
using namespace std;
ifstream f("evaluare.in");
FILE *g=fopen("evaluare.out","w");
char s[100001];
int st[100000];
int main()
{f.getline(s,100001);
int i,j,top,x;

top = -1;
for (i=0;s[i];i++)
		if (s[i] == '(') st[++top] = -1;
		else if (s[i] == '+') st[++top] = -2;
		else if (s[i] == '*') st[++top] = -3;
		else if (s[i] == '/') st[++top] = -4;
		else if (s[i] == '-') st[++top] = -5;
		else
		if ('0'<=s[i]&&s[i]<='9')
                                  {st[++top] =s[i]-'0';
                                   while ('0'<=s[i+1]&&s[i+1]<='9') st[top]=st[top]*10+s[++i]-'0';
                                  if (top>=1&&st[top-1]==-2)
                                                            {st[top-2]+=st[top];
                                                             st[top]=0;st[top-1]=0;
                                                             top-=2;
                                                             }else
                                  if (top>=1&&st[top-1]==-3)
                                                            {st[top-2]*=st[top];
                                                             st[top]=0;st[top-1]=0;
                                                             top-=2;
                                                             }else
                                  if (top>=1&&st[top-1]==-4)
                                                            {st[top-2]/=st[top];
                                                             st[top]=0;st[top-1]=0;
                                                             top-=2;
                                                             }else
                                  if (top>=1&&st[top-1]==-5)
                                                            {st[top-2]-=st[top];
                                                             st[top]=0;st[top-1]=0;
                                                             top-=2;
                                                             }
                                 }
		else if (s[i] == ')')
		{x=st[top--];
         while (top>=0&&st[top]!=-1)
                            {x=x+st[top];
                             top--;
                             }
         if (top >= 0) {st[top]=x;
                        if (st[top-1]==-2) {st[top-2]+=st[top];
                                            st[top]=0;st[top-1]=0;
                                            top-=2;
                                            }else
                        if (st[top-1]==-3) {st[top-2]*=st[top];
                                            st[top]=0;st[top-1]=0;
                                            top-=2;
                                            }else
                        if (st[top-1]==-4) {st[top-2]/=st[top];
                                            st[top]=0;st[top-1]=0;
                                            top-=2;
                                            }else
                        if (st[top-1]==-5) {st[top-2]-=st[top];
                                            st[top]=0;st[top-1]=0;
                                            top-=2;
                                            }
                        }
                else st[++top] = x;
		}
x = st[top--];
while (top>=0) x=x+st[top--];






return 0;
}