1 |
// ***** BEGIN LICENSE BLOCK *****
|
2 |
// * Version: MPL 1.1
|
3 |
// *
|
4 |
// * The contents of this file are subject to the Mozilla Public License Version
|
5 |
// * 1.1 (the "License"); you may not use this file except in compliance with
|
6 |
// * the License. You may obtain a copy of the License at
|
7 |
// * http://www.mozilla.org/MPL/
|
8 |
// *
|
9 |
// * Software distributed under the License is distributed on an "AS IS" basis,
|
10 |
// * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
11 |
// * for the specific language governing rights and limitations under the
|
12 |
// * License.
|
13 |
// *
|
14 |
// * The Original Code is TurboPower SysTools
|
15 |
// *
|
16 |
// * The Initial Developer of the Original Code is
|
17 |
// * TurboPower Software
|
18 |
// *
|
19 |
// * Portions created by the Initial Developer are Copyright (C) 1996-2002
|
20 |
// * the Initial Developer. All Rights Reserved.
|
21 |
// *
|
22 |
// * Contributor(s):
|
23 |
// *
|
24 |
// * ***** END LICENSE BLOCK *****
|
25 |
//---------------------------------------------------------------------------
|
26 |
#include <vcl\vcl.h>
|
27 |
#pragma hdrstop
|
28 |
|
29 |
#include <stdlib.h>
|
30 |
#include "Ex1DArrU.h"
|
31 |
//---------------------------------------------------------------------------
|
32 |
#pragma package(smart_init)
|
33 |
#pragma link "StLArr"
|
34 |
|
35 |
#pragma resource "*.dfm"
|
36 |
TForm1 *Form1;
|
37 |
//---------------------------------------------------------------------------
|
38 |
__fastcall TForm1::TForm1(TComponent* Owner)
|
39 |
: TForm(Owner)
|
40 |
{
|
41 |
}
|
42 |
//---------------------------------------------------------------------------
|
43 |
void __fastcall TForm1::FormCreate(TObject *Sender)
|
44 |
{
|
45 |
//RegisterClass should be used, but it is not there in C++Builder 1.0
|
46 |
RegisterClassAlias(__classid(TStLArray), "TStLArray");
|
47 |
UpdateButtons(false);
|
48 |
}
|
49 |
//---------------------------------------------------------------------------
|
50 |
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
|
51 |
{
|
52 |
delete MyLArray;
|
53 |
}
|
54 |
//---------------------------------------------------------------------------
|
55 |
void TForm1::SetBusy(Boolean B)
|
56 |
{
|
57 |
if (B) {
|
58 |
Screen->Cursor = crHourGlass;
|
59 |
}
|
60 |
else {
|
61 |
Screen->Cursor = crDefault;
|
62 |
}
|
63 |
};
|
64 |
//---------------------------------------------------------------------------
|
65 |
int __fastcall MyArraySort(const void* El1, const void* El2)
|
66 |
{
|
67 |
ARecord R1, R2;
|
68 |
int Res;
|
69 |
|
70 |
R1 = *(ARecord*) El1;
|
71 |
R2 = *(ARecord*) El2;
|
72 |
|
73 |
Res = (R1.X - R2.X);
|
74 |
if (Res == 0) {
|
75 |
Res = (R1.Y - R2.Y);
|
76 |
if (Res == 0) {
|
77 |
Res = floor(R1.Mag - R2.Mag);
|
78 |
if (Res == 0) {
|
79 |
Res = CompareText(R1.Name1, R2.Name1);
|
80 |
if (Res == 0)
|
81 |
Res = CompareText(R1.Name2, R2.Name2);
|
82 |
}
|
83 |
}
|
84 |
}
|
85 |
return Res;
|
86 |
};
|
87 |
//---------------------------------------------------------------------------
|
88 |
void TForm1::FillControls()
|
89 |
{
|
90 |
Edit1->Text = ARec.Name1;
|
91 |
Edit2->Text = ARec.Name2;
|
92 |
Edit3->Text = IntToStr((int)ARec.X);
|
93 |
Edit4->Text = IntToStr((int)ARec.Y);
|
94 |
Edit5->Text = FloatToStr(ARec.Mag);
|
95 |
};
|
96 |
//---------------------------------------------------------------------------
|
97 |
void TForm1::FillListBox()
|
98 |
{
|
99 |
int Step;
|
100 |
|
101 |
SetBusy(true);
|
102 |
LB1->Clear();
|
103 |
LB1->Perform(WM_SETREDRAW, 0, 0);
|
104 |
|
105 |
for (Step = 0; Step < MyLArray->Count; Step++) {
|
106 |
MyLArray->Get(Step, &ARec);
|
107 |
LB1->Items->Add(IntToStr((int)ARec.X) + ", " + IntToStr((int)ARec.Y));
|
108 |
};
|
109 |
|
110 |
LB1->Perform(WM_SETREDRAW,1,0);
|
111 |
LB1->Update();
|
112 |
MyLArray->Get(0, &ARec);
|
113 |
SetBusy(false);
|
114 |
};
|
115 |
//---------------------------------------------------------------------------
|
116 |
bool TForm1::CheckControls(ARecord& AR)
|
117 |
{
|
118 |
if ((Edit1->Text == "") || (Edit2->Text == "") ||
|
119 |
(Edit3->Text == "") ||
|
120 |
(Edit4->Text == "") ||
|
121 |
(Edit5->Text == ""))
|
122 |
return false;
|
123 |
|
124 |
AR.Name1 = Edit1->Text;
|
125 |
AR.Name2 = Edit2->Text;
|
126 |
AR.X = StrToInt(Edit3->Text);
|
127 |
AR.Y = StrToInt(Edit4->Text);
|
128 |
AR.Mag = StrToFloat(Edit5->Text);
|
129 |
|
130 |
return true;
|
131 |
};
|
132 |
//---------------------------------------------------------------------------
|
133 |
void TForm1::UpdateButtons(bool AOK)
|
134 |
{
|
135 |
ClearBtn->Enabled = AOK;
|
136 |
FillBtn->Enabled = AOK;
|
137 |
GetBtn->Enabled = AOK;
|
138 |
PutBtn->Enabled = AOK;
|
139 |
SortBtn->Enabled = AOK;
|
140 |
SaveBtn->Enabled = AOK;
|
141 |
};
|
142 |
//---------------------------------------------------------------------------
|
143 |
void __fastcall TForm1::LB1DblClick(TObject *Sender)
|
144 |
{
|
145 |
MyLArray->Get(LB1->ItemIndex, &ARec);
|
146 |
ElemNum->Text = IntToStr(LB1->ItemIndex);
|
147 |
FillControls();
|
148 |
}
|
149 |
//---------------------------------------------------------------------------
|
150 |
void __fastcall TForm1::CreateBtnClick(TObject *Sender)
|
151 |
{
|
152 |
int Step;
|
153 |
int I;
|
154 |
int Value;
|
155 |
|
156 |
LB1->Clear();
|
157 |
SetBusy(true);
|
158 |
|
159 |
if (MyLArray)
|
160 |
delete MyLArray;
|
161 |
|
162 |
UpdateButtons(false);
|
163 |
MyLArray = new TStLArray(5000, sizeof(ARec));
|
164 |
|
165 |
MyLArray->ElementsStorable = true;
|
166 |
MyLArray->Clear();
|
167 |
|
168 |
Randomize();
|
169 |
LB1->Perform(WM_SETREDRAW,0,0);
|
170 |
Value = MyLArray->Count;
|
171 |
|
172 |
for (Step = 0; Step < Value-1; Step++) {
|
173 |
ARec.Name1 = "";
|
174 |
ARec.Name2 = "";
|
175 |
for (I = 1; I <= 10; I++) {
|
176 |
ARec.Name1 = ARec.Name1 + (char)(random(26) + 'A');
|
177 |
ARec.Name2 = ARec.Name2 + (char)(random(26) + 'A');
|
178 |
};
|
179 |
|
180 |
ARec.X = floor(random(1000));
|
181 |
ARec.Y = floor(random(1000));
|
182 |
ARec.Mag = sqrt(random(25000));
|
183 |
|
184 |
MyLArray->Put(Step, &ARec);
|
185 |
LB1->Items->Add(IntToStr((int)ARec.X) + ", " + IntToStr((int)ARec.Y));
|
186 |
};
|
187 |
LB1->Perform(WM_SETREDRAW,1,0);
|
188 |
LB1->Update();
|
189 |
|
190 |
ElemNum->Text = "0";
|
191 |
MyLArray->Get(0, &ARec);
|
192 |
FillControls();
|
193 |
UpdateButtons(true);
|
194 |
SetBusy(false);
|
195 |
}
|
196 |
//---------------------------------------------------------------------------
|
197 |
void __fastcall TForm1::ClearBtnClick(TObject *Sender)
|
198 |
{
|
199 |
MyLArray->Clear();
|
200 |
LB1->Clear();
|
201 |
|
202 |
ElemNum->Text = "0";
|
203 |
MyLArray->Get(0, &ARec);
|
204 |
FillControls();
|
205 |
}
|
206 |
//---------------------------------------------------------------------------
|
207 |
void __fastcall TForm1::FillBtnClick(TObject *Sender)
|
208 |
{
|
209 |
if (!CheckControls(ARec)) {
|
210 |
ShowMessage("One or more invalid entries");
|
211 |
return;
|
212 |
};
|
213 |
|
214 |
MyLArray->Fill(&ARec);
|
215 |
|
216 |
FillListBox();
|
217 |
ElemNum->Text = "0";
|
218 |
MyLArray->Get(0, &ARec);
|
219 |
FillControls();
|
220 |
}
|
221 |
//---------------------------------------------------------------------------
|
222 |
void __fastcall TForm1::GetBtnClick(TObject *Sender)
|
223 |
{
|
224 |
long E;
|
225 |
|
226 |
if (ElemNum->Text == "")
|
227 |
ElemNum->Text = "0";
|
228 |
|
229 |
E = StrToInt(ElemNum->Text);
|
230 |
MyLArray->Get(E, &ARec);
|
231 |
|
232 |
FillControls();
|
233 |
}
|
234 |
//---------------------------------------------------------------------------
|
235 |
void __fastcall TForm1::PutBtnClick(TObject *Sender)
|
236 |
{
|
237 |
long E;
|
238 |
|
239 |
if (ElemNum->Text == "")
|
240 |
ElemNum->Text = "0";
|
241 |
|
242 |
if (!CheckControls(ARec)) {
|
243 |
ShowMessage("One or more invalid entries");
|
244 |
return;
|
245 |
};
|
246 |
|
247 |
E = StrToInt(ElemNum->Text);
|
248 |
MyLArray->Put(E, &ARec);
|
249 |
LB1->Items->Strings[E] = (IntToStr((int)ARec.X) + ", " + IntToStr((int)ARec.Y));
|
250 |
|
251 |
MyLArray->Get(E, &ARec);
|
252 |
FillControls();
|
253 |
}
|
254 |
//---------------------------------------------------------------------------
|
255 |
void __fastcall TForm1::SortBtnClick(TObject *Sender)
|
256 |
{
|
257 |
SetBusy(true);
|
258 |
MyLArray->Sort(MyArraySort);
|
259 |
SetBusy(false);
|
260 |
|
261 |
FillListBox();
|
262 |
FillControls();
|
263 |
SetBusy(false);
|
264 |
}
|
265 |
//---------------------------------------------------------------------------
|
266 |
void __fastcall TForm1::LoadBtnClick(TObject *Sender)
|
267 |
{
|
268 |
if (OD1->Execute()) {
|
269 |
if (!MyLArray) {
|
270 |
UpdateButtons(false);
|
271 |
MyLArray = new TStLArray(5000, sizeof(ARec));
|
272 |
MyLArray->ElementsStorable = true;
|
273 |
};
|
274 |
|
275 |
MyLArray->Clear();
|
276 |
MyLArray->LoadFromFile(OD1->FileName);
|
277 |
|
278 |
FillListBox();
|
279 |
FillControls();
|
280 |
UpdateButtons(true);
|
281 |
};
|
282 |
}
|
283 |
//---------------------------------------------------------------------------
|
284 |
void __fastcall TForm1::SaveBtnClick(TObject *Sender)
|
285 |
{
|
286 |
if (SD1->Execute())
|
287 |
MyLArray->StoreToFile(SD1->FileName);
|
288 |
}
|
289 |
//--------------------------------------------------------------------------- |