电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

c语言计算器源代码

9页
  • 卖家[上传人]:hs****ma
  • 文档编号:508954311
  • 上传时间:2023-03-21
  • 文档格式:DOCX
  • 文档大小:27.03KB
  • / 9 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、# include# include# include# definemaxsize100typedefdoubledatatype1;typedefchardatatype2;typedefstructstack1datatype1data1maxsize;inttop1;/*栈顶元素*/seqstack1,*pseqstack1;/*顺序栈*/typedefstructstack2datatype2data2maxsize;inttop2;/*栈顶元素*/seqstack2,*pseqstack2;/*顺序栈*/*栈的初始化*/pseqstack1init_seqstack1(void)pseqstack1S;S=(pseqstack1)malloc(sizeof(pseqstack1);if(S)S-top1=-1;returnS;pseqstack2init_seqstack2(void)pseqstack2S;S=(pseqstack2)malloc(sizeof(pseqstack2);if(S)S-top2=-1;returnS;/*判断栈空*/intempty_seqs

      2、tack1(pseqstack1S)if(S-top1=-1)return1;intempty_seqstack2(pseqstack2S)if(S-top2=-1)return1;elsereturn0;/*X入栈*/intpush_seqstack1(pseqstack1S,datatype1X)if(S-top1=maxsize-1)printf(栈满,无法入栈!n);return0;elseS-top1+;S-data1S-top1=X;return1;intpush_seqstack2(pseqstack2S,datatype2X)if(S-top2=maxsize-1)printf(栈满,无法入栈!n);return0;elseS-top2+;S-data2S-top2=X;return1;/*X出栈*/intpop_seqstack1(pseqstack1S,datatype1*X)if(empty_seqstack1(S)return0;else*X=S-data1S-top1;S-top1-;return1;intpop_seqstack2(pseqstack2S,d

      3、atatype2*X)if(empty_seqstack2(S)return0;else*X=S-data2S-top2;S-top2-;return1;/*求栈顶元素*/intgettop_seqstack1(pseqstack1S,datatype1*X)if(empty_seqstack1(S)return0;else*X=S-data1S-top1;return1;intgettop_seqstack2(pseqstack2S,datatype2*X)if(empty_seqstack2(S)return0;else*X=S-data2S-top2;return1;/*判断字符是否为操作数。若是返回1,否则返回0*/intisnum(charc)if(c=0&cA*/S=init_seqstack1();/*初始化栈*/while(ch!=#)/*遇到元素!=#时*/if(isnum(ch)/*判断ch是否为数字字符operand=operand*10+(ch-0);else/*否则*/a*/b*/*/,计算出操作数*/if(operand)push_seqstack1(S,o

      4、perand);/*当前字符不是数字,操作数结束,要入栈*/operand=0;if(ch!=&ch!=)pop_seqstack1(S,&b);/*运算符ch后的操作数出栈存入b*/pop_seqstack1(S,&a);/*运算符ch前的操作数出栈存入a*/switch(ch)/*求achb=?,将结果赋给c*/case+:c=a+b;break;case-:c=a-b;break;casec=a*b;break;case/:if(b!=0)c=a/b;*/w 不等于 # 时循printf(分母为零!);push_seqstack1(S,c);/*将c压入栈中*/ch=*A+;/*指针向下移动一位*/*遇到#循环结束*/gettop_seqstack1(S,&result);/*此时栈顶元素即为计算结果result*/returnresult;/*优先级判断函数*/intpriority(charop)switch(op)case#:return1;case):return2;case+:case-:return3;case*:case/:return4;case(:return5

      5、;default:return0;/*将指针infixexp指向的中缀表达式转换为指针postfixexp指向的后缀表达式intinfix_exp_value(char*infixexp,char*postfixexp)pseqstack2S;/*定义栈S*/intcount=0;charw;/*存放读取到的表达式(infixexp)的字符*/charc;/*存放栈顶元素*/chartopelement;/*存出栈元素*/S=init_seqstack2();/*初始化栈*/if(!S)/*栈的初始化判断*/printf(栈初始化失败!);return0;push_seqstack2(S,#);/*将结束符#加入运算符栈S中*/w=*infixexp;/*读表达式字符=w*/while(gettop_seqstack2(S,&c),c)!=#|w!=#)/*栈顶元素不等于#或环*/if(isnum(w)/*判断w是否为操作数,若是直接输出,读下一个字符二w,转*/if(count)*postfixexp=;postfixexp+;count=0;*postfixexp=w;postfi

      6、xexp+;w=*(+infixexp);else/*w若是运算符分类如下*/count=1;if(gettop_seqstack2(S,&c),c)=(&w=)/*如果栈顶为(并且w为)则(出栈不输出,读下一个字符二w,转*/pop_seqstack2(S,&topelement);/*将(出栈存入topelement*/w=*(+infixexp);elseif(gettop_seqstack2(S,&c),c)=(|priority(gettop_seqstack2(S,&c),c)w,转*/push_seqstack2(S,w);w=*(+infixexp);else/*否则*/*从运算符栈中出栈并输出,转*/pop_seqstack2(S,&topelement);*postfixexp=topelement;postfixexp+;*postfixexp=#;/*在指针postfixexp指向的后缀表达式结尾追加字符#*/*(+postfixexp)=0;/*在指针postfixexp指向的后缀表达式最后追加结束符0*/return1;/*主函数*/intmain()inti=0;charAmaxsize;charBmaxsize;printf(”请输入表达式,如:20+13#,必须以#结尾!n);/*1+2*(9+7)-4/2#23+(12*3-2)/4+34*5/7)+108/9#*/Ai=getchar();while(Ai+!=#)Ai=getchar();Ai=0;infix_exp_value(A,B);printf(A=%sn,A);printf(B=%sn,B);printf(上式的结果为:);printf(%gn,postfix_exp(B);return0;getch();

      《c语言计算器源代码》由会员hs****ma分享,可在线阅读,更多相关《c语言计算器源代码》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    监控施工 信息化课堂中的合作学习结业作业七年级语文 发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.