Cod sursa(job #2310535)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 1 ianuarie 2019 07:57:39
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>

#include <fstream>



using namespace std;



ifstream f("evaluare.in");

ofstream g("evaluare.out");



int i=0;

char s[100005];

int add();

int timesby();

int exp();



int add()

{

    int rez=timesby();

    while(s[i]=='+' || s[i]=='-')

     {

         if(s[i]=='+')

             {

                 i++;

                 rez+=timesby();

             }

           else

             {

                 i++;

                 rez-=timesby();

             }

     }

     return rez;

}



int timesby()

{

    int rez=exp();

    while(s[i]=='*' || s[i]=='/')

    {

        if(s[i]=='*')

        {

            i++;

            rez=rez*exp();

        }

        else

        {

            i++;

            rez=rez/exp();

        }

    }

    return rez;

}



int exp()

{

     int rez=0;

    if(s[i]=='(')

    {

        i++;

        rez=add();

        i++;

    }

    else

    {

        while(s[i]>='0' && s[i]<='9')

        {

            rez=rez*10+(s[i]-'0');

            i++;

        }

    }

    return rez;

}



int main()

{

    f >> s;

    g << add();

    return 0;

}