Cod sursa(job #1921609)

Utilizator vladm98Munteanu Vlad vladm98 Data 10 martie 2017 13:28:25
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;
int where;
char s[100009];
int functie3();
int functie2();
int functie1 ()
{
    int raspuns = functie2();
    while (s[where] == '+' || s[where] == '-')
    {
        if (s[where] == '+')
        {
            ++where;
            raspuns += functie2();
        }
        else
        {
            ++where;
            raspuns -= functie2();
        }
    }
    return raspuns;
}
int functie2 ()
{
    int raspuns = functie3();
    while (s[where] == '*' || s[where] == '/')
    {
        if (s[where] == '*')
        {
            ++where;
            raspuns *= functie3();
        }
        else
        {
            ++where;
            raspuns /= functie3();
        }
    }
    return raspuns;
}
int functie3 ()
{
    int raspuns = 0;
    if (s[where] == '(')
    {
        ++where;
        raspuns = functie1();
        ++where;
    }
    else
    {
        while (s[where]>='0' && s[where]<='9')
        {
            raspuns = raspuns*10+(s[where]-'0');
            ++where;
        }
    }
    return raspuns;
}
int main()
{
    ifstream fin ("evaluare.in");
    ofstream fout ("evaluare.out");
    fin.getline(s, 100009);
    fout << functie1();
    return 0;
}