Cod sursa(job #2305517)

Utilizator driver71528@gmail.comTerec Andrei-Sorin [email protected] Data 20 decembrie 2018 14:15:42
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream f("evaluare.in");
ofstream g("evaluare.out");
char s[100001],*p=s;
char op[2][3]={"+-","*/"};
int eval(int a,char o,int b)
{
    switch(o)
    {
        case '+': return a+b;
        case '-': return a-b;
        case '*': return a*b;
        case '/': return a/b;
    }
    return -1;
}

int expr(int lev=0)
{
    int x;
    if(lev==2)
        if(*p=='(')
            p++,x=expr(0),p++;
        else
            for(x=0;isdigit(*p);p++)
                x=x*10+*p-'0';
    else
        for(x=expr(lev+1);strchr(op[lev],*p)&& *p!=NULL;)
        {
            char s=*p++;
            x=eval(x,s,expr(lev+1));
        }
    return x;
}


int main()
{
    f>>s;
    f.close();
    g<<expr();
    g.close();
    return 0;
}