欢迎来到天天文库
浏览记录
ID:55926103
大小:34.50 KB
页数:8页
时间:2020-06-15
《C语言编写四则运算.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C语言编写四则运算#include//计算器#include#defineSTACK_SIZE100//maxsizeofthestack#defineSTACK_INCREMENT10//reallocsizetypedefstructFStack//stackofint{float*base;float*top;intstacksize;}FStack;voidInit(FStack*s){s->base=(float*)malloc(STACK_SIZE*sizeof(FStack));if(!s->base){
2、printf("overflow!");return;}s->top=s->base;s->stacksize=STACK_SIZE;}boolisEmpty(FStack*s){if(s->top==s->base){returntrue;}else{returnfalse;}}voidPush(FStack*s,floate){if(s->top-s->base>=s->stacksize){printf("stackisfull!realloc%d",STACK_INCREMENT);s->base=(float*)realloc(s->
3、base,(s->stacksize+sizeof(FStack)));if(!s->base){printf("overflow!");return;}s->top=s->base+s->stacksize;s->stacksize+=STACK_INCREMENT;}*(s->top)=e;(s->top)++;}floatGetTop(FStack*s){if(s->top==s->base){printf("stackisempty!");return0;}floate=*(s->top-1);returne;}voidPop(FStack
4、*s){if(s->top==s->base){printf("stackisempty!");return;}s->top--;}typedefstructCStack//stackofchar{char*base;char*top;intstacksize;}CStack;voidInit(CStack*s){s->base=(char*)malloc(STACK_SIZE*sizeof(CStack));if(!s->base){printf("overflow!");return;}s->top=s->base;s->stacksize=S
5、TACK_SIZE;}boolisEmpty(CStack*s){if(s->top==s->base){returntrue;}else{returnfalse;}}voidPush(CStack*s,inte){if(s->top-s->base>=s->stacksize){printf("stackisfull!realloc%d",STACK_INCREMENT);s->base=(char*)realloc(s->base,(s->stacksize+sizeof(CStack)));if(!s->base){printf("overf
6、low!");return;}s->top=s->base+s->stacksize;s->stacksize+=STACK_INCREMENT;}*(s->top)=e;(s->top)++;}charGetTop(CStack*s){if(s->top==s->base){printf("stackisempty!");return0;}chare=*(s->top-1);returne;}voidPop(CStack*s){if(s->top==s->base){printf("stackisempty!");return;}s->top
7、--;}boolisOper(charch){if(ch=='+'
8、
9、ch=='-'
10、
11、ch=='*'
12、
13、ch=='/'
14、
15、ch=='%'){returntrue;}else{returnfalse;}}intPriority(charch){intp;switch(ch){case'(':p=0;break;case'+':case'-':p=1;break;case'*':case'/':case'%':p=2;break;}returnp;}floatCalculate(floatf1,floatf2,charoper){floatf3;switch
16、(oper){case'+':f3=f1+f2;break;cas
此文档下载收益归作者所有