-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.h
366 lines (331 loc) · 9.81 KB
/
book.h
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <bits/stdc++.h>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#define MAXSIZE 10000
#define MAX_LINE 1024
typedef struct Book
{
string no;
string name;
double price;
}Book;
typedef struct Node {
Book data;
Node* before;
Node* next;
}Node;
class List {
public:
List() {
head = new Node;
head->next = NULL;
head->before = NULL;
head->data.price = 0.0;
number = 0;
}
void scan();
void show();
void shownumber();
void insertion(int place);
void deletion(const string& name);
void deletion(const int& place);
void invertion();
void mergesort();
void division(Node* first1, Node*& first2);
void recmergesort(Node*& firsthead);
void search(const int place);
void search(const string name);
void update();
void fileprint();
Node* merge(Node* first1, Node* first2);
private:
Node* head;
int number;
};
void List::insertion(int place) {
Node* newnode = new Node;
Node* current = head;
int i = 1;
double price;
string no, name;
cout << "please input the book's no, book's name and book's price in order. \n";
cin >> no >> name >> price;
newnode->data.no = no;
newnode->data.name = name;
newnode->data.price = price;
while (current->next != NULL && i != place) {
current = current->next;
i++;
}
newnode->next = current->next;
current->next = newnode;
number++;
fileprint();
}
void List::scan() {
Node* current = head;
FILE* fp;
errno_t err = fopen_s(&fp, "book.txt", "r");
if (fp == NULL) {
fprintf(stderr, "Can't open the file.\n");
exit(1);
}
char no[32], name[100];
double price;
char buffer[MAX_LINE];
for (int i = 0; i < 2; i++) {
if (fgets(buffer, MAX_LINE, fp) == NULL) {
exit(1);
}
printf("%s", buffer);
}
while (fgets(buffer, MAX_LINE, fp)) {
Node* newnode = new Node;
if (sscanf_s(buffer, "%s %s %lf", no, 32, name, 100, &price) == 3) {
for (int i = 0; i < strlen(no); i++) newnode->data.no += no[i];
for (int j = 0; j < strlen(name); j++) newnode->data.name += name[j];
newnode->data.price = price;
newnode->next = current->next;
current->next = newnode;
current = current->next;
number++;
}
else {
fprintf(stderr, "Unseccessful %s", buffer);
}
}
fclose(fp);
show();
}
void List::deletion(const string& name) {
Node* auxpointer = head, * pointer = head->next;
int i = 1;
while (pointer != NULL) {
if (pointer->data.name == name && auxpointer != head && i <= number) {
auxpointer->next = pointer->next;
pointer->next->before = auxpointer;
delete pointer;
number--;
}
else if (pointer->data.name == name && auxpointer == head && i <= number) {
auxpointer->next = pointer->next;
pointer->next->before = auxpointer;
delete pointer;
number--;
}
else if (i > number) break;
pointer = pointer->next;
auxpointer = auxpointer->next;
}
fileprint();
}
void List::deletion(const int& place) {
Node* auxpointer = head, * pointer = head->next;
int i = 1;
if (pointer == NULL)
return;
else {
while (pointer != NULL) {
if (i == place && auxpointer != head && i <= number) {
auxpointer->next = pointer->next;
pointer->next->before = auxpointer;
delete pointer;
number--;
break;
}
else if (i == place && auxpointer == head && i <= number) {
auxpointer->next = pointer->next;
pointer->next->before = auxpointer;
number--;
break;
}
else if (i > number) break;
i++;
pointer = pointer->next;
auxpointer = auxpointer->next;
}
}
fileprint();
}
void List::invertion() {
Node* auxpointer = head, * pointer = head->next;
if (pointer == NULL)
auxpointer->before = pointer->before;
else {
while (pointer != NULL) {
pointer->before = auxpointer;
pointer = pointer->next;
auxpointer = auxpointer->next;
}
}
ofstream fout("book-newinverse.txt");
if (!fout) {
cerr << "Can't open the file." << endl;
return;
}
fout << "Beijing Forestry University Library Computer Books Purchasing List" << endl;
fout << "ISBN Book_Title Pricing" << endl;
Node* curr = head;
while (auxpointer->before != NULL) {
fout << auxpointer->data.no << " " << auxpointer->data.name << " " << auxpointer->data.price << endl;
cout << auxpointer->data.no << " " << auxpointer->data.name << " " << auxpointer->data.price << endl;
auxpointer = auxpointer->before;
}
fout.close();
}
void List::show() {
Node* current = head->next;
while (current != NULL) {
cout << current->data.no << " " << current->data.name << " " << current->data.price << endl;
current = current->next;
}
}
void List::shownumber() {
cout << number << endl;
}
void List::mergesort() {
recmergesort(head);
show();
ofstream fout("book-newsort.txt");
if (!fout) {
cerr << "Can't open the file." << endl;
return;
}
fout << "Beijing Forestry University Library Computer Books Purchasing List" << endl;
fout << "ISBN Book_Title Pricing" << endl;
Node* curr = head->next;
while (curr) {
fout << curr->data.no << " " << curr->data.name << " " << curr->data.price << endl;
curr = curr->next;
}
fout.close();
}
void List::recmergesort(Node*& firsthead) {
Node* otherhead = head;
if (firsthead != NULL)
if (firsthead->next != NULL) {
division(firsthead, otherhead);
recmergesort(firsthead);
recmergesort(otherhead);
firsthead = merge(firsthead, otherhead);
}
}
void List::division(Node* first1, Node*& first2) {
Node* middle, * current;
if (first1 == NULL) {
first2 = NULL;
return;
}
else if (first1->next == NULL) {
first2 = NULL;
return;
}
else {
middle = first1;
current = first1->next;
if (current != NULL)
current = current->next;
while (current != NULL)
{
middle = middle->next;
current = current->next;
if (current != NULL)
current = current->next;
}
first2 = middle->next;
middle->next = NULL;
}
}
Node* List::merge(Node* first1, Node* first2) {
Node* auxpointer, * newhead;
if (first1 == NULL) return first2;
else if (first2 == NULL) return first1;
else {
if (first1->data.price > first2->data.price)
{
newhead = first2;
first2 = first2->next;
}
else
{
newhead = first1;
first1 = first1->next;
}
auxpointer = newhead;
while (first1 != NULL && first2 != NULL) {
if (first1->data.price > first2->data.price)
{
auxpointer->next = first2;
auxpointer = auxpointer->next;
first2 = first2->next;
}
else
{
auxpointer->next = first1;
auxpointer = auxpointer->next;
first1 = first1->next;
}
}
if (first1 == NULL)
auxpointer->next = first2;
else
auxpointer->next = first1;
return newhead;
}
}
void List::search(const int place) {
int i = 0;
Node* current = head;
while (current != NULL)
{
if (i == place)
cout << current->data.no << " " << current->data.name << " " << current->data.price << endl;
i++;
current = current->next;
}
}
void List::search(const string name) {
Node* current = head;
while (current != NULL)
{
if (name == current->data.name)
cout << current->data.no << " " << current->data.name << " " << current->data.price << endl;
current = current->next;
}
}
void List::update() {
Node* current = head;
while (current != NULL)
{
if (current->data.price >= 45)
current->data.price *= 1.1;
else
current->data.price *= 1.2;
current = current->next;
}
ofstream fout("book-newprice.txt");
if (!fout) {
cerr << "Can't open the file." << endl;
return;
}
fout << "Beijing Forestry University Library Computer Books Purchasing List" << endl;
fout << "ISBN Book_Title Pricing" << endl;
Node* curr = head->next;
while (curr) {
fout << curr->data.no << " " << curr->data.name << " " << curr->data.price << endl;
curr = curr->next;
}
fout.close();
}
void List::fileprint() {
ofstream fout("book.txt");
fout << "Beijing Forestry University Library Computer Books Purchasing List" << endl;
fout << "ISBN Book_Title Pricing" << endl;
Node* current = head;
while (current) {
fout << current->data.no << " " << current->data.name << " " << current->data.price << endl;
current = current->next;
}
fout.close();
}