Cod sursa(job #3142406)

Utilizator AlexandruDrg23Draghici Alexandru AlexandruDrg23 Data 21 iulie 2023 10:13:51
Problema Evaluarea unei expresii Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream fin ("evaluare.in");
ofstream fout ("evaluare.out");
int i;
char e[100000];
int a;
long long expresie();
long long termen();
long long factor();
long long nr();

long long nr()
{
    long long r=0;
    while(isalnum(e[i]) && !isalpha(e[i]))
    {
        r=r*10+e[i]-'0';
        i++;
    }
    return r;
}

long long expresie()
{
    long long r=termen();
    while(e[i]=='+' || e[i]=='-')
    {
        if(e[i]=='+')
        {
            i++;
            r+=termen();
        }
        else
        {
            i++;
            r-=termen();
        }
    }
    return r;
}

long long factor()
{
    long long r;
    if(e[i]=='(')
    {
        i++;
        r=expresie();
        i++;
    }
    else
        r=nr();
    return r;
}

long long termen()
{
    long long r=factor();
    while(e[i]=='*' || e[i]=='/')
    {
        if(e[i]=='*')
        {
            i++;
            r*=termen();
        }
        else
        {
            i++;
            r/=termen();
        }
    }
    return r;
}

int main()
{
    fin.getline(e,100000);
    ///int a;
    ///fin>>a;
    fout<<expresie();
    return 0;
}