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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
| #include <iostream> #include <fstream> using namespace std;
int MAXSIZE = 10; #define OK 1 #define ERROR 0 typedef int Status; typedef struct Book{ string id; string name; string press; int count; string author; double price; } Book;
typedef struct { Book *books; int length; }BookList;
void readFile(BookList & bookList); Status save(const BookList & bookList); int queryBook(const BookList & bookList, string value, int flag, bool isNeedPrint); void printOne(const Book & book); void printAll(const BookList &bookList); void deleteBook(BookList & bookList, string id); void addBook(BookList & bookList); void modifyBook(BookList & bookList); void insertSort(BookList & bookList, int flag); void classify(const BookList & bookList); void extend(BookList & bookList); void menu();
void readFile(BookList & bookList){ string filename; cout << "请输入需要读入的文件名:" << endl; cin >> filename; ifstream infile(filename); if (!infile.is_open()) { cout << "文件读入失败,请重试!" << endl; return; }
int length = bookList.length; while (!infile.eof() && length < MAXSIZE) { infile >> bookList.books[length].id >> bookList.books[length].name >> bookList.books[length].press >> bookList.books[length].count >> bookList.books[length].author >> bookList.books[length].price; ++length; } if (!infile.eof()) cout << "空间不足,读入失败!请先扩充空间!" << endl; else { bookList.length = length; cout << "文件读入成功" << endl; } infile.close(); }
Status save(const BookList & bookList) { string filename; cout << "请输入需要保存到的文件名:"; cin >> filename; ofstream outfile(filename); if (!outfile.is_open()) { cout << "文件保存失败,是否退出(Y/N):"; char choice; if ('Y' == choice || 'y' == choice) return OK; else return ERROR; } else { int length = bookList.length; for (int i = 0; i < length; ++i) outfile << bookList.books[i].id << " " << bookList.books[i].name << " " << bookList.books[i].press << " " << bookList.books[i].count << " " << " " << bookList.books[i].author << " " << bookList.books[i].price << endl; cout << "文件保存成功!" << endl; return OK; } outfile.close(); }
int queryBook(const BookList & bookList, string value, int flag, bool isNeedPrint) { int length = bookList.length; if (0 == flag) { for (int i = 0; i < length; i++) if (value == bookList.books[i].id) { if (isNeedPrint) printOne(bookList.books[i]); return i; } if (isNeedPrint) cout << "对不起,查无此书!" << endl; return -1; } else if (1 == flag) { Book temp[bookList.length]; int count = 0; for (int i = 0; i < length; i++) if (value == bookList.books[i].name) { temp[count] = bookList.books[i]; count ++; }
if (0 == count) { if (isNeedPrint) cout << "对不起,查无此书!" << endl; return -1; } else { if (isNeedPrint) for (int i = 0; i < count; i++) printOne(temp[i]); return 1; } } }
void printAll(const BookList &bookList) { int length = bookList.length; for (int i = 0; i < length; ++i) printOne(bookList.books[i]); }
void printOne(const Book & book) { cout << "书籍ID:" << book.id << " "; cout << "书籍名称:" << book.name << " "; cout << "书籍出版社:" << book.press << " "; cout << "书籍数量:" << book.count << " "; cout << "书籍作者:" << book.author << " "; cout << "书籍价格:" << book.price << " " << endl; }
void deleteBook(BookList & bookList, string id) { int position = queryBook(bookList, id, 0, false); int length = bookList.length; if (-1 != position) { for (int i = position; i < length - 1; i++) bookList.books[i] = bookList.books[i + 1]; bookList.length --; cout << "文件删除成功!" << endl; } else cout << "对不起, 查无此书!" << endl; }
void addBook(BookList & bookList) { int length = bookList.length; if (length >= MAXSIZE) cout << "空间已满,请先开拓空间!" << endl; else { cout << "请依次输入id、书名、出版社、数量、作者、价格" << endl; cin >> bookList.books[length].id >> bookList.books[length].name >> bookList.books[length].press >> bookList.books[length].count >> bookList.books[length].author >> bookList.books[length].price; if (-1 != queryBook(bookList, bookList.books[length].id, 0, false)) { cout << "信息添加失败,已存在同id书籍" << endl; return; } bookList.length ++; cout << "书籍添加成功!" << endl; } }
void modifyBook(BookList & bookList) { string id; cout << "请输入需要修改的书籍id:"; cin >> id; int position = queryBook(bookList, id , 0, false); if (position == -1) cout << "对不起,查无此书" <<endl; else { char choice; cout << "当前书籍名为:" << bookList.books[position].name << ",是否要修改(Y/N): "; cin >> choice; if (choice == 'Y' || choice == 'y') { cout << "请输入新书名:"; cin >> bookList.books[position].name;
}
cout << "当前书籍出版社为:" << bookList.books[position].press << ",是否要修改(Y/N): "; cin >> choice; if (choice == 'Y' || choice == 'y') { cout << "请输入新出版社:"; cin >> bookList.books[position].press; }
cout << "当前书籍数量为:" << bookList.books[position].count << ",是否要修改(Y/N): "; cin >> choice; if (choice == 'Y' || choice == 'y') { cout << "请输入新数量:"; cin >> bookList.books[position].count; }
cout << "当前书籍作者为:" << bookList.books[position].author << ",是否要修改(Y/N): "; cin >> choice; if (choice == 'Y' || choice == 'y') { cout << "请输入新作者:"; cin >> bookList.books[position].author; }
cout << "当前书籍价格为:" << bookList.books[position].price << ",是否要修改(Y/N): "; cin >> choice; if (choice == 'Y' || choice == 'y') { cout << "请输入新价格:"; cin >> bookList.books[position].price; }
cout << "信息修改完毕!" << endl; cout << "修改后内容为:" << endl; printOne(bookList.books[position]); } }
void insertSort(BookList & bookList, int flag) { if (bookList.length == 0) cout << "当前数据为空" << endl; else if (bookList.length == 1) return; else { int length = bookList.length; if (flag == 0) { int i, j; for (i = 1; i < length; ++i) if (bookList.books[i].id < bookList.books[i - 1].id) { Book temp = bookList.books[i]; for (j = i - 1; j >= 0 && temp.id < bookList.books[j].id; --j) bookList.books[j + 1] = bookList.books[j]; bookList.books[j + 1] = temp; } cout << "已经按照ID升序完成排序!" << endl; }
if (flag == 1) { int i, j; for (i = 1; i < length; ++i) if (bookList.books[i].price < bookList.books[i - 1].price) { Book temp = bookList.books[i]; for (j = i - 1; j >= 0 && temp.price < bookList.books[j].price; --j) bookList.books[j + 1] = bookList.books[j]; bookList.books[j + 1] = temp; }
cout << "已按照价格升序完成排序!" << endl; } }
}
void classify(const BookList & bookList) { const int length = bookList.length; if (bookList.length == 0) cout << "当前数据为空" << endl; else { int i ,j; Book *books = new Book[length]; for (i = 0; i < length; ++i) { books[i] = bookList.books[i]; }
for (i = 1; i < length; ++i) if (books[i].press < books[i - 1].press) { Book temp = books[i]; for (j = i - 1; j >= 0 && temp.press < books[j].press; --j) books[j + 1] = books[j]; books[j + 1] = temp; }
cout << books[0].press << ":" << endl; printOne(books[0]); for (i = 1; i < length; ++i) { if (books[i - 1].press != books[i].press) cout << books[i].press << ":" << endl; printOne(books[i]); } delete[] books; }
}
void extend(BookList & bookList) { MAXSIZE *= 2; const int length = bookList.length; Book *books = new Book[length]; for (int i = 0; i < length; ++i) { books[i] = bookList.books[i]; } delete[] bookList.books; bookList.books = new Book[MAXSIZE]; for (int i = 0; i < length; ++i) { bookList.books[i] = books[i]; } delete[] books; cout << "空间已扩大两倍" << endl; }
void menu() { cout << "============1. 文件读入================" << endl; cout << "============2. 根据ID查询==============" << endl; cout << "============3. 根据书名查询=============" << endl; cout << "============4. 添加书籍================" << endl; cout << "============5. 删除书籍================" << endl; cout << "============6. 修改书籍================" << endl; cout << "============7. 根据ID排序==============" << endl; cout << "============8. 根据价格排序=============" << endl; cout << "============9. 分类打印=================" << endl; cout << "============10. 打印全部信息=============" << endl; cout << "============11. 保存====================" << endl; cout << "============12. 扩容====================" << endl; cout << "============0. 退出====================" << endl; cout << "请选择:"; }
int main() { BookList bookList; bookList.length = 0; bookList.books = new Book[MAXSIZE]; readFile(bookList); int choice = 1; while (choice) { menu(); cin >> choice; switch (choice) { case 1:{ readFile(bookList); break; } case 2:{ string value; cout << "请输入ID:"; cin >> value; queryBook(bookList, value, 0, true); break; } case 3:{ string value; cout << "请输入书名:"; cin >> value; queryBook(bookList, value, 1, true); break; } case 4:{ addBook(bookList); break; } case 5:{ string id; cout << "请输入需要删除的书籍ID:"; cin >> id; deleteBook(bookList, id); break; } case 6:{ modifyBook(bookList); break; } case 7:{ insertSort(bookList, 0); break; } case 8:{ insertSort(bookList, 1); break; } case 9:{ classify(bookList); break; } case 10:{ printAll(bookList); break; } case 11:{ save(bookList); break; } case 12:{ extend(bookList); break; } case 0:{ if (save(bookList)) return 0; break; } default:{ cout << "请输入正确指令!" << endl; break; }
} } }
|