Сообщение от antikiler_
Выложи весь проект, надо посмотреть...
модуль ввода данных
Кликните здесь для просмотра всего текста
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ObjectUnit, Math, StdCtrls, Grids, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Button3: TButton;
Edit1: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
RadioGroup1: TRadioGroup;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyBase_of_Students:TBase_of_Students;
Student:TStudent;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:='Ф.И.О';
StringGrid1.Cells[1,0]:='Матем';
StringGrid1.Cells[2,0]:='Физика';
StringGrid1.Cells[3,0]:='Русский';
StringGrid1.Cells[4,0]:='Ср. балл';
StringGrid1.Cells[0,1]:='Никимченко Иван';
StringGrid1.Cells[1,1]:='8';
StringGrid1.Cells[2,1]:='9';
StringGrid1.Cells[3,1]:='8';
MyBase_of_Students:=TBase_of_Students.Create();
Memo1.Clear;
RadioGroup1.ItemIndex:=1;
end;
procedure TForm1.Button1Click(Sender: TObject); //Add new record to file
var
Fl:TextFile;
TFileName:String;
begin
if SaveDialog1.Execute then
begin
TFileName:=SaveDialog1.FileName;
AssignFile (Fl,TFileName);
if FileExists('Fl') then
begin
Append(Fl);
end
else Rewrite(Fl);
end
else exit;
MyBase_of_Students.AddNewRecordToFile(Fl,Memo1);
end;
procedure TForm1.Button2Click(Sender: TObject); //Add new element ot array
var
M,F,R,i:Integer;
K_Name:String;
AvgM:Extended;
begin
K_Name:=StringGrid1.Cells[0,1];
While ((Length(K_Name)>0) AND (Copy(K_Name,1,1)=' ')) do Delete(K_Name,1,1);
While ((Length(K_Name)>0) AND (Copy(K_Name,length(K_Name),1)=' ')) do Delete(K_Name,Length(K_Name),1);
While ((Length(K_Name)>0) AND (Pos(' ',K_Name)>1)) do Delete(K_Name,Pos(' ',K_Name),1);
M:=StrToInt(StringGrid1.Cells[1,1]);
F:=StrToInt(StringGrid1.Cells[2,1]);
R:=StrToInt(StringGrid1.Cells[3,1]);
AvgM:=RoundTo((M+F+R)/3,-1);
MyBase_of_Students.Add(TStudent.Create(K_Name,M,F,R,AvgM),Memo1);
for i:=0 to 3 do
begin
StringGrid1.Cells[i,1]:='';
end;
end;
procedure TForm1.Button3Click(Sender: TObject); //Add data from file
var
Flr:TextFile;
TFileName:String;
St:String;
K_Name:String;
M,F,R,i:Integer;
AvgM:Extended;
begin
if OpenDialog1.Execute then
begin
TFileName:=OpenDialog1.FileName;
AssignFile(Flr,TFileName);
try
Reset(Flr);
except
exit
end;
end
else exit;
While not(EOF(Flr)) do
begin
ReadLn(Flr,St);
While ((Length(St)>0) AND (Copy(St,1,1)=' ')) do Delete(St,1,1);
While ((Length(St)>0) AND (Copy(St,length(St),1)=' ')) do Delete(St,Length(St),1);
While ((Length(St)>0) AND (Pos(' ',St)>1)) do Delete(St,Pos(' ',St),1);
i:=1;
K_Name:='';
While not(St[i] in ['0','1','2','3','4','5','6','7','8','9']) do
begin
K_Name:=K_Name+St[i];
inc(i);
end;
While ((Length(K_Name)>0) AND (Copy(K_Name,length(K_Name),1)=' ')) do Delete(K_Name,Length(K_Name),1);
M:=StrToInt(St[i]);
F:=StrToInt(St[i+2]);
R:=StrToInt(St[i+4]);
AvgM:=StrToFloat(St[i+6]);
MyBase_of_Students.ReadFileToArray(TStudent.Create(K_Name,M,F,R,AvgM),Memo1)
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
K_Name:String;
begin
K_Name:=Edit1.Text;
While ((Length(K_Name)>0) AND (Copy(K_Name,1,1)=' ')) do Delete(K_Name,1,1);
While ((Length(K_Name)>0) AND (Copy(K_Name,length(K_Name),1)=' ')) do Delete(K_Name,Length(K_Name),1);
While ((Length(K_Name)>0) AND (Pos(' ',K_Name)>1)) do Delete(K_Name,Pos(' ',K_Name),1);
MyBase_of_Students.LinearSearch(K_Name);
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
K_Name:String;
begin
K_Name:=Edit1.Text;
While ((Length(K_Name)>0) AND (Copy(K_Name,1,1)=' ')) do Delete(K_Name,1,1);
While ((Length(K_Name)>0) AND (Copy(K_Name,length(K_Name),1)=' ')) do Delete(K_Name,Length(K_Name),1);
While ((Length(K_Name)>0) AND (Pos(' ',K_Name)>1)) do Delete(K_Name,Pos(' ',K_Name),1);
MyBase_of_Students.DeleteDependingOnKey(K_Name);
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
Memo1.Clear;
case RadioGroup1.ItemIndex of
0: MyBase_of_Students.QuickSort(Memo1);
1: MyBase_of_Students.SlipSort(Memo1);
end;
end;
procedure TForm1.BitBtn4Click(Sender: TObject);
var
TotalAvgMark:Extended;
begin
TotalAvgMark:=Roundto(MyBase_of_Students.AvgMarkDisplay(),-1);
Label2.Caption:=(FloatToStrF(TotalAvgMark,ffFixed,1,1));
case RadioGroup1.ItemIndex of
0: MyBase_of_Students.QuickSort(Memo1);
1: MyBase_of_Students.SlipSort(Memo1);
end;
MyBase_of_Students.AvgMarkSort(TotalAvgMark,Memo1);
end;
end. |
|
модуль объекта, процедура с ошибкой ReadFileToArray
Кликните здесь для просмотра всего текста
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
| unit ObjectUnit;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Math, StdCtrls, Grids;
type
TStudent=class
KEY_Name:String;
Math,Phys,Rus:integer;
AvgMark:Extended;
Constructor Create (K_Name:String; M,F,R:Integer; AvgM:Extended);
end;
type
DynBase=array [1..1] of TStudent;
PointDynBase=^DynBase;
TBase_of_Students=class
BasePrim, BaseSec: PointDynBase;
Count:Integer;
szof:word;
Procedure Add(S:TStudent; Memo1:TMemo);
Procedure ReadFirstElemFromBasePrim (elem:TStudent);
Procedure AddNewRecordToFile (var Base:TextFile; Memo1:Tmemo);
Procedure ReadFileToArray (S:TStudent; Memo1:TMemo);
Procedure LinearSearch (K_Name:String);
Procedure SlipSort (Memo1:TMemo);
Procedure QuickSort (Memo1:TMemo);
Procedure DeleteDependingOnKey (K_Name:String);
Function AvgMarkDisplay():Extended;
Procedure AvgMarkSort(TotalAvgMark:Extended; Memo1:TMemo);
Constructor Create();
end;
//---------------------------------------
implementation
Constructor TStudent.Create (K_Name:String; M,F,R:Integer; AvgM:Extended);
begin
KEY_Name:=K_Name;
Math:=M;
Phys:=F;
Rus:=R;
AvgMark:=AvgM;
end;
Procedure TBase_of_Students.Add (S:TStudent; Memo1:TMemo);
var
i:integer;
begin
GetMem(BaseSec,(Count+1)*szof);
BaseSec[Count]:=S;
if Count>0 then
begin
Memo1.Lines.Add(BaseSec[Count].KEY_Name+' '+IntToStr(BaseSec[Count].Math)+' '+IntToStr(BaseSec[Count].Phys)+' '+IntToStr(BaseSec[Count].Rus)+' '+FloatToStr(BaseSec[Count].AvgMark));
for i:=1 to Count do
BaseSec^[i]:=BasePrim^[i];
FreeMem(BasePrim,szof*Count);
end;
BasePrim:=BaseSec;
Inc(Count);
end;
Procedure TBase_of_Students.ReadFirstElemFromBasePrim (elem:TStudent); //Storing in TStudent
var
i:integer;
begin
if Count>0 then
begin
TStudent.Create(BasePrim[1].KEY_Name,BasePrim[1].Math, BasePrim[1].Phys, BasePrim[1].Rus, BasePrim[1].AvgMark);
Dec(Count);
if Count>0 then
begin
GetMem(BaseSec,Count*szof);
for i:=1 to Count do
BaseSec^[i]:=BasePrim^[i+1];
end
else BaseSec:=Nil;
FreeMem(BasePrim,(Count+1)*szof);
BasePrim:=BaseSec;
end;
end;
Procedure TBase_of_Students.AddNewRecordToFile(var Base:TextFile; Memo1:Tmemo);
begin
Memo1.Lines.SaveToFile('Base'+'.txt');
CloseFile(Base);
end;
Procedure TBase_of_Students.ReadFileToArray(S:TStudent; Memo1:TMemo);
var
i:integer;
begin
GetMem(BaseSec,(Count+1)*szof);
BaseSec[Count+1].KEY_Name:=S.KEY_Name;
BaseSec[Count+1].Math:=S.Math;
BaseSec[Count+1].Phys:=S.Phys;
BaseSec[Count+1].Rus:=S.Rus;
BaseSec[Count+1].AvgMark:=S.AvgMark;
Memo1.Lines.Add(BaseSec[Count+1].KEY_Name+' '+IntToStr(BaseSec[Count+1].Math)+' '+IntToStr(BaseSec[Count+1].Phys)+' '+IntToStr(BaseSec[Count+1].Rus)+' '+FloatToStr(BaseSec[Count+1].AvgMark));
if (Count>0) then
begin
for i:=1 to Count do
BaseSec[i].KEY_Name:=BasePrim[i].KEY_Name;
BaseSec[i].Math:=BasePrim[i].Math;
BaseSec[i].Phys:=BasePrim[i].Phys;
BaseSec[i].Rus:=BasePrim[i].Rus;
BaseSec[i].AvgMark:=BasePrim[i].AvgMark;
FreeMem(BasePrim,szof*Count);
end;
BasePrim:=BaseSec;
Inc(Count);
end;
Procedure TBase_of_Students.LinearSearch(K_Name:String);
var
i,ne:Integer;
begin
i:=1;
ne:=Count+1;
While (i<ne) AND (BasePrim[i].KEY_Name<>K_Name) do
inc(i);
if i=ne then ShowMessage('Student not found')
else ShowMessage(BasePrim[i].KEY_Name+' '+IntToStr(BasePrim[i].Math)+' '+IntToStr(BasePrim[i].Phys)+' '+IntToStr(BasePrim[i].Rus)+' '+FloatToStr(BasePrim[i].AvgMark))
end;
Procedure TBase_of_Students.SlipSort(Memo1:TMemo);
Procedure SlipRec(L,m,R:word);
Var AssisArr: TBase_of_Students;
i,j,k:integer;
begin
i:=L; k:=0; j:=m+1;
AssisArr:=TBase_of_Students.Create;
GetMem(AssisArr.BasePrim,Count*szof);
while (i<=m) and (j<=R) do
if BasePrim[i].AvgMark<BasePrim[j].AvgMark then
begin
AssisArr.Add(TStudent.Create(BasePrim[i].KEY_Name,BasePrim[i].Math,BasePrim[i].Phys,BasePrim[i].Rus,BasePrim[i].AvgMark),Memo1);
Inc(i);
Inc(k);
end
else
begin
AssisArr.Add(TStudent.Create(BasePrim[j].KEY_Name,BasePrim[j].Math,BasePrim[j].Phys,BasePrim[j].Rus,BasePrim[j].AvgMark),Memo1);
Inc(j);
Inc(k);
end;
while i<=m do
begin
AssisArr.Add(TStudent.Create(BasePrim[i].KEY_Name,BasePrim[i].Math,BasePrim[i].Phys,BasePrim[i].Rus,BasePrim[i].AvgMark),Memo1);
Inc(i);
inc(k)
end;
while j<=R do
begin
AssisArr.Add(TStudent.Create(BasePrim[j].KEY_Name,BasePrim[j].Math,BasePrim[j].Phys,BasePrim[j].Rus,BasePrim[j].AvgMark),Memo1);
Inc(j);
Inc(k)
end;
k:=0;
for i:=L to R do
begin
Inc(k);
BasePrim[i]:=AssisArr.BasePrim[k];
{BasePrim[i].KEY_Name:=AssisArr.Base[k].KEY_Name;
BasePrim[i].Math:=AssisArr.Base[k].Math;
BasePrim[i].Phys:=AssisArr.Base[k].Phys;
BasePrim[i].Rus:=AssisArr.Base[k].Rus;
BasePrim[i].AvgMark:=AssisArr.Base[k].AvgMark;}
end;
FreeMem(AssisArr.BasePrim,Count*szof)
end;
Procedure SlipSortRec(L,R:integer);
var
m:integer;
begin
if L<>R then
begin
m:=(L+R) div 2;
SlipSortRec(L,m);
SlipSortRec(m+1,R);
SlipRec(L,m,R)
end;
end;
var
i,c:integer;
begin
SlipSortRec(1,Count);
Memo1.Clear;
c:=Count;
for i:=c downto 1 do
Memo1.Lines.Add(BasePrim[i].KEY_Name+' '+IntToStr(BasePrim[i].Math)+' '+IntToStr(BasePrim[i].Phys)+' '+IntToStr(BasePrim[i].Rus)+' '+FloatToStr(BasePrim[i].AvgMark))
end;
Procedure TBase_of_Students.QuickSort(Memo1:TMemo);
Procedure QuickSortRec(L,R:Integer);
var
i,j,numb:integer;
x,assesObj:TStudent;
begin
i:=L;
j:=R;
numb:=L+Random(R-L+1);
x:=TStudent.Create(BasePrim[numb].KEY_Name, BasePrim[numb].Math, BasePrim[numb].Phys, BasePrim[numb].Rus, BasePrim[numb].AvgMark);
Repeat
While BasePrim[i].AvgMark<x.AvgMark do inc(i);
While BasePrim[j].AvgMark>x.AvgMark do dec(j);
if i<=j then
begin
assesObj:=TStudent.Create(BasePrim[i].KEY_Name,BasePrim[i].Math,BasePrim[i].Phys, BasePrim[i].Rus, BasePrim[i].AvgMark);
BasePrim[i].KEY_Name:=BasePrim[j].KEY_Name;
BasePrim[i].Math:=BasePrim[j].Math;
BasePrim[i].Phys:=BasePrim[j].Phys;
BasePrim[i].Rus:=BasePrim[j].Rus;
BasePrim[i].AvgMark:=BasePrim[j].AvgMark;
BasePrim[j].KEY_Name:=assesObj.KEY_Name;
BasePrim[j].Math:=assesObj.Math;
BasePrim[j].Phys:=assesObj.Phys;
BasePrim[j].Rus:=assesObj.Rus;
BasePrim[j].AvgMark:=assesObj.AvgMark;
assesObj.Destroy;
inc(i);
dec(j);
end;
until i>j;
if L<j then QuickSortRec(L,j);
if i<R then QuickSortRec(i,R);
end;
var
i,c:integer;
begin
QuickSortRec(1,Count);
Memo1.Clear;
c:=Count;
for i:=c downto 1 do
Memo1.Lines.Add(BasePrim[i].KEY_Name+' '+IntToStr(BasePrim[i].Math)+' '+IntToStr(BasePrim[i].Phys)+' '+IntToStr(BasePrim[i].Rus)+' '+FloatToStr(BasePrim[i].AvgMark))
end;
Procedure TBase_of_Students.DeleteDependingOnKey(K_Name:String);
var
i,ne:integer;
begin
i:=1;
ne:=Count+1;
While (i<ne) AND (BasePrim[i].KEY_Name<>K_Name) do
inc(i);
if i=ne then ShowMessage('Student not found')
else
begin
BasePrim[i].KEY_Name:='0';
ShowMessage('Info was successfully deleted');
end;
end;
Function TBase_of_Students.AvgMarkDisplay():Extended;
var
i:integer;
sum:extended;
begin
sum:=0;
for i:=1 to Count do
sum:=sum+BasePrim[i].AvgMark;
Result:=sum/Count;
end;
Procedure TBase_of_Students.AvgMarkSort(TotalAvgMark:Extended; Memo1:TMemo);
var
i:integer;
count2:integer;
begin
Memo1.Clear;
count2:=1;
while BasePrim[count2].AvgMark<=TotalAvgMark do
inc(count2);
for i:=(Count) downto (Count-count2+2) do
Memo1.Lines.Add(BasePrim[i].KEY_Name+' '+IntToStr(BasePrim[i].Math)+' '+IntToStr(BasePrim[i].Phys)+' '+IntToStr(BasePrim[i].Rus)+' '+FloatToStr(BasePrim[i].AvgMark));
end;
Constructor TBase_of_Students.Create();
begin
TStudent.Create('',0,0,0,0);
szof:=sizeof(TStudent);
Count:=0;
BasePrim:=Nil;
end;
begin
end. |
|
Спасибо за помощь
0
|