Delphi алгоритм

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
unit Unit1;
{2}
{3} interface
{4}
{5} uses
{6} Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
{7} Dialogs, Grids, StdCtrls,printers, ComCtrls;
{8}
{9} type
{10} TForm1 = class(TForm)
{11} PrinterSetupDialog1: TPrinterSetupDialog;
{12} StringGrid1: TStringGrid;
{13} Button1: TButton;
{14} Button2: TButton;
{15} Button3: TButton;
{16} Button4: TButton;
{17} StringGrid3: TStringGrid;
{18} StringGrid2: TStringGrid;
{19} procedure ButtonClick(Sender: TObject);
{20} procedure Button3Click(Sender: TObject);
{21} procedure Button2Click(Sender: TObject);
{22} procedure Button1Click(Sender: TObject);
{23} private
{24} { Private declarations }
{25} public
{26} { Public declarations }
{27} end;
{28}
{29} var
{30} Form1: TForm1;
{31} f: TextFile;
{32} i,j: integer;
{33} a:array of array of integer;
{34} b:array of integer;
{35} mass:array of integer;
{36} implementation
{37}
{38} {$R *.dfm}
{39}
{40} procedure open(StringGrid1: TStringGrid;s: string; x: integer); //ïðîöåäóðà îòêðûòèÿ ôàéëà
{41} var
{42} c: integer;
{43} begin
{44} AssignFile(F, s);
{45} i:=-1;
{46} Reset(F);
{47} StringGrid1.ColCount:=0;
{48} while not Eof(f) do begin
{49} if i>97 then
{50} exit;
{51} i:=i+1;
{52} StringGrid1.RowCount:=i+1;
{53} StringGrid1.Cells[j,i+1]:='';
{54} j:=-1;
{55} while not EoLn(f) do begin
{56} if (x=0) and (j>97) then
{57} Break
{58} else if (x=1) and (j=0) then
{59} Break;
{60} j:=j+1;
{61} if j>=StringGrid1.ColCount then begin
{62} StringGrid1.ColCount:=j+1;
{63} StringGrid1.Cells[j+1,i]:='';
{64} end;
{65} try
{66} Read(F,c);
{67} except //åñëè íå óäàëîñü ïðî÷èòàòü ôàéë
{68} CloseFile(f);
{69} raise Exception.Create('Îøèáêà ÷òåíèÿ äàííûõ èç ôàéëà!');
{70} end;
{71} if (c>=-100) and (c<=100) then //ïðîâåðåðÿåò âõîäÿò ëè ÷èñëà â äèàïîçîí [-100;100]
{72} StringGrid1.Cells[j,i]:=inttostr(c) else begin
{73} CloseFile(f);
{74} raise Exception.Create('×èñëà íå âõîäÿò â äèàïîçîí [-100;100]');
{75} end;
{76} end;
{77} readln(f);
{78} end;
{79} CloseFile(F);
{80} end;
{81}
{82} procedure TForm1.Button1Click(Sender: TObject); //ïðîöåäóðà îòêðûòèÿ ôàéëà
{83} begin
{84} open(StringGrid1,'ìàòðèöà.txt',0);
{85} open(StringGrid2,'âåêòîð.txt',1);
{86} if StringGrid1.ColCount>StringGrid2.RowCount then
{87} StringGrid2.RowCount:=StringGrid1.ColCount
{88} else if StringGrid1.ColCount<StringGrid2.RowCount then
{89} StringGrid1.ColCount:=StringGrid2.RowCount;
{90} StringGrid3.RowCount:=StringGrid1.RowCount;
{91} end;
{92}
{93} procedure save(StringGrid1: TStringGrid;s: string; x,p: integer); //ïðîöåäóðà ñîõðàíåíèÿ è ïå÷àòè ìàòðèöû
{94} begin
{95} For i:= 0 To StringGrid1.RowCount -1 Do begin //ïóñòûå ÿ÷åéêè çàïîëíÿþòñÿ 0
{96} For j:=0 To StringGrid1.ColCount -1 Do
{97} if StringGrid1.Cells[j,i]='' then
{98} StringGrid1.Cells[j,i]:='0';
{99} end;
{100} AssignFile(F, s);
{101} if p=1 then //ñîõðàíåíèå ìàòðèöû èëè å¸ ïå÷àòü
{102} AssignPrn(F);
{103} Rewrite(F);
{104} For i:= 0 To StringGrid1.RowCount -1 Do begin
{105} For j:=0 To StringGrid1.ColCount -1 Do begin
{106} if j=StringGrid1.ColCount -1 then
{107} Write(F, StringGrid1.Cells[j,i])
{108} else
{109} Write(F, StringGrid1.Cells[j,i],' ');
{110} end;
{111} if i<StringGrid1.RowCount -1 then
{112} writeln(F);
{113} end;
{114} CloseFile(F);
{115} end;
{116}
{117} procedure TForm1.Button3Click(Sender: TObject); //ïå÷àòü
{118} begin
{119} if not PrinterSetupDialog1.Execute then Exit;
{120} save(StringGrid1,'ìàòðèöà.txt',0,1);
{121} save(StringGrid2,'âåêòîð.txt',1,1);
{122} save(StringGrid3,'ðåçóëüòàò.txt',2,1);
{123} end;
{124}
{125} procedure TForm1.Button2Click(Sender: TObject); //ñîõðàíèòü
{126} begin
{127} save(StringGrid1,'ìàòðèöà.txt',0,0);
{128} save(StringGrid2,'âåêòîð.txt',1,0);
{129} save(StringGrid3,'ðåçóëüòàò.txt',2,0);
{130} end;
{131}
{132} procedure TForm1.ButtonClick(Sender: TObject); //ïðîöåäóðà óìíîæåíèÿ ìàòðèöû íà âåêòîð
{133} var
{134} N,M: integer;
{135} Begin
{136} N:= StringGrid1.RowCount;
{137} M:= StringGrid1.ColCount;
{138} SetLength(a,N,m); //ðàçìåð ìàññèâîâ
{139} SetLength(b,N);
{140} SetLength(mass,N);
{141} For j:=0 To StringGrid1.ColCount -1 Do begin
{142} if StringGrid2.Cells[0,j]='' then //ïóñòûå ÿ÷åéêè çàïîëíÿþòñÿ 0
{143} StringGrid2.Cells[0,j]:='0';
{144} b[j]:= strtoint(StringGrid2.Cells[0,j]); //çàïèñü âåêòîðà â ìàññèâ
{145} end;
{146} For i:= 0 To StringGrid1.RowCount -1 Do begin
{147} mass[i]:=0;
{148} For j:=0 To StringGrid1.ColCount -1 Do begin
{149} if StringGrid1.Cells[j,i]='' then //ïóñòûå ÿ÷åéêè çàïîëíÿþòñÿ 0
{150} StringGrid1.Cells[j,i]:='0';
{151} a[i,j]:= strtoint(StringGrid1.Cells[j,i]); //çàïèñü ìàòðèöû â ìàññèâ
{152} mass[i]:=mass[i]+a[i,j]*b[j]; //ðåçóëüòàò
{153} end;
{154} StringGrid3.Cells[0,i]:= inttostr(mass[i]); //âûâîä ðåçóëüòàòà
{155} end;
{156} end;
{157}
{158} end.