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.