资源描述:
《堆栈四则运算》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#include#defineTRUE1#defineFALSE0#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT20/************************************************************************/typedefstruct{int*pbase;int*ptop;intstacksize;}stack;/************************
2、************************************************/charoperator[7]="+-*/()!";charoptr;intopnd=-1;intresult;charprioritytable[7][7]={{'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},{'>','>','>','>','<','>','>'},{'>','>','>','>','<','>','>'},{'<','<','<','<','<','=','o'},{'>','>','>','>
3、','o','>','>'},{'<','<','<','<','<','o','='},};/************************************************************************///构造一个空栈stackinitstack(){stacks;s.pbase=(int*)malloc(STACK_INIT_SIZE*sizeof(int));if(!s.pbase){printf("分配失败");exit(-1);}else{s.ptop=s.pbase;s.stacksize=STACK_INIT_SIZE;}retur
4、ns;}/************************************************************************///销毁一个栈voiddestorystack(stack*s){if(s->pbase){free(s->pbase);s->ptop=s->pbase=NULL;}}/************************************************************************///栈不空,则返回栈顶intgettop(stacks){return*(s.ptop-1);}/*********
5、***************************************************************///压栈intpushstack(stack*s,inte){if(s->ptop-s->pbase==STACK_INIT_SIZE){s->pbase=(int*)realloc(s->pbase,(s->stacksize+STACKINCREMENT)*sizeof(int));if(!s->pbase){return0;}s->ptop=s->pbase+s->stacksize;s->stacksize=s->stacksize+STACKINC
6、REMENT;}*(s->ptop++)=e;return1;}/************************************************************************/intpop(stack*s,int*e){if(s->ptop==s->pbase){return0;}*e=*--(s->ptop);return1;}/************************************************************************///比较两个字符的优先级charcheckchar(charoperato
7、r_1,charoperator_2){inti,j;i=strchr(operator,operator_1)-operator;j=strchr(operator,operator_2)-operator;returnprioritytable[i][j];}/************************************************************************///判断是否为操作数isoperator(cha