Есть следующее задание :
1. Создать запись для хранения следующей информации:
– код владельца,
– номер автомобиля,
– марка автомобиля,
– дата выпуска,
– дата регистрации.
2. Предусмотреть возможность добавления, изменения и удаление записи и отображения
данных на экран. Для хранения данных использовать стек.
3. Получить список номеров и марок автомобилей, зарегистрированных в ноябре и декабре
прошлого года.
4. Найти средний возраст по каждой из встречающихся марок автомобилей.
5. Найти "возраст" с точностью до года каждого из автомобилей, зарегистрированных в
феврале и марте текущего года.
Нужно реализовать изменение данных в стеке( void change() )
Прикинул, что можно сделать так:
1. Ввести код владельца авто
2. Пока верхняя "структура" не равна коду владельца(грубо говоря- пока не найду то, что нужно изменить), переместить ее в другой, временный, стек.
3. Когда найду соответствующую запись - изменю данные.
4. Перемещу элементы из временного стека обратно, в "основной"
Попытался выполнить, но в 108-й строке выдает следующую ошибку: "ConsoleApplication1.exe вызвал срабатывание точки останова."
Что можно сделать или переделать в этом случае?
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
321
322
| #include "pch.h"
#include <iostream>
#include <string>
using namespace std;
struct DATE
{
int number;
int month;
int year;
};
struct informations
{
char code[10];//код владельца
char number_car[10];//номер автомобиля
char mark[10];//марка автомобиля
DATE release;//дата выпуска
DATE registration;//дата регистрации
informations* next; // указатель на след. элемент
} typedef Informations;
struct stack2
{
char code[10];//код владельца
char number_car[10];//номер автомобиля
char mark[10];//марка автомобиля
DATE release;//дата выпуска
DATE registration;//дата регистрации
stack2* next; // указатель на след. элемент
} typedef Stack2;
Informations* top = nullptr; //nullptr=NULL=0. Прим: но nullptr лучше передавать вместо 0/NULL в функцию если нужен указатель
Stack2* top2 = nullptr;
void push() //ввод
{
Informations* ptr = new Informations; // объявление новой динамической переменной
cin.get();
cout << "Enter the code owner: ";
cin.getline(ptr->code, 10); //обращение через указатель к полю code
cout << "Enter the number of the car: ";
cin.getline(ptr->number_car, 10);
cout << "Enter the mark of the car: ";
cin.getline(ptr->mark, 10);
cout << "Enter the date of release: " << endl;
do {
cout << "number: ";
cin >> ptr->release.number;
} while ((ptr->release.number > 31) || (ptr->release.number <= 0)); //писать дату, пока она не будет в пределах от 1 до 31
do {
cout << "month: ";
cin >> ptr->release.month;
} while ((ptr->release.month > 12) || (ptr->release.month <= 0)); //писать месяц, пока он не будет в пределах от 1 до 12
do {
cout << "year: ";
cin >> ptr->release.year;
} while ((ptr->release.year > 2019) || (ptr->release.year <= 1980)); //писать год, пока он не будет в пределах от 1980 до 2019
cout << "Enter the date of registration: " << endl;
do {
cout << "number: ";
cin >> ptr->registration.number;
} while ((ptr->registration.number > 31) || (ptr->registration.number <= 0)); //писать дату, пока она не будет в пределах от 1 до 31
do {
cout << "month: ";
cin >> ptr->registration.month;
} while ((ptr->registration.month > 12) || (ptr->registration.month <= 0)); //писать месяц, пока он не будет в пределах от 1 до 12
do {
cout << "year: ";
cin >> ptr->registration.year;
} while ((ptr->registration.year > 2019) || (ptr->registration.year <= 1980)); //писать год, пока он не будет в пределах от 1980 до 201
cout << endl;
ptr->next = top; // достаем указатель на следующий элемент *next и заменяем его на указатель, который указывает на вершину стека *top
top = ptr; // указатель того, что текущий элемент - вершина стека
}
void change()//изменение
{
//Процесс реализации - если запись не совпадает с той, которую нужно изменить, то верхнюю запись(top)
//переместить во второй(временный) стек и назначить следующую запись верхней(top = top->next)
Stack2* s2 = new Stack2;
char n[10]; //номер владельца автомобиля
cout << "Enter the number of the car: ";
cin >> n;
while (n != top->code) // пока изменяемая структура не является вершиной
{
auto udalit = top;
s2->code[10] = top->code[10];
s2->number_car[10] = top->number_car[10];
s2->mark[10] = top->mark[10];
s2->release.number = top->release.number;
s2->release.month = top->release.month;
s2->release.year = top->release.year;
s2->registration.number = top->registration.number;
s2->registration.month = top->registration.month;
s2->registration.year = top->registration.year;
s2->next = top2;// достаем указатель на следующий элемент *next и заменяем его на указатель, который указывает на вершину стека *top
top2 = s2;// указатель того, что текущий элемент - вершина стека
top->next = top;//достаем указатель на следующий элемент *next и заменяем его на указатель, который указывает на вершину стека *top
delete udalit;
}
if (n == top->code)
{
cout << "Enter the code owner: ";
cin.getline(top->code, 10); // изменение верхнего элемента
cout << "Enter the number of the car: ";
cin.getline(top->number_car, 10);
cout << "Enter the mark of the car: ";
cin.getline(top->mark, 10);
cout << "Enter the date of release:" << endl;
do {
cout << "number: ";
cin >> top->release.number;
} while ((top->release.number > 31) || (top->release.number <= 0));
do {
cout << "month: ";
cin >> top->release.month;
} while ((top->release.month > 12) || (top->release.month <= 0));
do {
cout << "year: ";
cin >> top->release.year;
} while ((top->release.year > 2019) || (top->release.year <= 1980));
cout << "Enter the date of registration: " << endl;
do {
cout << "number: ";
cin >> top->registration.number;
} while ((top->registration.number > 31) || (top->registration.number <= 0));
do {
cout << "month: ";
cin >> top->registration.month;
} while ((top->registration.month > 12) || (top->registration.month <= 0));
do {
cout << "year: ";
cin >> top->registration.year;
} while ((top->registration.year > 2019) || (top->registration.year <= 1980));
cout << endl;
}
while (s2 != NULL)
{
top->code[10] = s2->code[10];
top->number_car[10] = s2->number_car[10];
top->mark[10] = s2->mark[10];
top->release.number = s2->release.number;
top->release.month = s2->release.month;
top->release.year = s2->release.year;
top->registration.number = s2->registration.number;
top->registration.month = s2->registration.month;
top->registration.year = s2->registration.year;
s2->next = top2;// достаем указатель на следующий элемент *next и заменяем его на указатель, который указывает на вершину стека *top
top2 = s2;// указатель того, что текущий элемент - вершина стека
top->next = top;//достаем указатель на следующий элемент *next и заменяем его на указатель, который указывает на вершину стека *top
top = top;//указатель того, что текущий элемент - вершина стека
}
}
void pop() //удаление
{
auto bad = top; //автоматическое определение типа переменной
top = top->next; //назначение следующего элемента верхним
delete bad; //удаление верхнего элемента
}
void show_stack() //просмотр элем стека
{
//int i = 0;
cout << endl;
Informations* ptr = top;
while (ptr != NULL) //пока указатель не пустой
{
//i++;
cout << "-----------------------" << endl;
cout << "The code owner: " << ptr->code << endl; // вывести последний добавленный элемент
cout << "The number of the car: " << ptr->number_car << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << "The date of release:\nnumber:\t\t\t\t" << ptr->release.number << endl;
cout << "month:\t\t\t\t" << ptr->release.month << endl; // /t-отступ
cout << "year:\t\t\t\t" << ptr->release.year << endl;
cout << endl;
cout << "The date of registration:\nnumber:\t\t\t\t" << ptr->registration.number << endl;
cout << "month:\t\t\t\t" << ptr->registration.month << endl;
cout << "year:\t\t\t\t" << ptr->registration.year << endl;
cout << endl;
ptr = ptr->next; // перейти к следующему элементу в стеке
}
}
void November_and_December() //ноябрь и декабрь
{
Informations* ptr = top;
while (ptr != NULL) //пока указатель не пустой
{
if ((ptr->registration.month == 11) || (ptr->registration.month == 12))
{
if (ptr->registration.year == 2018)
{
cout << "The number of the car: " << ptr->number_car << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next; // перейти к следующему элементу
}
else cout << endl;
}
}
}
void Average_age()
{
Informations* ptr = top;
int i = 0;
int averageyear = 0;
while (ptr != NULL) //пока указатель не пустой
{
i++;
averageyear += (ptr->registration.year - ptr->release.year);
ptr = ptr->next; // перейти к следующему элементу
}
double average = averageyear / i;
cout << average << endl;
}
void February_and_March() // февраль и март
{
int vozrast;
Informations* ptr = top;
while (ptr != NULL) //пока указатель не пустой
{
if (((ptr->registration.month == 2) || (ptr->registration.month == 3)) && (ptr->registration.year == 2019)) //проверка на март или февраль 2019 года
{
vozrast = (ptr->registration.year) - (ptr->release.year); // найти возраст автомобиля, не считая месяцы и числа месяца
if ((ptr->registration.month) < (ptr->release.month)) //если месяц выпуска > мсеяца регистрации
{
vozrast = vozrast - 1;
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next; // перейти к следующему элементу
}
else if ((ptr->registration.month) == (ptr->release.month))
{
if ((ptr->registration.number) < (ptr->release.number))
{
vozrast = vozrast - 1;
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next; // перейти к следующему элементу
}
else
{
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next; // перейти к следующему элементу
}
}
}
else {
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next; // перейти к следующему элементу
}
}
}
int main()
{
int l;
do
{
cout << "--------------------------------------";
cout << endl;
cout << " 1)Enter " << endl << " 2)Change " << endl << " 3)Delete " << endl << " 4)Show " << endl << " 5)November and December " << endl << " 6)Average age" << endl << " 7)February and March" << endl << " 8)Exit " << endl;
cin >> l;
switch (l)
{
case 1:push(); break;
case 2: {
if (top != NULL) change(); break; //если указатель не пустой
}
case 3:
{
if (top != NULL) pop(); break;
}
case 4: show_stack(); break;
case 5:
{
if (top != NULL) November_and_December(); break;
}
case 6: if (top != NULL) Average_age(); break;
case 7:
{
if (top != NULL) February_and_March(); break;
}
default:break; //если проверяемое значение равно 8, то выйти из функции
}
} while (l != 8);
} |
|