收藏 分销(赏)

C语言编写四则运算.doc

上传人:二*** 文档编号:4484798 上传时间:2024-09-24 格式:DOC 页数:15 大小:45KB
下载 相关 举报
C语言编写四则运算.doc_第1页
第1页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、. .C语言编写四那么运算#include/计算器#include#defineSTACK_SIZE100/max size of the stack#defineSTACK_INCREMENT10/realloc sizetypedefstructFStack/stack of intfloat* base;float* top;intstacksize;FStack;voidInit(FStack* s)s-base = (float*)malloc(STACK_SIZE * sizeof(FStack);if (!s-base)printf(overflow!n);return;s-to

2、p = s-base;s-stacksize = STACK_SIZE;boolisEmpty(FStack* s)if (s-top = s-base)returntrue;elsereturnfalse;voidPush(FStack* s, floate)if (s-top - s-base = s-stacksize)printf(stack is full!nrealloc %dn, STACK_INCREMENT);s-base = (float*)realloc(s-base, (s-stacksize + sizeof(FStack);if (!s-base)printf(ov

3、erflow!n);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(stack is empty!n);return 0;floate = *(s-top - 1);returne;voidPop(FStack* s)if (s-top = s-base)printf(stack is empty!n);return;s-top-;typedefstructCStack/

4、stack of charchar* base;char* top;intstacksize;CStack;voidInit(CStack* s)s-base = (char*)malloc(STACK_SIZE * sizeof(CStack);if (!s-base)printf(overflow!n);return;s-top = s-base;s-stacksize = STACK_SIZE;boolisEmpty(CStack* s)if (s-top = s-base)returntrue;elsereturnfalse;voidPush(CStack* s, inte)if (s

5、-top - s-base = s-stacksize)printf(stack is full!nrealloc %dn, STACK_INCREMENT);s-base = (char*)realloc(s-base, (s-stacksize + sizeof(CStack);if (!s-base)printf(overflow!n);return;s-top = s-base + s-stacksize;s-stacksize += STACK_INCREMENT;*(s-top) = e;(s-top)+;charGetTop(CStack* s)if (s-top = s-bas

6、e)printf(stack is empty!n);return 0;chare = *(s-top - 1);returne;voidPop(CStack* s)if (s-top = s-base)printf(stack is empty!n);return;s-top-;boolisOper(charch)if (ch = + | ch = - | ch = * | ch = / | ch = %)returntrue;elsereturnfalse;intPriority(charch)intp;switch(ch)case(:p = 0;break;case+:case-:p =

7、 1;break;case*:case/:case%:p = 2;break;returnp;floatCalculate(floatf1, floatf2, charoper)floatf3;switch(oper)case+:f3 = f1 + f2;break;case-:f3 = f1 - f2;break;case*:f3 = f1 * f2;break;case%:f3 = (float)(int)f1 % (int)f2);break;case/:if (f2 = 0)printf(nDevided by zero!);exit(1);elsef3 = f1 / f2;break

8、;returnf3;floatStrtoFloat(char* str, int* pos)floatfRes;inti = *pos;intk;charn50;for (k = 0; stri = 0 & stri = 9 | stri = .; i+, k+)nk = stri;nk = 0;*pos = i;fRes = atof(n);returnfRes;boolCheck(char* str)inti = 0;while (stri != 0)if (stri != + & stri != - & stri != * & stri != / & stri != % & stri !

9、= . & stri != ( & stri != ) & (stri 9)returnfalse;i+;returntrue;voidmain()charexp100;inti;floatf, f1, f2;charoper;FStackfstack;CStackcstack;Init(&fstack);Init(&cstack);printf(The expression is:);gets(exp);if (!Check(exp)printf(input error! exit now!n);exit(1);for (i = 0; expi != 0 & expi != -52; i+)

10、if (!isOper(expi)f = StrtoFloat(exp, &i);Push(&fstack, f);if (isOper(expi)if (!isEmpty(&cstack)while (!isEmpty(&cstack) & Priority(expi) = Priority(GetTop(&cstack)oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(

11、&fstack, f);Push(&cstack, expi);elsePush(&cstack, expi);elseif (expi = ()Push(&cstack, expi);elseif (expi = )while (GetTop(&cstack) != ( & !isEmpty(&cstack)oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);Pop(&cstack);while (!isEmpty(&cstack)oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);printf(nThe result is:%fn, GetTop(&fstack);Pop(&fstack);. .word.

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 教育专区 > 初中其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服