procedure TForm2.FormDestroy(Sender: TObject);
begin
pic.FreeImage;
end;
procedure TForm2.Rename1Click(Sender: TObject);
begin
G.DSDinh[DinhDown].Ten:=inputbox('Rename','Name:',G.DSDinh[DinhDown].Ten);
HienThamSoCung(G);
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
FileChanged:=True;
end;
procedure TForm2.Exit1Click(Sender: TObject);
begin
close;
end;
23 trang |
Chia sẻ: thienmai908 | Lượt xem: 1307 | Lượt tải: 0
Bạn đang xem trước 20 trang nội dung tài liệu Giáo trình Toán rời rạc Phần phụ lục, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
gDi;St,So:string;
TimeNow:TDateTime;
SubPic:Tbitmap;
begin
If TimCacDinhKichHoat(G,D1,D2)=2 then
begin
If DuongDiNganNhat(G,D2,D1,DuongDi,ChiPhi) then
begin
SubPic:=Tbitmap.Create;
Imagelist2.GetBitmap(0,SubPic);
x:=G.DSDinh[DuongDi[0]].ToaDo.x;
y:=G.DSDinh[DuongDi[0]].ToaDo.y;
Pic.Canvas.Brush.Style:=BSclear;
Pic.Canvas.BrushCopy(rect(x,y-SubPic.Height,x+Subpic.Width,y),SubPic,Rect(0,0,SubPic.Width-1,SubPic.Height-1),RGB(255,255,255));
for i:=0 to high(DuongDi)-1 do
begin
Veline(G.DSDinh[DuongDi[i]].ToaDo,G.DSDinh[DuongDi[i+1]].ToaDo,
TrongSo(DuongDi[i],DuongDi[i+1]).Gia,Pic,RGB(255,0,0),100000);
TimeNow:=Time;
repeat
Application.ProcessMessages;
until (TimeNow+100000)>Time;
end;
St:='Duong di Tu ' + G.DSDinh[D1].Ten + ' Den ' + G.DSDinh[D2].Ten +' la:' + Cr + Lf;
for i:=0 to high(DuongDi)-1 do
begin
st:=st+G.DsDinh[DuongDi[i]].Ten +' --> ';
Vecung(Pic,G.DSDinh[DuongDi[i]].ToaDo,G.DSDinh[DuongDi[i+1]].ToaDo,
TrongSo(DuongDi[i],DuongDi[i+1]).Gia,True,RGB(255,0,0),RGB(0,0,255))
//Veline(G.DSDinh[DuongDi[i]].ToaDo,G.DSDinh[DuongDi[i+1]].ToaDo,
// TrongSo(DuongDi[i],DuongDi[i+1]).Gia,Pic,RGB(255,0,0),10000)
end;
st:=st+G.DsDinh[DuongDi[high(DuongDi)]].Ten+ cr+lf;
Str(ChiPhi:0:10,So);Catzerothua(So);
St:=St+ 'Voi chi phi la: ' + So;
Pic.Canvas.BrushCopy(rect(x,y-SubPic.Height,x+Subpic.Width,y),SubPic,Rect(0,0,SubPic.Width-1,SubPic.Height-1),RGB(255,255,255));
x:=G.DSDinh[DuongDi[high(DuongDi)]].ToaDo.x;
y:=G.DSDinh[DuongDi[high(DuongDi)]].ToaDo.y;
Pic.Canvas.Brush.Style:=BSclear;
Imagelist2.GetBitmap(1,SubPic);
Pic.Canvas.BrushCopy(rect(x,y-SubPic.Height,x+Subpic.Width,y),SubPic,Rect(0,0,SubPic.Width-1,SubPic.Height-1),RGB(255,255,255));
SubPic.Free;
DrawPaint(PaintBox1,Pic);
showmessage(st);
end
else
begin
Showmessage('Khong co duong di Tu ' + G.DSDinh[D1].Ten + ' Den ' + G.DSDinh[D2].Ten);
end;
end;
end;
procedure TForm2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var TraLoi:Word;
begin
If FileChanged then
begin
TraLoi:=MessageDlg('File changed. Do you want to save?',mtConfirmation ,[mbYes,mbNo,mbCancel],0);
If TraLoi=mrYes then
Form2.Save1Click(Sender)
else
If TraLoi=mrCancel then
begin CanClose:=false; exit;end;
end;
pic.FreeImage;
DeleteGraph(G);
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
pic.FreeImage;
end;
procedure TForm2.Rename1Click(Sender: TObject);
begin
G.DSDinh[DinhDown].Ten:=inputbox('Rename','Name:',G.DSDinh[DinhDown].Ten);
HienThamSoCung(G);
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
FileChanged:=True;
end;
procedure TForm2.Exit1Click(Sender: TObject);
begin
close;
end;
procedure TForm2.Delete1Click(Sender: TObject);
Var i,N,Start:integer;
Index:Array of integer;
begin
For i:=DinhDown to G.SoDinh-2 do
G.DSDinh[i]:=G.DSDinh[i+1];
G.SoDinh:=G.SoDinh-1;
Setlength(G.DSDinh,G.SoDinh);
Setlength(Index,G.SoCanh);
N:=0;Start:=-1;
For i:=0 to G.SoCanh-1 do
If (G.DSCanh[i].DinhDau=DinhDown)or(G.DSCanh[i].DinhCuoi=DinhDown) then
begin
If Start=-1 then Start:=N;
end
else
begin
Index[N]:=i;
N:=N+1;
end;
If Start-1 then
begin
G.SoCanh:=N;
For i:=Start to G.SoCanh-1 do
G.DSCanh[i]:=G.DSCanh[Index[i]];
For i:=0 to G.SoCanh-1 do
With G.DSCanh[i] do
begin
If DinhDau>DinhDown then DinhDau:=DinhDau-1;
If DinhCuoi>DinhDown then DinhCuoi:=DinhCuoi-1;
end;
Setlength(G.DSCanh,G.SoCanh);
end;
Setlength(Index,0);
HienThamSoCung(G);
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
FileChanged:=True;
end;
procedure TForm2.DeleteAll1Click(Sender: TObject);
begin
G.SoDinh:=0;G.SoCanh:=0;
Setlength(G.DSDinh,0);Setlength(G.DSCanh,0);
Pic.Canvas.Brush.Style:=bsSolid;
Pic.Canvas.Pen.Style:=psSolid;
Pic.Canvas.Brush.Color:=rgb(255,255,255);
Pic.Canvas.Pen.Color:=rgb(255,255,255);
Pic.Canvas.FillRect(Rect(0,0,Pic.Width,Pic.Height));
DrawPaint(PaintBox1,Pic);
FileChanged:=true;
end;
procedure TForm2.Save1Click(Sender: TObject);
var F:textfile;
i:integer;
begin
SaveDialog1.DefaultExt:='*.GRD';
SaveDialog1.Filter:='Graph data file (*.GRD)|*.GRD';
If not SaveDialog1.Execute then exit;
AssignFile(F,SaveDialog1.FileName);
Rewrite(F);
Try
Writeln(f,G.Sodinh,' ',G.Socanh);
For i:=0 to G.SoDinh-1 do
Writeln(F,G.DSDinh[i].ToaDo.x,' ',G.DSDinh[i].ToaDo.y,' ',G.DSDinh[i].Ten);
For i:=0 to G.SoCanh-1 do
Writeln(F,G.DSCanh[i].DinhDau,' ',G.DSCanh[i].DinhCuoi,' ',G.DSCanh[i].TrongSo.Gia);
except
Showmessage('Writting error');
end;
CloseFile(F);
FileChanged:=false;
end;
procedure TForm2.Open1Click(Sender: TObject);
Var F:TextFile;
i:integer;
begin
OpenDialog1.DefaultExt:='*.GRD';
OpenDialog1.Filter:='Graph data file (*.GRD)|*.GRD';
If not OpenDialog1.Execute then exit;
AssignFile(F,OpenDialog1.FileName);
ReSet(F);
Try
Readln(f,G.Sodinh,G.Socanh);
Setlength(G.DSDinh,G.SoDinh);
Setlength(G.DSCanh,G.SoCanh);
For i:=0 to G.SoDinh-1 do
begin
Readln(F,G.DSDinh[i].ToaDo.x,G.DSDinh[i].ToaDo.y,G.DSDinh[i].Ten);
G.DSDinh[i].Ten:=trimleft(G.DSDinh[i].Ten);
G.DSDinh[i].MucKichHoat:=0;
end;
For i:=0 to G.SoCanh-1 do
Readln(F,G.DSCanh[i].DinhDau,G.DSCanh[i].DinhCuoi,G.DSCanh[i].TrongSo.Gia);
except
DeleteGraph(G);
showmessage('Error struct file');
CloseFile(F);
Self.Caption:='Graph Algorithm - New document';
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
exit;
end;
CloseFile(F);
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
Filename:=OpenDialog1.FileName;
Self.Caption:='Graph Algorithm - ' + Filename;
FileChanged:=False;
end;
procedure TForm2.SpeedButton1Click(Sender: TObject);
var D1,D2,ChiSo,i:integer;
begin
TimCacDinhKichHoat(G,D1,D2);
If Not SpeedButton1.Down then
begin
Timcung(G,D2,D1,ChiSo);
for i:=Chiso to G.SoCanh-2 do
G.DSCanh[i]:=G.DSCanh[i+1];
G.SoCanh:=G.SoCanh-1;
Setlength(G.DSCanh,G.SoCanh);
end
else
begin
G.SoCanh:=G.SoCanh+1;
Setlength(G.DSCanh,G.SoCanh);
With G.DSCanh[G.SoCanh-1] do
begin
DinhDau:=D2;
DinhCuoi:=D1;
TrongSo.VoCung:=false;
TrongSo.Gia:=0;
end;
end;
HienThamSoCung(G);
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
end;
procedure TForm2.SpeedButton2Click(Sender: TObject);
var D1,D2,ChiSo,i:integer;
begin
TimCacDinhKichHoat(G,D1,D2);
If not SpeedButton2.Down then
begin
Timcung(G,D1,D2,ChiSo);
for i:=Chiso to G.SoCanh-2 do
G.DSCanh[i]:=G.DSCanh[i+1];
G.SoCanh:=G.SoCanh-1;
Setlength(G.DSCanh,G.SoCanh);
end
else
begin
G.SoCanh:=G.SoCanh+1;
Setlength(G.DSCanh,G.SoCanh);
With G.DSCanh[G.SoCanh-1] do
begin
DinhDau:=D1;
DinhCuoi:=D2;
TrongSo.VoCung:=false;
TrongSo.Gia:=0;
end;
end;
HienThamSoCung(G);
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
end;
procedure TForm2.New1Click(Sender: TObject);
begin
Filename:='';
FileChanged:=false;
DeleteGraph(G);
VeDoThi(G,Pic,imagelist1);
DrawPaint(PaintBox1,Pic);
end;
procedure TForm2.ExportPicturefile2Click(Sender: TObject);
Var T:TJpegimage;
begin
SaveDialog1.DefaultExt:='*.JPG';
SaveDialog1.Filter:='Bitmap image (*.BMP)|*.BMP|Jpeg image (*.JPG)|*.JPG';
SaveDialog1.FilterIndex:=2;
If not SaveDialog1.Execute then exit;
case SaveDialog1.FilterIndex of
1:{BMP}
Pic.SaveToFile(SaveDialog1.FileName);
2:{Jpeg}
begin
T:=TJpegimage.Create;
T.Assign(Pic);
try
T.SaveToFile(SaveDialog1.FileName);
finally
T.Free
end;
end;
end
end;
end.
Chương trình chính cài đặt như sau:
program Project1;
uses
Forms,
Func_DoThi in 'Func_DoThi.pas',
Unit2 in 'Unit2.pas' {Form2},
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm2, Form2);
Application.Run;
end.
Các file đính kèm theo tài liệu này:
- Giao trinh Toan roi rac - Phu luc 1.doc