Cod sursa(job #1903140)

Utilizator lucadDragomir Luca lucad Data 4 martie 2017 23:49:42
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.94 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int prioritate(char c)
{
    if(c=='('||c==')')return 0;
       else
         if(c=='+'||c=='-')return 1;
        else
            if(c=='*'||c=='/')return 2;
}
int main()
{
    char E[100003],FP[100003],S[100000], aux[4],sirnr[20],*p;
    int i,vf,nr,semn,S1[1],R,k;
    FILE *f,*g;
    f=fopen("evaluare.in","r");
    g=fopen("evaluare.out","w");
    fgets(E,100000,f);
    //puts(E);
    E[strlen(E)-1]=NULL;
    strcpy(FP,"(");
    strcat(FP,E);
    strcat(FP,")");
    strcpy(E,FP);

    //puts(E);
    strcpy(FP,"");
    vf=0;
    for(i=0;E[i]!=NULL;i++)
    {
        if(E[i]>='0' && E[i]<='9')
        {
            nr=0;
            if (E[i-1]=='-' && E[i-2]=='('&&i-2>=0) semn=-1;
                             else semn=1;
            k=0;
            if (semn==-1)
            {
                sirnr[0]='-';
                k++;
            }
            while(E[i]>='0' && E[i]<='9')
            {
                nr=nr*10+E[i]-48;
                sirnr[k]=E[i];
                k++;
                i++;

            }
            sirnr[k]=NULL;
            nr=nr*semn;

            //itoa(nr,sirnr,10);
            strcat(FP,sirnr);
            strcat(FP," ");
            i--;
           // puts(FP);
        }
         else
            if(E[i]=='+'||(E[i]=='-' &&E[i-1]!='(')||E[i]=='*'||E[i]=='/')
         {
             while(prioritate(S[vf])>=prioritate(E[i]))
             {
                 aux[0]=S[vf];
                 aux[1]=' ';
                 aux[2]=NULL;
                 strcat(FP,aux);
                 vf--;
             }
             vf++;
             S[vf]=E[i];
         }
           else
             if(E[i]=='(')
                {
                    vf++;
                    S[vf]=E[i];
                }
               else
               if(E[i]==')')
                {
                   while(S[vf]!='(')
                   {
                       aux[0]=S[vf];
                       aux[1]=' ';
                       aux[2]=NULL;
                       strcat(FP,aux);
                       vf--;
                   }
                   vf--;
                }
        //for(int j=1;j<=vf;j++)
            //printf("%c ",S[j]);
        //printf("\n");
        //printf("%d ",i);
    }
    //puts(FP);
    p=strtok(FP," ");
    vf=0;
    while(p!=NULL)
    {
        if(*p=='+' || (*p=='-' && *(p+1)==NULL) ||*p=='*' || *p=='/')
         {
             if(*p=='+') R=S1[vf]+S1[vf-1];
             if(*p=='-') R=S1[vf-1]-S1[vf];
             if(*p=='*') R=S1[vf]*S1[vf-1];
             if(*p=='/') R=S1[vf-1]/S1[vf];
             vf--;
             S1[vf]=R;
         }
           else
           {
               vf++;
               S1[vf]=atoi(p);
           }
        p=strtok(NULL," ");
    }
    fprintf(g,"%d",S1[1]);
    fclose(f);
    fclose(g);
    return 0;

}