收藏 分享(赏)

Pascal表达式求值.doc

上传人:11xg27ws 文档编号:7860403 上传时间:2019-05-27 格式:DOC 页数:5 大小:34.50KB
下载 相关 举报
Pascal表达式求值.doc_第1页
第1页 / 共5页
Pascal表达式求值.doc_第2页
第2页 / 共5页
Pascal表达式求值.doc_第3页
第3页 / 共5页
Pascal表达式求值.doc_第4页
第4页 / 共5页
Pascal表达式求值.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、Pascal 表达式求值 :负数能够参与运算。其中负数(-m)的解决办法是通过在数字栈中构建 0-m 方式,把单目的负数符号变成减法运算(负数主要出现在表达式的开头以及“(”后面,程序默认输入的表达式格式正确。program express;var s:string; /输入字符串fuhao:array1100 of char; /符号栈shuzi:array1100 of longint; /数字栈fh_top,sz_top:integer; /栈指针i,j,len,k:longint;c:char;function jibie(c:char):integer; /定义运算符的优先级begi

2、ncase c of(:exit(0);+,-:exit(1);*,/:exit(2);:exit(3);end;end;function jisuan(x,y:longint;c:char):longint; /计算函数var r,t:longint;begincase c of+:exit(x+y);-:exit(x-y);*:exit(x*y);/:exit(x div y);:begint:=1;for r:=1 to y dot:=t*x;end;end;jisuan:=t;end;beginreadln(s);fh_top:=0;sz_top:=0;len:=length(s);i

3、:=1;if si=- then /判断表达式第一个数是否是负数begininc(sz_top);shuzisz_top:=0;end;while i0)and(jibie(si)( dobegink:=jisuan(shuzisz_top-1,shuzisz_top,fuhaofh_top);dec(fh_top);dec(sz_top);shuzisz_top:=k;end;dec(fh_top);end;if si in09 thenbegink:=ord(si)-48;inc(i);while si in 09 dobegink:=k*10+ord(si)-48;inc(i);end;inc(sz_top);shuzisz_top:=k;dec(i);end;inc(i);end;while fh_top0 do /符号栈中的元素全部运算,并弹栈begink:=jisuan(shuzisz_top-1,shuzisz_top,fuhaofh_top);dec(fh_top);dec(sz_top);shuzisz_top:=k;end;writeln(shuzisz_top); /数字栈底元素为结果end.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报