Cod sursa(job #2071124)

Utilizator TudorFinaruTudor Cristian Finaru TudorFinaru Data 20 noiembrie 2017 12:31:47
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.17 kb
#include <fstream>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int

int prioritate(char c)
{
    if(c=='*' || c=='/') return 2;
    if(c=='+' || c=='-') return 1;
    return 0;
}

int main()
{
    f.getline(s,100003);
    int kp=1;
   char aux[2]=" ";
    p[1]='(';
    strcat(s,")");
    for(i=0;s[i];i++)
       {
        if(s[i]=='(') p[++kp]=s[i];
         else
           if(s[i]==')')
           { ///extrag tot pana la (
             while(p[kp]!='(')
             {aux[0]=p[kp];
               strcat(fp,aux);
               strcat(fp," ");
               kp--;
             }
               kp--; ///am eliminat si (
           }
          else
             if(strchr("+-*/",s[i]))
              { ///extrag operatorii cu pr >=
                while(prioritate(s[i])<=prioritate(p[kp]))
                { aux[0]=p[kp];
                  strcat(fp,aux);
                  strcat(fp," ");
                  kp--;
                 }
             ///adaug s[i]
             p[++kp]=s[i];
            }
          else
          { ///am ajuns la numar
            int j=0;
            while(s[i]>='0' && s[i]<='9')
              numar[j++]=s[i++];
            numar[j]=0;
            strcat(fp,numar);
            strcat(fp," ");
            i--;
          }

       }
       fout<<fp<<endl;
     ///evaluez expresia
       int kst=0;kp=0;
       point=strtok(fp," ");
       while(point)
       { if(point[0]>='0' && point[0]<'9')
           { ///extrag numarul
             int x=0;
             for(int j=0;point[j];j++)
                x=x*10+(point[j]-'0');
             ///adaug in st
             st[++kst]=x;
           }
         else
           if(kst<2){corect=0;break;}
               else
                  switch(point[0])
               {
                   case  '+': st[kst-1]+=st[kst];kst--;break;
                   case  '-': st[kst-1]-=st[kst];kst--;break;
                   case  '*': st[kst-1]*=st[kst];kst--;break;
                   case  '/': st[kst-1]/=st[kst];kst--;break;
               }
         point=strtok(NULL," ");
       }
      if(corect && kst==1) fout<<st[1];
}