📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
memo1: TMemo;
Button3: TButton;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Timer1: TTimer;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Timer1Timer(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const max=100;
var
Form1: TForm1;
flag:boolean;
number:array[0..max] of integer;
symbol:array[1..max] of char;
s,t,tr:string;
i,p,j,code,kk,count,mm,tt:integer;
implementation
{$R *.dfm}
procedure push;
begin
inc(count);
inc(p);symbol[p]:=s[i];
end;
procedure pop;
begin
inc(count);
dec(p);
case symbol[p+1] of
'+':inc(number[p],number[p+1]);
'-':dec(number[p],number[p+1]);
'*':number[p]:=number[p]*number[p+1];
'/':
begin
if number[p+1]=0 then
begin
showmessage('您的表达式存在除数为0的情况,请确认后重输!');
flag:=false;
exit;
end;
number[p]:=number[p] div number[p+1];
end;
'%':
begin
if number[p+1]=0 then
begin
showmessage('您的表达式存在模数为0的情况,请确认后重输!');
flag:=false;
exit;
end;
number[p]:=number[p] mod number[p+1];
end;
'^': begin
tt:=number[p];
if number[p+1]=0 then
begin
number[p]:=1;
exit
end;
if number[p+1]<0 then
begin
showmessage('本人能力,无法显示小数,指数请为正整数,谢谢!');
flag:=false;
exit;
end;
for kk:=1 to number[p+1]-1 do
number[p]:=tt*number[p];
end;
end;
end;
function can:boolean;
begin
can:=true;
if (s[i] in ['+','-']) and (symbol[p]<>'(') then exit;
if (s[i] in ['*','/']) and (symbol[p] in ['^','%']) then exit;
if (s[i] in ['^','%']) and (symbol[p] in ['^']) then exit;
if (s[i] in ['%']) and (symbol[p] in ['%']) then exit;
can:=false;
end;
function isRight(tr:string):boolean;
var a:array [1..max] of char;
pp:integer;
begin
case tr[1] of
'+','-','*','/','^','%',')':
begin
isRight:=false;
showmessage('第一个字符不能为运算符,请确认后重新输入!');
exit;
end;
end;
for pp:=1 to length(tr)-1 do
begin
case tr[pp] of
'+','-','*','/','^','%':
begin
case
tr[pp+1] of
'+','-','*','/','^','%':
begin
isRight:=false;
showmessage('符号不能叠加,请确认后重新输入!');
exit
end;
end;
end;
end;
end;
isRight:=true;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
form1.Close;
end;
procedure TForm1.Button1Click(Sender: TObject);
var error:integer;
begin
flag:=true;
button2.Visible:=true;
label2.Visible:=true;
edit2.Visible:=true;
listbox1.Visible:=true;
listbox1.Clear;
s:=edit1.Text ;
//edit1.Enabled:=false;
while not(isRight(s)) do
begin
button2.Visible:=false;
edit1.Text:='0';
s:=edit1.Text;
end;
count:=1;
s:='('+s+')'; i:=1; p:=0;
while i<=length(s) do
begin
while s[i]='(' do
begin
push; inc(i);
end;
j:=i;
repeat
inc(i);
until ((s[i]<'0') or (s[i]>'9'));
t:=copy(s,j,i-j);
val(t,number[p],code);
inc(count);
repeat
if s[i]=')' then
begin
while symbol[p]<>'(' do pop;
dec(p); number[p]:=number[p+1];
end
else
begin
error:=1;
while can do
begin
pop;
if flag=false then
begin
edit1.Text:='0';
button2.Visible:=false;
exit;
end;
inc(error);
if error>3 then
begin
showmessage('您的输入中括号搭配有问题!请确认后重输!');
button2.Visible:=false;
edit1.Text:='0';
exit;
end;
end;
push;
end;
inc(i);
until (i>length(s)) or (s[i-1]<>')');
end;
edit2.Text:=inttostr(number[0]);
end;
procedure TForm1.Button2Click(Sender: TObject);
var error:integer;
begin
button2.Visible:=false;
//edit1.Enabled:=true;
for i:=0 to max do
number[i]:=0;
for i:=1 to max do
symbol[i]:=' ';
s:=edit1.Text ;
count:=1;
s:='('+s+')'; i:=1; p:=0;
tr:='第'+inttostr(count)+'步:变字符串为:'+s;
listbox1.AddItem(tr,listbox1);
while i<=length(s) do
begin
while s[i]='(' do
begin
push; tr:='第'+inttostr(count)+'步:符号'+s[i]+'入符号栈,此时符号栈内容为:';
listbox1.AddItem(tr,listbox1);
for kk:=0 to p do
begin
tr:=symbol[kk]+' ';
listbox1.AddItem(tr,listbox1);
end;
inc(i);
end;
j:=i;
repeat
inc(i);
until ((s[i]<'0') or (s[i]>'9'));
t:=copy(s,j,i-j);
val(t,number[p],code);
inc(count);
tr:='第'+inttostr(count)+'步:把数据'+t+'压入数据栈.数据栈内容为:';
listbox1.AddItem(tr,listbox1);
for kk:=0 to p do
begin
tr:=inttostr(number[kk])+' ';
listbox1.AddItem(tr,listbox1);
end;
repeat
if s[i]=')' then
begin
while symbol[p]<>'(' do pop;
dec(p); number[p]:=number[p+1];
tr:='第'+inttostr(count)+'步:数据栈阶段处理,数据栈内容为:';
listbox1.AddItem(tr,listbox1);
for kk:=0 to p do
begin
tr:=inttostr(number[kk])+' ';
listbox1.AddItem(tr,listbox1);
end;
end
else
begin
error:=1;
while can do
begin
pop;
tr:='第'+inttostr(count)+'步:开始取操作数做'+symbol[p+1]+'运算,数据栈内容为:';
listbox1.AddItem(tr,listbox1);
for kk:=0 to p do
begin
tr:=inttostr(number[kk])+' ';
listbox1.AddItem(tr,listbox1);
end;
end;
push; tr:='第'+inttostr(count)+'步:符号'+s[i]+'入符号栈,此时符号栈内容为:';
listbox1.AddItem(tr,listbox1);
for kk:=0 to p do
begin
tr:=symbol[kk]+' ';
listbox1.AddItem(tr,listbox1);
end;
end;
inc(i);
until (i>length(s)) or (s[i-1]<>')');
end;
edit2.Text:=inttostr(number[0]);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
for i:=0 to max do
number[i]:=0;
button2.Visible:=false;
label2.Visible:=false;
edit2.Visible:=false;
listbox1.Visible:=false;
mm:=0;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
mm:=mm+1;
case mm mod 7 of
0:label3.Font.Color:=clblue;
1:label3.font.Color:=clred;
2:label3.font.Color:=clyellow;
3:label3.font.Color:=clgreen;
4:label3.font.Color:=clpurple;
5:label3.font.Color:=clblack;
6:label3.font.Color:=clwhite;
end;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case key of
'+','-','*','/','%','^','(',')',#8:exit;
'0'..'9':exit;
end;
key:=#0;
showmessage('为了程序的正常运行,非法字符已锁定,抱歉!');
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -