程序是解决一个3X3的方阵,使得上下左右相加都得质数,开始给一个数,搜索的范围小于等于这个数
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
procedure GetPrimeNum(Max:integer);
function IsPrimeNum(num:integer):Boolean;
procedure SearchPhalanx(num:Integer);
procedure Output;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
phalanx: array[1..3,1..3] of Integer;
PrimeNum: array[1..100] of Integer;
ar:array of Integer;
entries: Integer;
ac: Integer;
got:Boolean;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
num:string;
i,j:Integer;
begin
entries:=0; ac:=0; got:=false;
num:=Edit1.Text;
try
SetLength(ar,StrToInt(num)); //注意下面的异常处理....1 except
on EConvertError do
begin
Application.MessageBox('That is wrong','Exception',mb_OK);
exit;
end;
end;
if StrToInt(num)<=10 then
begin
Application.MessageBox('请输入不小于10的整数!','错误',mb_OK);
exit;
end;
GetPrimeNum(2*StrToInt(num));
for i:=1 to 3 do
for j:=1 to 3 do
phalanx[i,j]:=0;
for i:=1 to StrToInt(num) do ar[i]:=0;
SearchPhalanx(StrToInt(num));
Output;
end;procedure TForm1.GetPrimeNum(Max:integer);
var
i,j: Integer;
book: Boolean;
begin
for i:=3 to Max do
begin
book:=false;
for j:=2 to round(sqrt(i)) do
if i div j=i/j then
begin
book:=true;
break;
end;
if not book then
begin
inc(entries);
PrimeNum[entries]:=i;
end;
end;
end;function TForm1.IsPrimeNum(num:Integer):Boolean;
var
i:Integer;
begin
result:=false;
for i:=1 to entries do
if num=PrimeNum[i] then
begin
result:=true;
break;
end;
end;procedure TForm1.SearchPhalanx(num:Integer);
var
i,j,k,ac1,ac2:Integer;
begin
for k:=1 to num do
begin
if ar[k]<>0 then continue;
for i:=1 to 3 do
for j:=1 to 3 do
begin
if phalanx[i,j]<>0 then continue;
ac1:=0; ac2:=0;
if (i-1>0) and (phalanx[i-1,j]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i-1,j]) then inc(ac2);
end;
if (i+1<4) and (phalanx[i+1,j]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i+1,j]) then inc(ac2);
end;
if (j-1>0) and (phalanx[i,j-1]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i,j-1]) then inc(ac2);
end;
if (j+1<4) and (phalanx[i,j+1]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i,j+1]) then inc(ac2);
end;
if ac1=ac2 then
begin
phalanx[i,j]:=k;
ar[k]:=1;
inc(ac);
if ac=9 then
begin
// Output;
got:=true;
exit;
end;
SearchPhalanx(num);
if got then exit;
dec(ac);
ar[k]:=0;
phalanx[i,j]:=0;
end;
end;
end;
end;procedure TForm1.Output;
var
i:Integer;
st:string;
begin
for i:=1 to 3 do
begin
//注意这里的st...2
st:=IntToStr(phalanx[i,1])+' '+IntToStr(phalanx[i,2])+' '+IntToStr(phalanx[i,3]);
Memo1.Lines.Add(st);
end;
Memo1.Lines.Add(' ');
end;end.问题1:这里的异常是当TEdit1取得非数值的内容后用StrToInt转换时产生的,我用的捕捉异常的代码并没有产生效用,而是系统给的异常处理问题2:程序编译没有问题,但是当运行的第二个注意的地方时,弹出对话框
Debugger Fault Notificaton
Project D:\temp\Project1.exe faulted with message:
'access violation at 0x00405929: write of address 0x00030df4'.
Process Stopped.Use Step or Run to continue. 另外的一个对话框就时 CPU 啦
不知道这时什么问题?问题3:我不知道怎么给分!!!!!!!!!!!!!!!哪位高手一次解决我的3个问题,我就给他分!!:)
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
procedure GetPrimeNum(Max:integer);
function IsPrimeNum(num:integer):Boolean;
procedure SearchPhalanx(num:Integer);
procedure Output;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
phalanx: array[1..3,1..3] of Integer;
PrimeNum: array[1..100] of Integer;
ar:array of Integer;
entries: Integer;
ac: Integer;
got:Boolean;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
num:string;
i,j:Integer;
begin
entries:=0; ac:=0; got:=false;
num:=Edit1.Text;
try
SetLength(ar,StrToInt(num)); //注意下面的异常处理....1 except
on EConvertError do
begin
Application.MessageBox('That is wrong','Exception',mb_OK);
exit;
end;
end;
if StrToInt(num)<=10 then
begin
Application.MessageBox('请输入不小于10的整数!','错误',mb_OK);
exit;
end;
GetPrimeNum(2*StrToInt(num));
for i:=1 to 3 do
for j:=1 to 3 do
phalanx[i,j]:=0;
for i:=1 to StrToInt(num) do ar[i]:=0;
SearchPhalanx(StrToInt(num));
Output;
end;procedure TForm1.GetPrimeNum(Max:integer);
var
i,j: Integer;
book: Boolean;
begin
for i:=3 to Max do
begin
book:=false;
for j:=2 to round(sqrt(i)) do
if i div j=i/j then
begin
book:=true;
break;
end;
if not book then
begin
inc(entries);
PrimeNum[entries]:=i;
end;
end;
end;function TForm1.IsPrimeNum(num:Integer):Boolean;
var
i:Integer;
begin
result:=false;
for i:=1 to entries do
if num=PrimeNum[i] then
begin
result:=true;
break;
end;
end;procedure TForm1.SearchPhalanx(num:Integer);
var
i,j,k,ac1,ac2:Integer;
begin
for k:=1 to num do
begin
if ar[k]<>0 then continue;
for i:=1 to 3 do
for j:=1 to 3 do
begin
if phalanx[i,j]<>0 then continue;
ac1:=0; ac2:=0;
if (i-1>0) and (phalanx[i-1,j]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i-1,j]) then inc(ac2);
end;
if (i+1<4) and (phalanx[i+1,j]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i+1,j]) then inc(ac2);
end;
if (j-1>0) and (phalanx[i,j-1]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i,j-1]) then inc(ac2);
end;
if (j+1<4) and (phalanx[i,j+1]<>0) then
begin
inc(ac1);
if IsPrimeNum(k+phalanx[i,j+1]) then inc(ac2);
end;
if ac1=ac2 then
begin
phalanx[i,j]:=k;
ar[k]:=1;
inc(ac);
if ac=9 then
begin
// Output;
got:=true;
exit;
end;
SearchPhalanx(num);
if got then exit;
dec(ac);
ar[k]:=0;
phalanx[i,j]:=0;
end;
end;
end;
end;procedure TForm1.Output;
var
i:Integer;
st:string;
begin
for i:=1 to 3 do
begin
//注意这里的st...2
st:=IntToStr(phalanx[i,1])+' '+IntToStr(phalanx[i,2])+' '+IntToStr(phalanx[i,3]);
Memo1.Lines.Add(st);
end;
Memo1.Lines.Add(' ');
end;end.问题1:这里的异常是当TEdit1取得非数值的内容后用StrToInt转换时产生的,我用的捕捉异常的代码并没有产生效用,而是系统给的异常处理问题2:程序编译没有问题,但是当运行的第二个注意的地方时,弹出对话框
Debugger Fault Notificaton
Project D:\temp\Project1.exe faulted with message:
'access violation at 0x00405929: write of address 0x00030df4'.
Process Stopped.Use Step or Run to continue. 另外的一个对话框就时 CPU 啦
不知道这时什么问题?问题3:我不知道怎么给分!!!!!!!!!!!!!!!哪位高手一次解决我的3个问题,我就给他分!!:)
except
on EConvertError do
begin
Application.MessageBox('That is wrong','Exception',mb_OK);
exit;
end;
end;
改成:
except
Application.MessageBox('That is wrong','Exception',mb_OK);
exit;
end;