Cod sursa(job #2862215)

Utilizator EduardSanduSandu Eduard Alexandru EduardSandu Data 5 martie 2022 00:23:57
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <fstream>
#include<cstring>
#include<algorithm>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char s[100002];
int index;

long long expresie();
long long produs();
long long suma();

long long expresie()
{
    if(isdigit(s[index]))
    {
        long long nr = 0;
        while(isdigit(s[index]))
        {
            nr = nr * 10 + (s[index] - '0');
            index++;
        }
        return nr;
    }
    else if(s[index] == '(')
    {
        index++;
        //o iau de la capat
        long long x = suma();
        index++;
        return x;
    }
}

long long produs()
{
    int temp = expresie();
    while(s[index] == '*' || s[index] == '/')
    {
        if(s[index] == '*')
        {
            index++;
            temp *= expresie();
        }
        else if(s[index] == '/')
        {
            index++;
            temp /= expresie();
        }
    }
    return temp;
}


long long suma()
{
    int temp = produs();
    while(s[index] == '+' || s[index] == '-')
    {
        if(s[index] == '+')
        {
            index++;
            temp += produs();
        }
        else if(s[index] == '-')
        {
            index++;
            temp -= produs();
        }
    }
    return temp;
}



int main()
{
    int n,i,j,k;
    fin.getline(s, 100002);
    fout<<suma();
    return 0;
}