Работа со структурой! - C++ - Ответ 5423621
02.12.2013, 19:41. Показов 487. Ответов 0
Итак необходимо создать структуру, заполнить ее, вывести на экран или в файл, затем отсортировать по выбраному параметру(ну там вес, рост и тд...по возрастанию или убыванию). Ну дак вот все написал) почти все работает!Вот код:
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
| #include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Man
{
string fio;
int date;
double stat,weight;
char gen;
};
void Init(Man *people,int n)
{
char gent,Mas_Char[30];
for (int i=0;i<n;i++)
{
cin.sync();
cout <<"\nВведите ФИО: ";
cin.getline(Mas_Char,30);
people[i].fio=Mas_Char;
cout <<"Введите год рождения: ";
cin >>people[i].date;
cout <<"Введите рост человека: ";
cin >>people[i].stat;
cout <<"Введите вес человека: ";
cin >>people[i].weight;
cout <<"Введите пол человека ""м"" или ""ж"": ";
label:cin >>gent;
if ((gent=='м')||(gent=='ж'))
people[i].gen=gent;
else
{
cout <<"Введите ""м"" или ""ж"": ";
goto label;
}
}
}
void Print(Man *people,int n)
{
fstream file;
file.open("people.txt",ios::app);
if (file)
{
for (int i=0;i<n;i++)
{
file <<"\n"<<"ФИО человека : "<<people[i].fio<<"\n";
file <<"Год рождения : "<<people[i].date<<"\n";
file <<"Рост : "<<people[i].stat<<"\n";
file <<"Вес : "<<people[i].weight<<"\n";
file <<"Пол : "<<people[i].gen<<"\n";
}
}
else cout <<"Файл не может быть открыт или создан!";
file.close();
}
void Display(Man *people, int n)
{
for(int i=0;i<n;i++)
{
cout <<"ФИО человека : "<<people[i].fio<<"\n";
cout <<"Год рождения : "<<people[i].date<<"\n";
cout <<"Рост : "<<people[i].stat<<"\n";
cout <<"Вес : "<<people[i].weight<<"\n";
cout <<"Пол : "<<people[i].gen<<"\n";
}
}
Man* Sort(Man *people, int n, int z, int priz)
{
int k;
Man buff;
if (z=0)
switch (priz)
{
case (0):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].weight > people[j].weight)
k=j;
}
buff=people[i];
people[i]=people[k];
people[k]=buff;
}
}Display(people,n);break;
case (1):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].stat > people[j].stat)
k=j;
}
buff=people[i];
people[i]=people[k];
people[k]=buff;
}
}Display(people,n);break;
case (2):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].date < people[j].date)
k=j;
}
buff=people[i];
people[i]=people[k];
people[k]=buff;
}
}Display(people,n);break;
}
if (z=1)
switch (priz)
{
case (0):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].weight < people[j].weight)
k=j;
}
buff=people[i];
people[i]=people[k];
people[k]=buff;
}
}Display(people,n);break;
case (1):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].stat < people[j].stat)
k=j;
}
buff=people[i];
people[i]=people[k];
people[k]=buff;
}
}Display(people,n);break;
case (2):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].date > people[j].date)
k=j;
}
buff=people[i];
people[i]=people[k];
people[k]=buff;
}
}Display(people,n);break;
}
return people;
}
int main()
{
setlocale(LC_ALL,"Russian");
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
int n,poss,poss1,i;
cout <<"Введите количество человек: ";
cin >>n;
Man* people = new Man[n];
Man tmp;
Init(people,n);
system("cls");
lab:cout <<"\nВыберите:";
cout <<"\n0---------->Вывод на экран";
cout <<"\n1---------->Вывод в файл";
cout <<"\n2---------->Выход!";
cout <<"\n---->";cin >>poss;
switch (poss)
{
case (0):
Display(people,n);
break;
case (1):
Print(people,n);
break;
case (2):
exit(0);
break;
}
cout <<"\nВыберите метод сортировки:";
cout <<"\n0---------->Убывание";
cout <<"\n1---------->Возрастание";
cout <<"\n---->";cin >>poss;
cout <<"\nВыберите метод сортировки:";
cout <<"\n0---------->По Возрасту";
cout <<"\n1---------->По Росту";
cout <<"\n2---------->По Весу";
cout <<"\n---->";cin >>poss1;
people=Sort(people,n,poss,poss1);
goto lab;
system("pause");
return 0;
} |
|
Вот только сортировка не пашет, не могу понять почему, точнее не знаю правильно ли я работаю с элементами структуры в функции сортировки! Вывод на экран в сортировке вставил для проверки, думал функция не передаёт значение. Помогите пожалуйста...
Добавлено через 27 минут
Да правда еще перепутал сортировку по убыванию с сортировкой по возрастанию, уже поменял...
Добавлено через 6 минут
Исправил ошибку в If (условие) там где было z=0 и z=1, все равно не пашет...
Добавлено через 14 минут
Жаль что никто не ответил конечно) Но все уже решил Все работает... Проблема была в том что не получалось присвоить структурную переменную целиком, пришлось прописывать каждое поле по отдельности, если кто может ответьте пожалуйста... Ведь все таки структура это единое целое (переменная) почему нельзя было присваивать просто переменные одного типа(одной и той же структуры)
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
| #include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Man
{
string fio;
int date;
double stat,weight;
char gen;
};
void Init(Man *people,int n)
{
char gent,Mas_Char[30];
for (int i=0;i<n;i++)
{
cin.sync();
cout <<"\nВведите ФИО: ";
cin.getline(Mas_Char,30);
people[i].fio=Mas_Char;
cout <<"Введите год рождения: ";
cin >>people[i].date;
cout <<"Введите рост человека: ";
cin >>people[i].stat;
cout <<"Введите вес человека: ";
cin >>people[i].weight;
cout <<"Введите пол человека ""м"" или ""ж"": ";
label:cin >>gent;
if ((gent=='м')||(gent=='ж'))
people[i].gen=gent;
else
{
cout <<"Введите ""м"" или ""ж"": ";
goto label;
}
}
}
void Print(Man *people,int n)
{
fstream file;
file.open("people.txt",ios::app);
if (file)
{
for (int i=0;i<n;i++)
{
file <<"\n"<<"ФИО человека : "<<people[i].fio<<"\n";
file <<"Год рождения : "<<people[i].date<<"\n";
file <<"Рост : "<<people[i].stat<<"\n";
file <<"Вес : "<<people[i].weight<<"\n";
file <<"Пол : "<<people[i].gen<<"\n";
}
}
else cout <<"Файл не может быть открыт или создан!";
file.close();
}
void Display(Man *people, int n)
{
for(int i=0;i<n;i++)
{
cout <<"\nФИО человека : "<<people[i].fio<<"\n";
cout <<"Год рождения : "<<people[i].date<<"\n";
cout <<"Рост : "<<people[i].stat<<"\n";
cout <<"Вес : "<<people[i].weight<<"\n";
cout <<"Пол : "<<people[i].gen<<"\n";
}
}
Man* Sort(Man *people, int n, int z, int priz)
{
int k;
Man buff;
if (z==0)
switch (priz)
{
case (0):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].weight > people[j].weight)
k=j;
}
buff.fio=people[i].fio;
buff.date=people[i].date;
buff.stat=people[i].stat;
buff.weight=people[i].weight;
buff.gen=people[i].gen;
people[i].fio=people[k].fio;
people[i].date=people[k].date;
people[i].stat=people[k].stat;
people[i].weight=people[k].weight;
people[i].gen=people[k].gen;
people[k].fio=buff.fio;
people[k].date=buff.date;
people[k].stat=buff.stat;
people[k].weight=buff.weight;
people[k].gen=buff.gen;
}
}Display(people,n);break;
case (1):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].stat > people[j].stat)
k=j;
}
buff.fio=people[i].fio;
buff.date=people[i].date;
buff.stat=people[i].stat;
buff.weight=people[i].weight;
buff.gen=people[i].gen;
people[i].fio=people[k].fio;
people[i].date=people[k].date;
people[i].stat=people[k].stat;
people[i].weight=people[k].weight;
people[i].gen=people[k].gen;
people[k].fio=buff.fio;
people[k].date=buff.date;
people[k].stat=buff.stat;
people[k].weight=buff.weight;
people[k].gen=buff.gen;
}
}Display(people,n);break;
case (2):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].date < people[j].date)
k=j;
}
buff.fio=people[i].fio;
buff.date=people[i].date;
buff.stat=people[i].stat;
buff.weight=people[i].weight;
buff.gen=people[i].gen;
people[i].fio=people[k].fio;
people[i].date=people[k].date;
people[i].stat=people[k].stat;
people[i].weight=people[k].weight;
people[i].gen=people[k].gen;
people[k].fio=buff.fio;
people[k].date=buff.date;
people[k].stat=buff.stat;
people[k].weight=buff.weight;
people[k].gen=buff.gen;
}
}Display(people,n);break;
}
if (z==1)
switch (priz)
{
case (0):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].weight < people[j].weight)
k=j;
}
buff.fio=people[i].fio;
buff.date=people[i].date;
buff.stat=people[i].stat;
buff.weight=people[i].weight;
buff.gen=people[i].gen;
people[i].fio=people[k].fio;
people[i].date=people[k].date;
people[i].stat=people[k].stat;
people[i].weight=people[k].weight;
people[i].gen=people[k].gen;
people[k].fio=buff.fio;
people[k].date=buff.date;
people[k].stat=buff.stat;
people[k].weight=buff.weight;
people[k].gen=buff.gen;
}
}Display(people,n);break;
case (1):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].stat < people[j].stat)
k=j;
}
buff.fio=people[i].fio;
buff.date=people[i].date;
buff.stat=people[i].stat;
buff.weight=people[i].weight;
buff.gen=people[i].gen;
people[i].fio=people[k].fio;
people[i].date=people[k].date;
people[i].stat=people[k].stat;
people[i].weight=people[k].weight;
people[i].gen=people[k].gen;
people[k].fio=buff.fio;
people[k].date=buff.date;
people[k].stat=buff.stat;
people[k].weight=buff.weight;
people[k].gen=buff.gen;
}
}Display(people,n);break;
case (2):{
for(int i=0;i<n-1;i++)
{
k=i;
for(int j=i+1;j<n;j++)
{
if (people[k].date > people[j].date)
k=j;
}
buff.fio=people[i].fio;
buff.date=people[i].date;
buff.stat=people[i].stat;
buff.weight=people[i].weight;
buff.gen=people[i].gen;
people[i].fio=people[k].fio;
people[i].date=people[k].date;
people[i].stat=people[k].stat;
people[i].weight=people[k].weight;
people[i].gen=people[k].gen;
people[k].fio=buff.fio;
people[k].date=buff.date;
people[k].stat=buff.stat;
people[k].weight=buff.weight;
people[k].gen=buff.gen;
}
}Display(people,n);break;
}
return people;
}
int main()
{
setlocale(LC_ALL,"Russian");
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
int n,poss,poss1,i;
cout <<"Введите количество человек: ";
cin >>n;
Man* people = new Man[n];
Man tmp;
Init(people,n);
system("cls");
lab:cout <<"\nВыберите:";
cout <<"\n0---------->Вывод на экран";
cout <<"\n1---------->Вывод в файл";
cout <<"\n2---------->Выход!";
cout <<"\n---->";cin >>poss;
switch (poss)
{
case (0):
Display(people,n);
break;
case (1):
Print(people,n);
break;
case (2):
exit(0);
break;
}
cout <<"\nВыберите метод сортировки:";
cout <<"\n0---------->По возростанию";
cout <<"\n1---------->По убыванию";
cout <<"\n---->";cin >>poss;
cout <<"\nВыберите метод сортировки:";
cout <<"\n0---------->По Возрасту";
cout <<"\n1---------->По Росту";
cout <<"\n2---------->По Весу";
cout <<"\n---->";cin >>poss1;
people=Sort(people,n,poss,poss1);
goto lab;
system("pause");
return 0;
} |
|
Вернуться к обсуждению: Работа со структурой! C++
0
|