Cod sursa(job #2256300)

Utilizator MDiana15Diana M MDiana15 Data 8 octombrie 2018 14:49:05
Problema Evaluarea unei expresii Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int i,n;
char s[100005];

int termen();
int expresie();
int factor();


int termen()
{
    int t=factor();
    while(i<n&&s[i]=='+')
    {
        i++;
        t=t+factor();
    }
    while(i<n&&s[i]=='-')
    {
        i++;
        t=t-factor();
    }
    return t;
}
int factor()
{
    int t=expresie();
    while(i<n&&s[i]=='*')
    {
        i++;
        t=t*expresie();
    }
    while(i<n&&s[i]=='/')
    {
        i++;
        t=t/expresie();
    }
    return t;
}
int expresie()
{
    int t;
    if(s[i]=='(')
    {
        i++;
        t=termen();
        i++;
    }
    else {int nr=0;
          while(isdigit(s[i]))
                {
                    nr=nr*10+(s[i]-'0');
                    i++;
                }
        t=nr;}
        return t;
}
int main()
{
    f.getline(s,100005);
    n=strlen(s);
    i=0;
    g<<termen();
    return 0;
}