Cod sursa(job #1080558)

Utilizator sddddgjdZloteanu Anastasia sddddgjd Data 12 ianuarie 2014 16:58:15
Problema Evaluarea unei expresii Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 3.42 kb
#include<stdio.h>
char v[200001];
char v1[200000];
char stack[100001];
int stack1[100001];
int precedence[]= {3,2,0,2,0,3};
int RPN(int n)
{
    int size=-1;
    int cont=0,cont2=0;
    while(cont<n)
    {
        if(v[cont]<='9'&&v[cont]>='0')
        {
            while(v[cont]<='9'&&v[cont]>='0'&&cont<n)
            {
                v1[cont2++]=v[cont];
                cont++;
            }
            v1[cont2++]=' ';
        }
        if(v[cont]=='+'||v[cont]=='*'||v[cont]=='/'||v[cont]=='-')
        {
            int pp=0;
            while(size>=0&&stack[size]!='('&&precedence[v[cont]-'*']<=precedence[stack[size]-'*'])
            {
                pp++;
                v1[cont2++]=stack[size];
                size--;
            }
            if(pp)
                stack[++size]=v[cont];
            else
                stack[++size]=v[cont];
        }
        if(v[cont]=='(')
            stack[++size]='(';
        if(v[cont]==')')
        {
            while(size>=0&&stack[size]!='(')
            {
                v1[cont2++]=stack[size];
                size--;
            }
            size--;
        }
        cont++;
    }
    while(size>=0)
    {
        v1[cont2++]=stack[size];
        size--;
    }
    return cont2;
}
int main()
{
    FILE *fin,*fout;
    fin=fopen("expr.in","r");
    fout=fopen("expr.out","w");
    char ch=fgetc(fin);
    int cont=0;
    while(ch!='\n'&&ch!=EOF)
    {
        if(ch=='-')
        {
            if(cont==0||v[cont-1]=='(')
               v[cont++]='0';
        }
        v[cont]=ch;
        cont++;
        ch=fgetc(fin);
    }
    int cont1=RPN(cont);
    int size=0,i=0;
    while(i<cont1)
    {
        if(v1[i]>='0'&&v1[i]<='9')
        {
            while(i<cont1&&v1[i]>='0'&&v1[i]<='9')
            {
                stack1[size]=stack1[size]*10+(v1[i]-'0');
                i++;
            }
            size++;
        }
        else
        {
            switch(v1[i])
            {
            case '/':
                if(size>=2)
                {
                    stack1[size-2]=stack1[size-2]/stack1[size-1];
                    stack1[size-1]=0;
                    size--;
                }
                else if(size>=1)
                    stack[size-1]=0/stack[size-1];
                break;
            case '*':
                if(size>=2)
                {
                    stack1[size-2]=stack1[size-2]*stack1[size-1];
                    stack1[size-1]=0;
                    size--;
                }
                else if(size>=1)
                    stack[size-1]=0*stack[size-1];
                break;
            case '+':
                if(size>=2)
                {
                    stack1[size-2]=stack1[size-2]+stack1[size-1];
                    stack1[size-1]=0;
                    size--;
                }
                else if(size>=1)
                    stack[size-1]=0+stack[size-1];
                break;
            case '-':
                if(size>=2)
                {
                    stack1[size-2]=stack1[size-2]-stack1[size-1];
                    stack1[size-1]=0;
                    size--;
                }
                else if(size>=1)
                    stack[size-1]=0-stack[size-1];
                break;
            }
        }
        i++;
    }
    fprintf(fout,"%d",stack1[size-1]);
    return 0;
}