#include<stdio.h> /*******************************************************************************/ #define M 300 char express[M]; char string[M]; int len=0; /*******************************************************************************/ int express_calc(char *express,int len); /*******************************************************************************/ typedef struct{ char cstore[M]; int ctop; }Stack_ct; void stack_cinit( Stack_ct * A) { (*A).ctop=-1; } int stack_cpush(Stack_ct * A,char val) { if( (*A).ctop==M-1 )// stack full, cannot push return (-1); (*A).cstore[ ++((*A).ctop) ]=val; } int stack_cpop(Stack_ct *A,char *val) { if( (*A).ctop==-1 )// stack empty, cannot pop return (-1); *val=(*A).cstore[ ((*A).ctop)-- ]; } Stack_ct token_store; /*******************************************************************************/ typedef struct{ int istore[M]; int itop; }Stack_it; void stack_iinit( Stack_it *A) { (*A).itop=-1; } int stack_ipush(Stack_it * A,int val) { if( (*A).itop==M-1 )// stack full, cannot push return (-1); (*A).istore[ ++((*A).itop) ]=val; } int stack_ipop(Stack_it *A,int *val) { if( (*A).itop==-1 )// stack empty, cannot pop return (-1); *val=(*A).istore[ ((*A).itop)-- ]; } Stack_it intstore; /*******************************************************************************/ int comp_pri_before(char x, char y) { char token[5]={'+','-','*','/','('}; int before_pri[5]={1,1,2,2,3}; int i=0,a=0,b=0; for(i=0;i<5;i++) { if(x==token[i]) a=i; if(y==token[i]) b=i; } if( before_pri[a] > before_pri[b]) return (1); else return (0); } /* int comp_pri_after(char x, char y) { char token[5]={'+','-','*','/','('}; int after_pri[5]={1,1,2,2,0}; int i=0,a=0,b=0; for(i=0;i<5;i++) { if(x==token[i]) a=i; if(y==token[i]) b=i; } if( after_pri[a] > after_pri[b]) return (1); else return (0); } */ /*******************************************************************************/ int calc(int a, int b, char c) { switch(c) { case '+': return (a+b); case '-': return (a-b); case '*': return (a*b); case '/': return (a/b); default: ;//return NULL; } } /*******************************************************************************/ int main(void) { int test_case; int T=0,th=1; freopen("input.txt", "r", stdin); setbuf(stdout, NULL); //scanf("%d", &T); T=10; for(test_case = 0; test_case < T; test_case++) { int ret=0; /********************************** * Implement your algorithm here. */ scanf("%d",&len); scanf("%s",express); ret=express_calc(express,len); /**********************************/ // Print the answer to standard output(screen). printf("#%d %d\n",th++,ret); fflush(stdout);//修复Eclipse printf()不能显示的小bug } return (0);//Your program should return 0 on normal termination. } int express_calc(char *express,int len) { int i=0,j=0,k=0,sum=0,a=0,b=0; char tmp; char stoken[14]={'0','1','2','3','4','5','6','7','8','9','+','-','*','/'}; for(i=0;i<len;i++) string[i]='\0'; stack_cinit( &token_store); stack_iinit( &intstore); for(i=0;i<len;i++) { if( express[i]>=48 && express[i]<=57)// number string[k++]=express[i]; else//char { if(express[i]==')') { tmp=token_store.cstore[token_store.ctop]; while(tmp !='(') { stack_cpop(&token_store,&string[k++]); tmp=token_store.cstore[token_store.ctop]; } stack_cpop(&token_store,&tmp); continue; } if( token_store.ctop==-1) stack_cpush(&token_store,express[i]); else { tmp=token_store.cstore[token_store.ctop]; if(tmp=='(')///////////////////////////////////////lost { stack_cpush(&token_store,express[i]); continue; } if( comp_pri_before(express[i],tmp) ) stack_cpush(&token_store,express[i]); else { tmp=token_store.cstore[token_store.ctop]; while( ! comp_pri_before(express[i],tmp) ) { if(token_store.ctop==-1 || tmp=='(') break; stack_cpop(&token_store,&string[k++]); tmp=token_store.cstore[token_store.ctop]; } stack_cpush(&token_store,express[i]); } } } } while(token_store.ctop!=-1) { stack_cpop(&token_store,&string[k++]); } ///////////////////////////////////////////////////////////////////////////////////// i=0; for(i=0;i<len;i++) { for(j=0;j<14;j++) { if( string[i] ==stoken[j] ) break; } if(j==14) break; if( string[i]>=48 && string[i]<=57 ) stack_ipush(&intstore,string[i]-48); else{ stack_ipop(&intstore,&a); stack_ipop(&intstore,&b); sum=calc(b,a,string[i]); stack_ipush(&intstore,sum); } } sum=intstore.istore[intstore.itop]; return (sum); }