Cod sursa(job #1262037)

Utilizator marta_diannaFII Filimon Marta Diana marta_dianna Data 12 noiembrie 2014 22:17:52
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.43 kb
#include<fstream>
#include<cstring>
#define NMAX 100010

using namespace std;

ifstream f("evaluare.in");
ofstream g("evaluare.out");

struct nodChar
{
    char elt;
    nodChar *succ;
}*sOperatori;

struct nodInt
{
    int numar;
    nodInt *succ;
}*sNumere;

char exp[NMAX];

void adaugaNumere(nodInt* & p, int i)
{
    nodInt *q;
    if (p==NULL)
    {
        p=new nodInt;
        p->numar=0;
        p->succ=NULL;
    }

    if (i==0 || (i>0 && exp[i-1]>='0' && exp[i-1]<='9')) p->numar=p->numar*10+exp[i]-'0';
    else
    {
        q=new nodInt;
        q->numar=exp[i]-'0';
        q->succ=p; p=q;
    }
}

int calculeaza(int a, int b, char c)
{
    switch (c)
    {
        case '+': a+=b; break;
        case '-': a-=b; break;
        case '*': a*=b; break;
        case '/': a/=b; break;
    }
    return a;
}

void adaugaOperatori(nodChar * &p, int i)
{
    nodChar *q;
    nodInt *qN;
    int a, b;
    if (p==NULL)
    {
        p=new nodChar;
        p->elt=exp[i];
        p->succ=NULL;
    }
    else
        if (exp[i]=='(')
        {
            q=new nodChar;
            q->elt=exp[i];
            q->succ=p; p=q;
        }
        else
            if (exp[i]==')')
            {
                while (p->elt!='(')
                {
                    a=sNumere->numar; sNumere=sNumere->succ;
                    b=sNumere->numar; sNumere=sNumere->succ;
                    qN=new nodInt; qN->numar=calculeaza(b, a, p->elt);
                    qN->succ=sNumere;
                    sNumere=qN;
                    p=p->succ;
                }
                p=p->succ;
            }
            else
                if (exp[i]=='+' || exp[i]=='-')
                {
                    while (p->elt=='+' || p->elt=='-' || p->elt=='*' || p->elt=='/')
                    {
                        a=sNumere->numar; sNumere=sNumere->succ;
                        b=sNumere->numar; sNumere=sNumere->succ;
                        qN=new nodInt; qN->numar=calculeaza(b, a, p->elt);
                        qN->succ=sNumere;
                        sNumere=qN;
                        p=p->succ;
                    }
                    q=new nodChar;
                    q->elt=exp[i];
                    q->succ=p;
                    p=q;
                }
                else
                    if (exp[i]!='*' || exp[i]!='/')
                    {
                        while (p->elt=='*' || p->elt=='/')
                        {
                            a=sNumere->numar; sNumere=sNumere->succ;
                            b=sNumere->numar; sNumere=sNumere->succ;
                            qN=new nodInt; qN->numar=calculeaza(b, a, p->elt);
                            qN->succ=sNumere;
                            sNumere=qN;
                            p=p->succ;
                        }
                        q=new nodChar;
                        q->elt=exp[i];
                        q->succ=p;
                        p=q;
                    }
}

int main()
{
    exp[0]='(';
    f.getline(exp+1, NMAX);
    exp[strlen(exp+1)+1]=')';
    exp[strlen(exp+1)+2]='\0';

    sNumere=NULL; sOperatori=NULL;
    for (int i=0; exp[i]!='\0'; ++i)
        if (exp[i]>='0' && exp[i]<='9') adaugaNumere(sNumere, i);
        else adaugaOperatori(sOperatori, i);

    g<<sNumere->numar<<"\n";

    f.close();
    g.close();
    return 0;
}