-
Notifications
You must be signed in to change notification settings - Fork 0
/
DishClass.cpp
301 lines (273 loc) · 7.22 KB
/
DishClass.cpp
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
#pragma once
#include "DishClass.h"
string dish_path = "dish.txt";
DishClass::DishClass() {
head = new DishNOde();
length = 0;
fstream file;
file.open(dish_path, ios::in);
if (!file.is_open()) {
cout << "read error!" << endl;
return;
}
Dish dish;
DishNOde* p = head;
//文件中读取数据
while (!file.eof()) {
file >> dish.id >> dish.name >> dish.price >> dish.score >> dish.scnum >> dish.sales;
// if (file.good()) {
if (file.good()) {
p->next = new DishNOde(dish);
p = p->next;
last = p;
length++;
}
// }
}
file.close();
}
bool DishClass::OutToFile(DishNOde* p, string str) {//写到文件中
fstream file;
file.open(str, ios::out | ios::app);
if (!file.is_open()) {
cout << "failed" << endl;
return false;
}
file << p->dish_inf.id << " " << p->dish_inf.name << " " << p->dish_inf.price << " " <<
p->dish_inf.score << " " << p->dish_inf.scnum << " " << p->dish_inf.sales << endl;
return true;
}
DishNOde* DishClass::SortBysorce(DishNOde* start)
{
if (start == NULL || start->next == NULL) return start;
DishNOde* slow = start;
DishNOde* fast = start->next;
//寻找断开点
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
//递归分割
DishNOde* right = SortBysorce(slow->next);
slow->next = NULL;//分割
DishNOde* left = SortBysorce(start);
return mergeBySorce(left, right);
}
DishNOde* DishClass::mergeBySorce(DishNOde* left, DishNOde* right)
{
DishNOde temp, * q;
q = &temp;
//归并
while (left != NULL && right != NULL) {
if (left->dish_inf.score >= right->dish_inf.score) {
q->next = left;
left = left->next;
}
else {
q->next = right;
right = right->next;
}
q = q->next;
}
if (left != NULL) q->next = left;
if (right != NULL) q->next = right;
return temp.next;
}
DishNOde* DishClass::SortBySales(DishNOde* start)
{
if (start == NULL || start->next == NULL) return start;
DishNOde* slow = start;
DishNOde* fast = start->next;
//寻找断开点
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
//递归分割
DishNOde* right = SortBySales(slow->next);
slow->next = NULL;//分割
DishNOde* left = SortBySales(start);
return mergeBySales(left, right);
}
DishNOde* DishClass::mergeBySales(DishNOde* left, DishNOde* right)
{
DishNOde temp, * q;
q = &temp;
//归并
while (left != NULL && right != NULL) {
if (left->dish_inf.sales >= right->dish_inf.sales) {
q->next = left;
left = left->next;
}
else {
q->next = right;
right = right->next;
}
q = q->next;
}
if (left != NULL) q->next = left;
if (right != NULL) q->next = right;
return temp.next;
}
void DishClass::print(Dish dish) {
char str_id[3], str_price[5], str_score[5], str_scum[5], str_sales[5];
sprintf_s(str_id, "%d", dish.id);
sprintf_s(str_price, "%.1f", dish.price);
sprintf_s(str_score, "%.1f", dish.score);
sprintf_s(str_scum, "%d", dish.scnum);
sprintf_s(str_sales, "%d", dish.sales);
//string str = str_id;
cout << "菜品号:" << str_id << " 菜品名:";
cout << dish.name << " 菜品价格:" << str_price << " 菜品评分:"
<< str_score << " 评分人数:" << str_scum << " 销量:" << str_sales << endl;
}
DishClass::~DishClass() {
fstream file;
file.open(dish_path, ios::out | ios::trunc);
if (!file.is_open()) {
cout << "---------open failed--------" << endl;
return;
}
file.close();
//重新写入文件
DishNOde* q = head->next;//工作指针
for (int i = 0; i < length && q != NULL; q = q->next) {
OutToFile(q, dish_path);
}
}
bool DishClass::GetID(int id, Dish*& dish) {//通过ID查询菜谱
DishNOde* p = new DishNOde;
p = head->next;
for (int i = 0; i < length; i++) {
if (id == p->dish_inf.id) {
dish = p->GetDish();
print(p->dish_inf);
return true;
}
p = p->next;
}
return false;
}
bool DishClass::GetByName(string name, Dish*& dish)
{
DishNOde* p = new DishNOde;
p = head->next;
for (int i = 0; i < length; i++) {
if (!name.compare(p->dish_inf.name)) {
dish = p->GetDish();
print(p->dish_inf);
return true;
}
p = p->next;
}
return false;
}
bool DishClass::AddDish(Dish dish) {//增加菜单
DishNOde* p = new DishNOde(dish);
Dish *temp = new Dish;
if (GetID(dish.id, temp))
{
delete temp;
return 0;
}
last->next = p;
last = p;
length++;
delete p;
delete temp;
return true;
}
bool DishClass::DeleteDish(int id) {//删除菜品
DishNOde* p;
DishNOde* q;
q = head;
p = head->next;
for (int i = 0; i < length; i++) {
if (id == p->dish_inf.id) {
q->next = p->next;
p->next = NULL;
length--;
return true;
}
p = p->next;
q = q->next;
}
return false;
}
bool DishClass::UpdataScore(int id, int addSorce) {//更新评分
DishNOde* p = new DishNOde;
p = head->next;
for (int i = 0; i < length; i++) {
if (id == p->dish_inf.id) {
int oldScore;
oldScore = p->GetDish()->score;
p->GetDish()->score = (oldScore * p->GetDish()->scnum + addSorce) / (++p->GetDish()->scnum);
return true;
}
p = p->next;
}
return false;
}
bool DishClass::UpdataSales(int id)
{
DishNOde* p = new DishNOde;
p = head->next;
for (int i = 0; i < length; i++) {
if (id == p->dish_inf.id) {
p->GetDish()->sales++;
return true;
}
p = p->next;
}
return false;
}
void DishClass::ShowAll() {
DishNOde* p;
p = head->next;
for (int i = 0; i < length; i++) {
print(p->dish_inf);
p = p->next;
}
}
bool DishClass::UpdataName(int id, string name) {
DishNOde* p = new DishNOde;
p = head->next;
for (int i = 0; i < length; i++) {
if (id == p->dish_inf.id) {
p->change(name);
return true;
}
p = p->next;
}
return false;
}
bool DishClass::UpdataPrice(int id, float price) {
DishNOde* p = new DishNOde;
p = head->next;
for (int i = 0; i < length; i++) {
if (id == p->dish_inf.id) {
p->GetDish()->price = price;
return true;
}
p = p->next;
}
return false;
}
bool DishClass::ShowByScore()
{
DishNOde* p = SortBysorce(head->next);
if (p == NULL || p->next == NULL) cout << "-------------菜谱上没有菜------------" << endl;
head->next = p;
cout << "------------结果如下------------" << endl;
ShowAll();
return 1;
}
bool DishClass::ShowBySales()
{
DishNOde* p = SortBySales(head->next);
if (p == NULL || p->next == NULL) cout << "-------------菜谱上没有菜------------" << endl;
head->next = p;
cout << "------------结果如下------------" << endl;
ShowAll();
return 1;
}