Pagini recente » Cod sursa (job #159876) | Cod sursa (job #2131152) | Cod sursa (job #961613) | Cod sursa (job #1783057) | Cod sursa (job #2334912)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream in("evaluare.in");
ofstream out("evaluare.out");
string input;
int index;
char curent;
int factor();
int termen();
int expresie();
void moveon(){
curent = input[++index];
}
int factor(){
int r = 0;
if(curent == '('){
moveon();
r = expresie();
moveon();///trec peste )
}while(isdigit(curent)){
r = r * 10 + curent - '0';
moveon();
}
return r;
}
int termen(){
int r = factor();
while(curent == '*' or curent == '/'){
if(curent == '*'){
moveon();
r *= factor();
}else{
moveon();
r /= factor();
}
}
return r;
}
int expresie(){
int r = termen();
while(curent == '+' or curent == '-'){
if(curent == '+'){
moveon();
r += termen();
}else{
moveon();
r -= termen();
}
}
return r;
}
int main()
{
in>>input;
curent = input[index];
out<<expresie();
return 0;
}