Delphi计算器控制台版(含括号)
来源:互联网 发布:华为大数据研发岗累吗 编辑:程序博客网 时间:2024/06/08 16:20
program Project1;
{$APPTYPE CONSOLE}{$R *.res}uses System.SysUtils;procedure translate(str:Array of char;var exp:array of char);type StrStruct =record data:Array[0..99]of char; top:integer; end; var ch:char; i,t:integer; op:StrStruct; begin i:=0; t:=0; op.top:=-1; ch:=str[i]; i:=i+1; while(ch<> '') do begin Case ch of '(':begin op.top:=op.top+1; op.data[op.top]:=ch; end; ')':begin while(op.data[op.top] <> '(') do begin exp[t]:=op.data[op.top]; op.top:=op.top-1; t:=t+1; end; op.top:=op.top-1; end; '+','-':begin while(op.top <> -1)and(op.data[op.top] <> '(') do begin exp[t] := op.data[op.top]; op.top:=op.top-1; t:=t+1; end; op.top:=op.top+1; op.data[op.top] := ch; end; '*','/':begin while(op.data[op.top] = '/') or (op.data[op.top] = '*') do begin exp[t] := op.data[op.top]; op.top:=op.top-1; t:=t+1; end; op.top:=op.top+1; op.data[op.top] := ch; end; ' ':; else begin while(ch >= '0')and(ch <= '9') do begin exp[t] := ch; t:=t+1; ch := str[i]; i:=i+1; end; i:=i-1; exp[t] := '#'; t:=t+1; end; end; ch := str[i]; i:=i+1; end; while(op.top <> -1) do begin exp[t] := op.data[op.top]; t:=t+1; op.top:=op.top-1; end; exp[t] := #0; end;function cal_value(exp:Array of char):real;type FloatStruct =record data:Array[0..99] of real; top:integer; end;var st:FloatStruct; d:real; ch:char; t:integer;begin t:=0; st.top := -1; ch := exp[t]; t:=t+1; while(ch <> '') do begin case ch of '+':begin st.data[st.top-1] := st.data[st.top-1]+st.data[st.top]; st.top:=st.top-1; end; '-':begin st.data[st.top-1] := st.data[st.top-1]-st.data[st.top]; st.top:=st.top-1; end; '*':begin st.data[st.top-1] := st.data[st.top-1]*st.data[st.top]; st.top:=st.top-1; end; '/': begin if(st.data[st.top] <> 0) then st.data[st.top-1]:=st.data[st.top-1]/st.data[st.top] else begin write('除数不能为0!'); exit; end; st.top:=st.top-1; end; else begin d:=0; while(ch >= '0')and(ch <= '9') do begin d := d*10+ord(ch)-48; ch := exp[t]; t:=t+1; end; st.top:=st.top+1; st.data[st.top] := d; end; end; ch := exp[t]; t:=t+1; end; cal_value:=st.data[st.top];end;var str,exp:array[0..99] of char;begin write('表达式:'); readln(str); translate(str,exp); write('计算结果:',format('%g',[cal_value(exp)])); readln;end.
阅读全文
0 0
- Delphi计算器控制台版(含括号)
- C语言编辑计算器(含括号)
- C++版控制台计算器
- 简单计算器(不支持括号)
- 计算器(delphi)
- 计算器(能识别括号)代码
- "计算器算法"(含括号匹配,小数点)-之高效,通俗易懂详细解析.
- 简单四则运算计算器的C++实现(含括号和+-*/的优先级判断)
- 控制台计算器(类,枚举,继承)
- 无括号的计算器(int)和有括号的计算器(int)
- 四则运算,含括号
- 含括号的四则运算
- 括号匹配(delphi)-转
- 简易Java控制台计算器
- console calc 控制台 计算器
- 控制台的四则运算计算器
- C# 控制台应用程序 计算器
- 简易计算器(3)- 扩展(支持括号和负数)
- nodejs入门
- android rename to(file)方法失败
- DSP TMS320C6000学习笔记
- 如何用自己的电脑作为服务器搭一个网站
- C++ vector拷贝使用总结
- Delphi计算器控制台版(含括号)
- Android7.0上PopupWindow的showAsDropDown位置问题
- C++里vector::erase函数
- JDBC连接数据库学习心得
- sklearn-例程--Plotting Cross-Validated Predictions
- redhat6.3安装Keepalived+LVS
- LeetCode之路:237. Delete Node in a Linked List
- java多线程总结(3)
- 马踏棋盘问题