Pagini recente » Cod sursa (job #2664980) | Cod sursa (job #1029488) | Cod sursa (job #2689856) | Cod sursa (job #720752) | Cod sursa (job #1820142)
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
ifstream h("evaluare.in");
ofstream g("evaluare.out");
char p[10005];
int j=0,n; //pozitia curenta din sir;
struct nod{
int info;
char type;
nod* left;
nod* right;
};
nod* e();
nod* t();
nod* f();
int eval(nod* p){
int a,b;
if(p->type==' ')
return p->info;
else{
a=eval(p->left);
b=eval(p->right);
switch(p->type){
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return a/b;
}
}
}
void error(int j);
int main(){
nod* R;
h.getline(p,10004);
n=strlen(p);
R=e();
if(j<n)
error(j);
else
g<<eval(R)<<' ';
return 0;
}
nod* e(){
nod* temp1,*temp2;
nod* rez;
temp1=t();
if(p[j]=='+'){
j++;
temp2=e();
rez=new nod;
rez->type='+';
rez->left=temp1;
rez->right=temp2;
}
else if(p[j]=='-'){
j++;
temp2=e();
rez=new nod;
rez->type='-';
rez->left=temp1;
rez->right=temp2;
}
else
rez=temp1;
return rez;
}
nod* t(){
nod* temp1,*temp2;
nod* rez;
temp1=f();
if(p[j]=='*'){
j++;
temp2=t();
rez=new nod;
rez->type='*';
rez->left=temp1;
rez->right=temp2;
}
else if(p[j]=='/'){
j++;
temp2=t();
rez=new nod;
rez->type='/';
rez->left=temp1;
rez->right=temp2;
}
else
rez=temp1;
return rez;
}
void error(int j){
g<<"eroare la caracterul "<<j;
}
nod* f(){
nod *temp1,*rez;
if(p[j]=='('){
j++;
temp1=e();
if(p[j]!=')')
error(j);
j++;
rez=temp1;
}
else if(isdigit(p[j])){
int aux=0;
while(j<=n&&isdigit(p[j])){
aux=aux*10+p[j]-'0';
j++;
}
rez=new nod;
rez->type=' ';
rez->info=aux;
rez->left=NULL;
rez->right=NULL;
}
else
error(j);
return rez;
}