xiao_baby xiao_baby
关注数: 62 粉丝数: 213 发帖数: 18,347 关注贴吧数: 26
这个程序怎么改动,可以成为能计算多位整数的程序? 这个程序只可以做1位整数的4则运算和括号内计算,怎么改动可以计算多位整数呢?#include #include int opnd_top=-1,optr_top=-1;double opnd_stack[40];char optr_stack[40];char optr[7]={'+', '-' ,'*' ,'/' ,'(' ,')' ,'#'};char priority[7][7]={ {'>','>','<','<','<','>','>'}, {'>','>','<','<','<','>','>'}, {'>','>','>','>','<','>','>'}, {'>','>','>','>','<','>','>'}, {'<','<','<','<','<','=',' '}, {'>','>','>','>',' ','>','>'}, {'<','<','<','<','<',' ','='}};double pop_nd(){ double e; e=opnd_stack[opnd_top--]; return e;}char pop_tr(){ char e; e=optr_stack[optr_top--]; return e;}void push_nd(double e){ opnd_stack[++opnd_top]=e;}void push_tr(char e){ optr_stack[++optr_top]=e;}double gettop_nd(){ return opnd_stack[opnd_top];}char gettop_tr(){ return optr_stack[optr_top];}int getIndex(char op){ int i; for(i=0;i<7;i++) if(optr[i]==op) return i; return -1;}char getprior(char optr1,char optr2){ int i,j; i=getIndex(optr1); j=getIndex(optr2); if(i==-1 || j==-1) return ' '; return priority[i][j];}double calculate(double a,double b ,char c){ double r=0; switch(c) { case '+': r=a+b; break; case '-': r=a-b; break; case '*': r=a*b; break; case '/': r=a/b; break; } return r;}void main(){ char c; push_tr('#'); printf("input a expression end with #\n"); c=getchar(); while(c!='#' || gettop_tr()!='#') { if(c>='0' && c<='9') { push_nd((int)(c-'0')); c=getchar(); } else { switch(getprior(gettop_tr(),c)){ case '<': push_tr(c); c=getchar(); break; case '=': pop_tr(); c=getchar(); break; case '>': push_nd( calculate(pop_nd(),pop_nd(),pop_tr()) ); break;} } } printf("the result is: %f\n",pop_nd());}
首页 1 2 下一页