Pagini recente » Cod sursa (job #1408524) | Cod sursa (job #894027) | Cod sursa (job #769613) | Cod sursa (job #1117428) | Cod sursa (job #713569)
Cod sursa(job #713569)
#include<fstream>
#include<string>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
char c;
long numar();
long prod();
long expr(){
long rez = prod();
while(c == '+'|| c == '-'){
if(c == '+'){
f>>c;
rez += prod();
}
if(c == '-'){
f >> c;
rez -= prod();
}
}
return rez;
}
long prod(){
long rez = numar();
while(c == '*'|| c == '/'){
if(c == '*'){
f >> c;
rez *= numar();
}
if(c == '/'){
f >> c;
rez /= numar();
}
}
return rez;
}
long numar(){
long nr = 0;
if(c == '('){
f>>c;
nr = expr();
f>>c;
}else{
while(c>'0'&&c<'9'){
nr = nr*10+c-'0';
f>>c;
}
}
return nr;
}
int main(){
f>>c;
g<<expr();
return 0;
}