L
lbeast_2001
Unregistered / Unconfirmed
GUEST, unregistred user!
我正在写一个自动计算24点的小游戏,也参考过了一些资料
但是其中的算法总是不对,请大家指教
24点游戏就是取4个数,按任意+ - * /四则运算得结果24
复源码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button1: TButton;
Button2: TButton;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
function caculate(j,k:real;operator:integer;var answer:real):boolean;
function operate(op:integer):string;
function change(t:integer):real;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
num1,num2,num3,num4:integer;
op1,op2,op3:integer;
answer1,answer2,answer3:real;
//const zero=0.00000001;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
form1.Edit1.Text:='';
form1.Edit2.Text:='';
form1.Edit3.Text:='';
form1.Edit4.Text:='';
form1.Edit1.enabled:=true;
form1.Edit2.enabled:=true;
form1.Edit3.enabled:=true;
form1.Edit4.enabled:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
x,y,z,w:integer;
begin
form1.Edit1.enabled:=false;
form1.Edit2.enabled:=false;
form1.Edit3.enabled:=false;
form1.Edit4.enabled:=false;
num1:=strtoint(form1.edit1.Text);
num2:=strtoint(form1.edit2.Text);
num3:=strtoint(form1.edit3.Text);
num4:=strtoint(form1.edit4.Text);
form1.edit5.text:='无解';
form1.edit6.Text:='无解';
form1.edit7.Text:='无解';
form1.edit8.Text:='无解';
form1.edit9.Text:='无解';
for x:=1 to 4do
for y:=1 to 4do
if y<>x then
for z:=1 to 4do
if (z<>x) and (z<>y) then
for w:=1 to 4do
if (w<>x) and (w<>y) and (w<>z) then
begin
for op1:=1 to 4do
for op2:=1 to 4do
for op3:=1 to 4do
begin
if caculate(change(x),change,op1,answer1) and
caculate(answer1,change(z),op2,answer2) and
caculate(answer2,change(w),op3,answer3) then
begin
if answer3=24 then
begin
form1.Edit5.Text:='(('+inttostr(round(change(x)))+operate(op1)+inttostr(round(change))+')'+operate(op2)
+inttostr(round(change(z)))+')'+operate(op3)+inttostr(round(change(w)))+'=24';
//exit;
end;
end;
if caculate(change(x),change,op1,answer1) and
caculate(change(z),change(w),op3,answer2) and
caculate(answer1,answer2,op2,answer3) then
begin
if answer3=24 then
begin
form1.Edit6.Text:='('+inttostr(round(change(x)))+operate(op1)+inttostr(round(change))+')'+operate(op2)+'('+inttostr(round(change(z)))+operate(op3)+inttostr(round(change(w)))+')'+'=24';
//exit;
end;
end;
if caculate(change,change(z),op2,answer1) and
caculate(change(x),answer1,op1,answer2) and
caculate(answer2,change(w),op3,answer3) then
begin
if answer3=24 then
begin
form1.Edit7.Text:='('+inttostr(round(change(x)))+operate(op1)+'('+inttostr(round(change))+operate(op2)
+inttostr(round(change(z)))+'))'+operate(op3)+inttostr(round(change(w)))+'=24';
//exit;
end;
end;
if caculate(change,change(z),op2,answer1) and
caculate(answer1,change(w),op3,answer2) and
caculate(change(x),answer2,op1,answer3) then
begin
if answer3=24 then
begin
form1.Edit8.Text:=inttostr(round(change(x)))+operate(op1)+'(('+inttostr(round(change))+operate(op2)
+inttostr(round(change(z)))+')'+operate(op3)+inttostr(round(change(w)))+')'+'=24';
//exit;
end;
end;
if caculate(change(z),change(w),op3,answer1) and
caculate(change,answer1,op2,answer2) and
caculate(change(x),answer2,op1,answer3) then
begin
if answer3=24 then
begin
form1.Edit9.Text:=inttostr(round(change(x)))+operate(op1)+'('+inttostr(round(change))+operate(op2)
+'('+inttostr(round(change(z)))+operate(op3)+inttostr(round(change(w)))+'))'+'=24';
exit;
end;
end;
end;
end;
end;
function tform1.caculate(j,k:real;operator:integer;var answer:real):boolean;
begin
case operator of
1: answer:=j+k;
2: answer:=j-k;
3: answer:=j*k;
4: if k<0 then
begin
result:=false;
exit;
end;
else
answer:=j/k;
end;
result:=true;
end;
function tform1.operate(op:integer):string;
begin
case op of
1: result:='+';
2: result:='-';
3: result:='*';
4: result:='/';
end;
end;
function tform1.change(t:integer):real;
begin
case t of
1: result:=num1;
2: result:=num2;
3: result:=num3;
4: result:=num4;
end;
end;
end.
但是其中的算法总是不对,请大家指教
24点游戏就是取4个数,按任意+ - * /四则运算得结果24
复源码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button1: TButton;
Button2: TButton;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
function caculate(j,k:real;operator:integer;var answer:real):boolean;
function operate(op:integer):string;
function change(t:integer):real;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
num1,num2,num3,num4:integer;
op1,op2,op3:integer;
answer1,answer2,answer3:real;
//const zero=0.00000001;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
form1.Edit1.Text:='';
form1.Edit2.Text:='';
form1.Edit3.Text:='';
form1.Edit4.Text:='';
form1.Edit1.enabled:=true;
form1.Edit2.enabled:=true;
form1.Edit3.enabled:=true;
form1.Edit4.enabled:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
x,y,z,w:integer;
begin
form1.Edit1.enabled:=false;
form1.Edit2.enabled:=false;
form1.Edit3.enabled:=false;
form1.Edit4.enabled:=false;
num1:=strtoint(form1.edit1.Text);
num2:=strtoint(form1.edit2.Text);
num3:=strtoint(form1.edit3.Text);
num4:=strtoint(form1.edit4.Text);
form1.edit5.text:='无解';
form1.edit6.Text:='无解';
form1.edit7.Text:='无解';
form1.edit8.Text:='无解';
form1.edit9.Text:='无解';
for x:=1 to 4do
for y:=1 to 4do
if y<>x then
for z:=1 to 4do
if (z<>x) and (z<>y) then
for w:=1 to 4do
if (w<>x) and (w<>y) and (w<>z) then
begin
for op1:=1 to 4do
for op2:=1 to 4do
for op3:=1 to 4do
begin
if caculate(change(x),change,op1,answer1) and
caculate(answer1,change(z),op2,answer2) and
caculate(answer2,change(w),op3,answer3) then
begin
if answer3=24 then
begin
form1.Edit5.Text:='(('+inttostr(round(change(x)))+operate(op1)+inttostr(round(change))+')'+operate(op2)
+inttostr(round(change(z)))+')'+operate(op3)+inttostr(round(change(w)))+'=24';
//exit;
end;
end;
if caculate(change(x),change,op1,answer1) and
caculate(change(z),change(w),op3,answer2) and
caculate(answer1,answer2,op2,answer3) then
begin
if answer3=24 then
begin
form1.Edit6.Text:='('+inttostr(round(change(x)))+operate(op1)+inttostr(round(change))+')'+operate(op2)+'('+inttostr(round(change(z)))+operate(op3)+inttostr(round(change(w)))+')'+'=24';
//exit;
end;
end;
if caculate(change,change(z),op2,answer1) and
caculate(change(x),answer1,op1,answer2) and
caculate(answer2,change(w),op3,answer3) then
begin
if answer3=24 then
begin
form1.Edit7.Text:='('+inttostr(round(change(x)))+operate(op1)+'('+inttostr(round(change))+operate(op2)
+inttostr(round(change(z)))+'))'+operate(op3)+inttostr(round(change(w)))+'=24';
//exit;
end;
end;
if caculate(change,change(z),op2,answer1) and
caculate(answer1,change(w),op3,answer2) and
caculate(change(x),answer2,op1,answer3) then
begin
if answer3=24 then
begin
form1.Edit8.Text:=inttostr(round(change(x)))+operate(op1)+'(('+inttostr(round(change))+operate(op2)
+inttostr(round(change(z)))+')'+operate(op3)+inttostr(round(change(w)))+')'+'=24';
//exit;
end;
end;
if caculate(change(z),change(w),op3,answer1) and
caculate(change,answer1,op2,answer2) and
caculate(change(x),answer2,op1,answer3) then
begin
if answer3=24 then
begin
form1.Edit9.Text:=inttostr(round(change(x)))+operate(op1)+'('+inttostr(round(change))+operate(op2)
+'('+inttostr(round(change(z)))+operate(op3)+inttostr(round(change(w)))+'))'+'=24';
exit;
end;
end;
end;
end;
end;
function tform1.caculate(j,k:real;operator:integer;var answer:real):boolean;
begin
case operator of
1: answer:=j+k;
2: answer:=j-k;
3: answer:=j*k;
4: if k<0 then
begin
result:=false;
exit;
end;
else
answer:=j/k;
end;
result:=true;
end;
function tform1.operate(op:integer):string;
begin
case op of
1: result:='+';
2: result:='-';
3: result:='*';
4: result:='/';
end;
end;
function tform1.change(t:integer):real;
begin
case t of
1: result:=num1;
2: result:=num2;
3: result:=num3;
4: result:=num4;
end;
end;
end.