Cod sursa(job #773125)

Utilizator misu007Pogonaru Mihai misu007 Data 1 august 2012 02:07:26
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.98 kb
#include <fstream>
#include <cstring>
using namespace std;

char expr[100001],exprfp[100001];
int k=0,n;
ifstream f("evaluare.in");
ofstream g("evaluare.out");

void cit()
{
    f.get(expr,100000);
    n=strlen(expr);
}

int prio(char c)
{
    switch(c)
    {
        case '+':
        case '-': return 2;
        case '*':
        case '/': return 3;
        case ')': return 1;
        case '(': return 0;
        default: return 4;
    }
}

void fp()
{
    int i=0,st1=0;
    char st[100001];
    while(expr[i]!='\0')
    {
        switch(prio(expr[i]))
        {
            case 3: while(prio(expr[i])<=prio(st[st1-1])&&st1>0)
                    {
                        exprfp[k++]=st[--st1];
                    }
                    st[st1++]=expr[i++];
                    break;
            case 2: while(prio(expr[i])<=prio(st[st1-1])&&st1>0)
                    {
                        exprfp[k++]=st[--st1];
                    }
                    st[st1++]=expr[i++];
                    break;
            case 1: while(st[st1-1]!='('&&st1>0)
                    {
                        exprfp[k++]=st[--st1];
                    }
                    st1--;
                    i++;
                    break;
            case 0: st[st1++]=expr[i++];
                    break;
            default: exprfp[k++]=expr[i];
                     while(prio(expr[++i])==4&&i<n)
                     {
                         exprfp[k++]=expr[i];
                     }
                     exprfp[k++]='#';
                     break;
        }
    }
    while(st1>0)
    {
        exprfp[k]=st[st1-1];
        st1--;
        k++;
    }
}

int conv(char c)
{
    switch(c)
    {
        case '0': return 0;
        case '1': return 1;
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
    }
}

void eval()
{
    int i=0,st[100000],st1=0;
    while(i<k)
    {
        switch(prio(exprfp[i]))
        {
            case 4: st[st1]=0;
                    while(exprfp[i]!='#')
                    {
                        st[st1]=st[st1]*10+conv(exprfp[i++]);
                    }
                    st1++;
                    i++;
                    break;
            default: switch(exprfp[i++])
                     {
                         case '*':st[st1-2]=st[--st1]*st[--st1];break;
                         case '/':st[st1-2]=st[st1-2]/st[--st1];st1--;break;
                         case '+':st[st1-2]=st[--st1]+st[--st1];break;
                         case '-':st[st1-2]=st[st1-2]-st[--st1];st1--;break;
                     }
                     st1++;
                     break;
        }
    }
    g<<st[0]<<endl;
}

int main()
{
    cit();
    f.close();
    fp();
    eval();
    g.close();
    return 0;
}