Cod sursa(job #1334826)

Utilizator IonMosnoiIon Mosnoi IonMosnoi Data 4 februarie 2015 18:22:29
Problema Evaluarea unei expresii Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.67 kb
#include<fstream>
using namespace std;
ifstream f("evaluare.in");
ofstream o("evaluare.out");
char in[100100],po[100100],co[100010];
int c[100000];
int number(char *i){
if(*i>='0'&&*i<='9'){
    return (*i-'0')*(*(1+i)>='0'&& *(1+i)<='9'?10:1)+number(i+1);
}else return 0;
}
int main(){
f>>in;
char *i = &in[0],*p = &po[0];
int q = 0;
while(*i){
    if(*i==' ')i++;
    if(*i>='0'&&*i<='9'){
        while(*i>='0'&&*i<='9'){
            *p=*i;
            p++;i++;
        }
        *p=' ';
        p++;
    }
    if(*i=='('){co[++q]=*i;i++;}
    if(*i==')'){
        while(co[q]!='('){
                *p=co[q--];
                p++;
              }
        q--;
        i++;
    }
    if(*i=='*'||*i=='/'){
         /*   while(co[q]=='/'||co[q]=='*'){
                *p=co[q--];
                p++;
            }*/
            co[++q]=*i;
            i++;
    }
    if(*i=='+'||*i=='-'){
            while(co[q]=='/'||co[q]=='*'||co[q]=='+'||co[q]=='-'){
                *p=co[q--];
                p++;
            }
            co[++q]=*i;i++;
    }
}
while(q){
 *p=co[q--];
  p++;
}
*p='\0';
//o<<po<<endl;
p=&po[0];
q=0;
while(*p!='\0'){
    if(*p==' ')p++;
    if(*p>='0'&&*p<='9'){
        c[++q]=number(p);
    //    o<<c[q]<<endl;
        while(*p>='0'&&*p<='9')p++;
    }
    if(*p=='+'){
        c[--q]+=c[q+1];
    //      o<<c[q]<<endl;
        p++;
    }
    if(*p=='-'){
        c[--q]-=c[q+1];
        p++;
    }
    if(*p=='*'){
        c[--q]*=c[q+1];
       //   o<<c[q]<<endl;
        p++;
    }
    if(*p=='/'){
        c[--q]/=c[q+1];
      //    o<<c[q]<<endl;
        p++;
    }
}
o<<c[1];
}