07.01.2020, 18:51. Показов 378. Ответов 0
Есть следующее задание:
1. Создать запись для хранения следующей информации:
– код владельца,
– номер автомобиля,
– марка автомобиля,
– дата выпуска,
– дата регистрации.
2. Предусмотреть возможность добавления, изменения и удаление записи и отображения
данных на экран. Для хранения данных использовать стек.
3. Получить список номеров и марок автомобилей, зарегистрированных в ноябре и декабре
прошлого года.
4. Найти средний возраст по каждой из встречающихся марок автомобилей.
5. Найти "возраст" с точностью до года каждого из автомобилей, зарегистрированных в
феврале и марте текущего года.
|
Комментарий модератора |
 |
П.5.16.Правил
Запрещено создавать темы с множеством вопросов во всех разделах, кроме разделов платных услуг. Один вопрос - одна тема. |
|
Мне нужно сделать так, чтобы удалялась и изменялась указанная мной запись(вторая, пятая итп), а у меня удаляется последняя добавленная. Помогите пожалуйста это сделать.
| 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
| #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; // указатель на структуру informations
} typedef Informations;
Informations* top = nullptr; //nullptr=NULL=0. Прим: но nullptr лучше передавать вместо 0/NULL в функцию если нужен указатель
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()//изменение
{
cin.get();
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;
}
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 << " " << i << 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->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);
} |
|