Cod sursa(job #1468735)

Utilizator lflorin29Florin Laiu lflorin29 Data 6 august 2015 20:52:18
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;

ifstream fin ("evaluare.in");
ofstream fout ("evaluare.out");

string s;
int thispos;
int termen(), factor();

int make_number ( ) {
    int now = 0;
    for (; isdigit(s[thispos]); ++thispos)
       now = 10 * now + s[thispos] - '0';
    return now;
}


int sigma ( ) {
    int now = termen();
    while (s[thispos] == '+' || s[thispos] == '-'){
            if (s[thispos] == '+')
               ++thispos, now += termen();
            else ++thispos, now -= termen();
    }
    return now;
}

int termen() {
    int now = factor();
    while (s[thispos] == '*' || s[thispos ] == '/'){
           if (s[thispos] == '*')
              ++thispos, now *= factor();
            else ++thispos, now /= factor();
    }
    return now;
}

int factor(){
    int toret;
    if (s[thispos] == '(') {
      ++thispos;
      toret = sigma();
      ++thispos;
    }
    else toret = make_number();
    return toret ;
}

int main(){
    fin >> s;
    fout << sigma() << "\n";
}