新增9個TButton、3個TMemo、1個TLabel
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Memo2: TMemo;
Button6: TButton;
Button7: TButton;
Memo3: TMemo;
Label1: TLabel;
Button9: TButton;
Button8: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
n : array[1..100] of integer;
a : array [1..4, 1..4] of integer;
m : array[1..100] of integer;
b : array [1..4, 1..4] of integer;
implementation
{$R *.dfm}
procedure swap(var a,b:integer);
var
t : integer;
begin
t := a;
a := b;
b := t;
end;
// 讀入陣列A
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
F : textfile;
begin
Memo1.clear;
i := 0;
AssignFile(F,'number.txt');
reset(F);
while not(EOF(F)) do begin
i := i+1;
readln(F,n[i]);
Memo1.lines.add(inttostr(n[i]));
end;
closeFile(F);
end;
// 排序
procedure TForm1.Button2Click(Sender: TObject);
var
i, j : integer;
begin
Memo1.clear;
for i:=1 to 15 do begin
for j:=i+1 to 16 do
if n[i]>n[j] then
swap(n[i], n[j]);
Memo1.lines.add(inttostr(n[i]));
end;
Memo1.lines.add(inttostr(n[16]));
end;
// 轉成矩陣(橫轉)
procedure TForm1.Button3Click(Sender: TObject);
var
i,j : integer;
s : string;
begin
memo1.Clear;
for i :=1 to 4 do begin
s :='';
for j :=1 to 4 do begin
a[i,j] :=n[4*(i-1)+j];
s := s+ inttostr(a[i,j])+' ';
end;
memo1.Lines.Add(s);
end;
end;
// 轉成矩陣(直轉)
procedure TForm1.Button9Click(Sender: TObject);
var
i,j : integer;
s : string;
begin
memo1.Clear;
for i :=1 to 4 do begin
s :='';
for j :=1 to 4 do begin
a[i,j] :=n[i+4*(j-1)];
s := s+ inttostr(a[i,j])+' ';
end;
memo1.Lines.Add(s);
end;
end;
// 亂數產生B矩陣
procedure TForm1.Button6Click(Sender: TObject);
var
i, j:integer;
s : string;
begin
memo2.Clear;
for i:=1 to 16 do
m[i]:=random(100);
for i :=1 to 4 do begin
s :='';
for j :=1 to 4 do begin
b[i,j] :=m[j+4*(i-1)];
s := s+ inttostr(b[i,j])+' ';
end;
memo2.Lines.Add(s);
end;
end;
// B矩陣排序
procedure TForm1.Button7Click(Sender: TObject);
var
i, j : integer;
s : string;
begin
Memo2.clear;
for i:=1 to 15 do begin
for j:=i+1 to 16 do
if m[i]>m[j] then
swap(m[i], m[j]);
end;
for i :=1 to 4 do begin
s :='';
for j :=1 to 4 do begin
b[i,j] :=m[4*(i-1)+j];
s := s+ inttostr(b[i,j])+' ';
end;
memo2.Lines.Add(s);
end;
end;
// +
procedure TForm1.Button4Click(Sender: TObject);
var
i, j : integer;
s : string;
begin
Memo3.Clear;
for i := 1 to 4 do begin
s := '';
for j := 1 to 4 do begin
s := s + inttostr(a[i,j] + b[i,j]) + ' ';
end;
Memo3.Lines.Add(s);
end;
end;
// -
procedure TForm1.Button5Click(Sender: TObject);
var
i, j : integer;
s : string;
begin
Memo3.Clear;
for i := 1 to 4 do begin
s := '';
for j := 1 to 4 do begin
s := s + inttostr(a[i,j] - b[i,j]) + ' ';
end;
Memo3.Lines.Add(s);
end;
end;
procedure TForm1.Button8Click(Sender: TObject);
var
i, j, k, max, min : integer;
begin
Memo3.clear;
for j := 1 to 4 do begin //列
min := 1;
for i := 2 to 4 do
if a[j,i] min := i;
max := j;
for k := 1 to 4 do
if (a[k,min]>a[j,min]) and (k<>j) then //k不能=j
max := k;
if max=j then
Memo3.lines.add('鞍點在:'+inttostr(j)+', '+inttostr(min))
else
Memo3.lines.add('沒有鞍點');
end;
end;
end.
沒有留言:
張貼留言