Добавление элементов в коллекцию из файла
06.04.2013, 18:38. Показов 939. Ответов 0
Народ help  . Сейчас при нажатии на кнопку открыть данные из файла выводятся в стринггрид потом из него записываются в коллекцию. Нужно при нажатии на кнопку открыть что бы из сохраненных ранее в файл данных,они записались в новую коллекцию, а уже потом в стринггрид. И тут же сделать исключительную ситуацию например если в файле вместо целочисленного поля мы поставим буквы. Пожалуйста помогите очень срочно нужно, буду очень признателен.
| C++ | 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
| #ifndef _AUTO_CPP_
#define _AUTO_CPP_
#include <iostream.h>
#include "Auto.h"
using namespace std;
Auto::Auto() : name("Opel"),
age(1997),
owner("Max"),
collor("blue"),
nomer(1336),
model("astra"){}
Auto::Auto ( std::string name,
int age,
std::string owner,
std::string collor,
int nomer,
std::string model ) : name(name),
age(age),
owner(owner),
collor(collor),
nomer(nomer),
model(model) {}
Auto::Auto(const Auto &automobil): name(automobil.name),
age(automobil.age),
owner(automobil.owner),
collor(automobil.collor),
nomer(automobil.nomer),
model(automobil.model) {}
Auto::~Auto() {}
const std::string Auto::getName() {
return name;
}
void Auto::setName(const std::string &name) {
this->name.assign(name);
}
const int Auto::getAge() {
return age;
}
void Auto::setAge(int age) {
this->age = age;
}
const std::string Auto::getOwner() {
return owner;
}
void Auto::setOwner(const std::string &owner) {
this->owner.assign(owner);
}
const std::string Auto::getCollor() {
return collor;
}
void Auto::setCollor(const std::string &collor) {
this->collor.assign(collor);
}
const int Auto::getNomer() {
return nomer;
}
void Auto::setNomer(int nomer) {
this->nomer = nomer;
}
const std::string Auto::getModel() {
return model;
}
void Auto::setModel(const std::string &model) {
this->model.assign(model);
}
void Auto::bark() {
cout << endl;
cout << " Марка автомобиля : " << getName() << endl;
cout << " Модель : " << getModel() << endl;
cout << " Год выпуска : " << getAge() << endl;
cout << " Цвет : " << getCollor() << endl;
cout << " Номер : " << getNomer() << endl;
cout << " Владелец : " << getOwner() << endl;
}
std::ostream& operator<<(std::ostream& out, Auto *automobil) {
automobil->bark();
return out;
}
int Auto::getKey (int key) {
switch (key) {
case 1: {
int size = this->getName().size();
return size;
}
case 2: {
return getModel().size();
}
case 3: {
return getAge();
}
case 4: {
return getNomer();
}
case 5: {
return getOwner().size();
}
default: {
return 0;
}
}
}
#endif |
|
| C++ | 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
| #include <iostream>
#include "CollectionAuto.h"
void copy(Auto **dest,Auto **src, int n) {
int i;
for (i = 0; i < n && src[i] != NULL; i++) {
dest[i] = src[i];
}
for (; i < n; i++) {
dest[i] = NULL;
}
}
Collection::Collection():capacity(5), quantity(0){
elements = new Auto *[capacity];
for (int i = 0; i < capacity; i++) {
elements[i] = NULL;
}
}
Collection::~Collection() {
clear();
}
void Collection::add(Auto *automobil1) {
if (quantity >= capacity) {
capacity *= 2;
copy(elements, elements, capacity);
}
elements[quantity++] = automobil1;
}
void Collection::remove(int position) {
if (position <= quantity) {
quantity--;
if ( quantity*4 < capacity ) {
capacity /= 2;
delete elements[position];
copy(&elements[position], &elements[position + 1], quantity - position);
elements[quantity] = NULL;
} else {
delete elements[position];
copy(&elements[position], &elements[position + 1], quantity - position);
elements[quantity] = NULL;
}
} else {
cout << "ERROR! Unable to remove element from postion " << position << endl;
}
}
void Collection::clear() {
for (int i = 0; i < quantity; i++) {
delete elements[i];
}
delete [] elements;
quantity = 0;
}
Auto *Collection::get(int position) const {
if (position < quantity) {
return elements[position];
} else {
cout << "ERROR!!!" << endl;
return NULL;
}
}
const int Collection::get_quantity() const {
return quantity;
}
void Collection::sort(int key) {
Auto *tmp;
bool swapped = true;
int j = 0;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < quantity - j; i++) {
if ((*elements[i]).getKey(key) > (*elements[i+1]).getKey(key)) {
tmp = elements[i];
elements[i] = elements[i+1];
elements[i+1] = tmp;
swapped = true;
}
}
}
}
Auto *Collection::operator[](int position) {
return this->get(position);
}
Collection& operator-=(Collection& collection, int position) {
collection.remove(position);
return collection;
}
Collection& operator+=(Collection& collection, Auto* automobil) {
collection.add(automobil);
return collection;
}
std::ostream& operator<<(std::ostream& out, const Collection *collection) {
for (int i = 0; i < collection->get_quantity(); i++) {
cout << collection->get(i);
}
return out;
} |
|
| C++ | 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
| #include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "CollectionAuto.cpp"
#include "Auto.cpp"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner){
}
int edt_dlt=0;
int KeySrot=1;
Collection *collection = new Collection();
bool chec(String st){
for(int i=0;i<st.Length();i++){
bool as = false;
for(int j=0;j<10;j++){
if(st.SubString(i,1) ==j)
as=true;
}
if(!as)
return true;
}
return false;
}
void __fastcall TForm1::N7Click(TObject *Sender){
ShowMessage("Вычеслительная практика \nЛабороторная работа №7\n\nВыполнил: Лемеш Максим Михайлович\nГруппа: ЭК-21");
}
void __fastcall TForm1::N6Click(TObject *Sender){
Close();
}
void __fastcall TForm1::BitBtn5Click(TObject *Sender){
Close();
}
void __fastcall TForm1::BitBtn1Click(TObject *Sender){
Form2->Caption = "Добавление";
Form2->BitBtn1->Caption = "Добавить";
Form2->Show();
}
void __fastcall TForm1::FormActivate(TObject *Sender){
try {
if(Form2->E1->Text!="" && Form2->E2->Text!=""
&& Form2->E3->Text!="" && Form2->E4->Text!=""
&& Form2->E5->Text!="" && Form2->E6->Text!=""){
int k = collection->get_quantity();
if(atoi(Form2->E3->Text.c_str())<0 ) throw atoi(Form2->E3->Text.c_str());
if(atoi(Form2->E5->Text.c_str())<0 ) throw atoi(Form2->E5->Text.c_str());
if(chec(Form2->E3->Text)) throw Form2->E3->Text;
if(chec(Form2->E5->Text)) throw Form2->E5->Text;
if (add) {
if (nom==-1) {
Auto *Auto1 = new Auto();
Auto1->setName(Form2->E1->Text.c_str());
Auto1->setModel(Form2->E2->Text.c_str());
Auto1->setAge(StrToInt(Form2->E3->Text));
Auto1->setCollor(Form2->E4->Text.c_str());
Auto1->setNomer(StrToInt(Form2->E5->Text));
Auto1->setOwner(Form2->E6->Text.c_str());
*collection+=Auto1;
for (int i=0; i<collection->get_quantity(); ++i) {
SG1->Cells[0][i+1]=(*collection->get(i)).getName().c_str();
SG1->Cells[1][i+1]=(*collection->get(i)).getModel().c_str();
SG1->Cells[2][i+1]=IntToStr((*collection->get(i)).getAge());
SG1->Cells[3][i+1]=(*collection->get(i)).getCollor().c_str();
SG1->Cells[4][i+1]=IntToStr((*collection->get(i)).getNomer());
SG1->Cells[5][i+1]=(*collection->get(i)).getOwner().c_str();
}
SG1->RowCount=(collection)->get_quantity()+1;
add = false;
} else {
(*collection->get(nom)).setName(Form2->E1->Text.c_str());
(*collection->get(nom)).setModel(Form2->E2->Text.c_str());
(*collection->get(nom)).setAge(StrToInt(Form2->E3->Text));
(*collection->get(nom)).setCollor(Form2->E4->Text.c_str());
(*collection->get(nom)).setNomer(StrToInt(Form2->E5->Text));
(*collection->get(nom)).setOwner(Form2->E6->Text.c_str());
nom=-1;
for (int i=0; i<collection->get_quantity(); ++i) {
SG1->Cells[0][i+1]=(*collection->get(i)).getName().c_str();
SG1->Cells[1][i+1]=(*collection->get(i)).getModel().c_str();
SG1->Cells[2][i+1]=IntToStr((*collection->get(i)).getAge());
SG1->Cells[3][i+1]=(*collection->get(i)).getCollor().c_str();
SG1->Cells[4][i+1]=IntToStr((*collection->get(i)).getNomer());
SG1->Cells[5][i+1]=(*collection->get(i)).getOwner().c_str();
}
}
}
} else {
ShowMessage("Заполните все окна!");
}
}catch(int Auto1){
ShowMessage("введено не верное значение " + IntToStr(Auto1));
}
catch(String Auto1){
ShowMessage("введено не верное значение " + Auto1);
}
}
void __fastcall TForm1::FormCreate(TObject *Sender){
SG1->Cells[0][0]="Марка";
SG1->Cells[1][0]="Модель";
SG1->Cells[2][0]="Год";
SG1->Cells[3][0]="Цвет";
SG1->Cells[4][0]="Номер";
SG1->Cells[5][0]="Владелец";
nom=-1;
}
void __fastcall TForm1::BitBtn3Click(TObject *Sender){
if (collection->get_quantity()>0){
edt_dlt =2;
P2->Visible=true;
E1->SetFocus();
} else ShowMessage("Нет елементов");
}
void __fastcall TForm1::FormDestroy(TObject *Sender){
delete collection;
}
void __fastcall TForm1::BitBtn2Click(TObject *Sender){
if (collection->get_quantity()>0){
E1->Text=IntToStr(SG1->Row);
edt_dlt =1;
P2->Visible=true;
E1->SetFocus();
Form2->BitBtn1->Caption = "Изменить";
Form2->Caption = "Изменение";
} else ShowMessage("Нет елементов");
}
void __fastcall TForm1::BitBtn4Click(TObject *Sender){
if (collection->get_quantity()>0){
if (CB1->ItemIndex==-1) {
CB1->Visible=true;
} else {
switch (CB1->ItemIndex) {
case 0: {
KeySrot=CB1->ItemIndex+1;
break;
}
case 1: {
KeySrot=CB1->ItemIndex+1;
break;
}
case 2: {
KeySrot=CB1->ItemIndex+1;
break;
}
case 3: {
KeySrot=CB1->ItemIndex+1;
break;
}
case 4: {
KeySrot=CB1->ItemIndex+1;
break;
}
case 5: {
KeySrot=CB1->ItemIndex+1;
break;
}
}
collection->sort(KeySrot);
for (int i=0; i<collection->get_quantity(); ++i) {
SG1->Cells[0][i+1]=(*collection->get(i)).getName().c_str();
SG1->Cells[1][i+1]=(*collection->get(i)).getModel().c_str();
SG1->Cells[2][i+1]=IntToStr((*collection->get(i)).getAge());
SG1->Cells[3][i+1]=(*collection->get(i)).getCollor().c_str();
SG1->Cells[4][i+1]=IntToStr((*collection->get(i)).getNomer());
SG1->Cells[5][i+1]=(*collection->get(i)).getOwner().c_str();
}
SG1->RowCount=(collection)->get_quantity()+1;
//CB1->ItemIndex=;
}
} else ShowMessage("Нет елементов");
}
void __fastcall TForm1::SpeedButton1Click(TObject *Sender){
if (collection->get_quantity()>=StrToInt(E1->Text)){
if (edt_dlt==2) {
*collection-=StrToInt(E1->Text)-1;
for (int i=0; i<collection->get_quantity(); ++i) {
SG1->Cells[0][i+1]=(*collection->get(i)).getName().c_str();
SG1->Cells[1][i+1]=(*collection->get(i)).getModel().c_str();
SG1->Cells[2][i+1]=IntToStr((*collection->get(i)).getAge());
SG1->Cells[3][i+1]=(*collection->get(i)).getCollor().c_str();
SG1->Cells[4][i+1]=IntToStr((*collection->get(i)).getNomer());
SG1->Cells[5][i+1]=(*collection->get(i)).getOwner().c_str();
}
SG1->RowCount=(collection)->get_quantity()+1;
P2->Visible=false;
if((collection)->get_quantity() == 0){
SG1->RowCount += 1;
SG1->FixedRows = 1;
SG1->Cells[0][1]="";
SG1->Cells[1][1]="";
SG1->Cells[2][1]="";
SG1->Cells[3][1]="";
SG1->Cells[4][1]="";
SG1->Cells[5][1]="";
}
}
if (edt_dlt==1) {
nom = StrToInt(E1->Text)-1;
Form2->Show();
P2->Visible=false;
Form2->E1->Text=(*collection->get(nom)).getName().c_str();
Form2->E2->Text=(*collection->get(nom)).getModel().c_str();
Form2->E3->Text=IntToStr((*collection->get(nom)).getAge());
Form2->E4->Text=(*collection->get(nom)).getCollor().c_str();
Form2->E5->Text=IntToStr((*collection->get(nom)).getNomer());
Form2->E6->Text=(*collection->get(nom)).getOwner().c_str();
}
} else ShowMessage("Нет елемента");
}
void __fastcall TForm1::N9Click(TObject *Sender){
CB1->ItemIndex=0;
Form1->BitBtn4Click(Sender);
}
void __fastcall TForm1::N10Click(TObject *Sender){
CB1->ItemIndex=1;
Form1->BitBtn4Click(Sender);
}
void __fastcall TForm1::N11Click(TObject *Sender){
CB1->ItemIndex=2;
Form1->BitBtn4Click(Sender);
}
void __fastcall TForm1::N12Click(TObject *Sender){
CB1->ItemIndex=3;
Form1->BitBtn4Click(Sender);
}
void __fastcall TForm1::N13Click(TObject *Sender){
CB1->ItemIndex=4;
Form1->BitBtn4Click(Sender);
}
void __fastcall TForm1::SaveClick(TObject *Sender){
if (collection->get_quantity() == 0){
ShowMessage("Коллекция пуста");
return;
}
SD1->Filter = "Текстовые файлы|*.txt";
if(SD1->Execute()){
TStringList *pStrings = new TStringList;
for(int i=1; i < SG1->RowCount; i++){
pStrings->Add(SG1->Cells[0][i] + ";"
+ SG1->Cells[1][i] + ";"
+ SG1->Cells[2][i] + ";"
+ SG1->Cells[3][i] + ";"
+ SG1->Cells[4][i] + ";"
+ SG1->Cells[5][i]);
}
pStrings->SaveToFile(SD1->FileName);
delete pStrings;
}
}
void __fastcall TForm1::OpenClick(TObject *Sender){
for(int i=0;i<collection->get_quantity();i++)
collection->remove(i);
OD1->Filter = "Текстовые файлы|*.txt";
if(OD1->Execute()){
TStringList *pStrings = new TStringList;
pStrings->LoadFromFile(OD1->FileName);
SG1->RowCount = pStrings->Count+1;
SG1->ColCount=6;
for(int i=0; i < pStrings->Count; i++){
SG1->Rows[i+1]->Delimiter = ';';
SG1->Rows[i+1]->DelimitedText = pStrings->Strings[i];
}
delete pStrings;
}
try{
for(int i = 1;i < SG1->RowCount;i++){
string name = SG1->Cells[0][i].c_str();
string model = SG1->Cells[1][i].c_str();
int age = SG1->Cells[2][i].ToInt();
string collor = SG1->Cells[3][i].c_str();
int nomer = SG1->Cells[4][i].ToInt();
string owner = SG1->Cells[5][i].c_str();
Auto *auto1=new Auto(name,age,owner,collor,nomer,model);
*collection+=auto1;
}
}catch (int age){
ShowMessage("dfsdfdsf");
}
}
void __fastcall TForm1::E1KeyPress(TObject *Sender, char &Key){
char *allow = "1234567890-," ;
bool ok = false;
for ( unsigned i =0; i < strlen(allow); ++i) {
if (Key == allow[i])
ok = true;
}
if (!ok){
Key = '\0';
ShowMessage("Введдите цифры");
}
} |
|
| C++ | 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
| #include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
__fastcall TForm2::TForm2(TComponent* Owner): TForm(Owner){}
void __fastcall TForm2::BitBtn2Click(TObject *Sender){
Form1->add = false;
Form2->Visible = false;
Form1->Show();
}
void __fastcall TForm2::BitBtn1Click(TObject *Sender){
Form1->add = true;
Form2->Visible = false;
Form1->Show();
}
void __fastcall TForm2::E3KeyPress(TObject *Sender, char &Key){
char *allow = "1234567890-," ;
bool ok = false;
for ( unsigned i =0; i < strlen(allow); ++i) {
if (Key == allow[i])
ok = true;
}
if (!ok){
Key = '\0';
ShowMessage("Введдите цифры");
}
} |
|
0
|