《图书销售系统 C语言 C++ 数据结构 链表 课程设计.docx》由会员分享,可在线阅读,更多相关《图书销售系统 C语言 C++ 数据结构 链表 课程设计.docx(23页珍藏版)》请在三一办公上搜索。
1、图书销售系统 C语言 C+ 数据结构 链表 课程设计#include<stdio.h>#include<stdlib.h>#include<memory.h>#include<string.h>#include<conio.h>/* 销售信息结构定义 */typedef struct Bookchar id32;char sn32;char name32;char author32;char type32;char press32;char publish_time32;char price32;char sale_time32;char salesma
2、n32;char number32;struct Book* next;/* 指向链表的下一个节点 */Book;/* 链表头 */Book* head = NULL;/* 记录条数 */int size = 0;/* 从键盘得到一项数据 */void get_item(char* name, char* value)printf(%s:, name);scanf(%s, value);/* 输入一条新纪录 */Book* new_recordBook* book = malloc(sizeof(Book);memset(book, 0, sizeof(Book);fflush(stdin);
3、get_item(id, book->id);get_item(sn, book->sn);get_item(name, book->name);get_item(author, book->author);get_item(type, book->type);get_item(press, book->press);get_item(publish time, book->publish_time);get_item(price, book->price);get_item(sale time, book->sale_time);get_item(sale
4、sman, book->salesman);get_item(number, book->number);return book;/* 保存一项数据 */void save_item(FILE* fp, char* value)fwrite(value, 32, 1, fp);/* 从文件读取一项数据 */void load_item(FILE* fp, char* value)fread(value, 32, 1, fp);/* 保存到文件 */void saveBook* book = head;FILE* fp = fopen(BookSale.txt, wb);fwrite(&
5、size, sizeof(size), 1, fp);while(book)save_item(fp, book->id);save_item(fp, book->sn);save_item(fp, book->name);save_item(fp, book->author);save_item(fp, book->type);save_item(fp, book->press);save_item(fp, book->publish_time);save_item(fp, book->price);save_item(fp, book->sale_tim
6、e);save_item(fp, book->salesman);save_item(fp, book->number);book = book->next;fclose(fp);/* 向链表中添加新纪录 */void add_record(Book* book)if(head=NULL)head = book;elseBook* temp = head;while(temp->next)temp = temp->next;temp->next = book;/* 从文件中读取所有记录 */void loadint i;FILE* fp = fopen(BookSale
7、.txt, rb);fread(&size, sizeof(size), 1, fp);if(feof(fp)size = 0;return;for(i=0; i<size; i+)Book* book = malloc(sizeof(Book);memset(book, 0, sizeof(Book);load_item(fp, book->id);load_item(fp, book->sn);load_item(fp, book->name);load_item(fp, book->author);load_item(fp, book->type);load_it
8、em(fp, book->press);load_item(fp, book->publish_time);load_item(fp, book->price);load_item(fp, book->sale_time);load_item(fp, book->salesman);load_item(fp, book->number);add_record(book);fclose(fp);/* 从链表中删除一条记录 */void remove_record(Book* book)Book* temp = head; if(book=head)head = book-
9、>next;while(temp)if(temp->next=book)temp->next = book->next;break;temp = temp->next;free(book);size-;save;/* 根据name查询 */Book* get_by_nameint i = 0;char name32;Book* temp = head;Book* books = malloc(sizeof(Book*)*size);memset(books, 0, sizeof(Book*)*size);printf(Please input the book name:)
10、;scanf(%s, name);while(temp)if(strcmp(temp->name, name)=0) booksi+ = temp;temp = temp->next;return books;/* 根据name排序 */Book* sort_by_nameBook* books = malloc(sizeof(Book*)*size);int i = 0, j;Book* temp = head;while(temp)booksi+ = temp;temp = temp->next;for(i=0; i<size-1; i+)for(j=1; j<size
11、-i; j+)if(strcmp(booksj->name, booksj-1->name)<0)Book* t = booksj;booksj = booksj-1;booksj-1 = t;return books;Book* get_by_authorint i = 0;char author32;Book* temp = head;Book* books = malloc(sizeof(Book*)*size);memset(books, 0, sizeof(Book*)*size);printf(Please input the author:);scanf(%s, au
12、thor);while(temp)if(strcmp(temp->author, author)=0) booksi+ = temp;temp = temp->next;return books;Book* sort_by_authorBook* books = malloc(sizeof(Book*)*size);int i = 0, j;Book* temp = head;while(temp)booksi+ = temp;temp = temp->next;for(i=0; i<size-1; i+)for(j=1; j<size-i; j+)if(strcmp(bo
13、oksj->author, booksj-1->author)<0)Book* t = booksj;booksj = booksj-1;booksj-1 = t;return books;Book* get_by_timeint i = 0;char time32;Book* temp = head;Book* books = malloc(sizeof(Book*)*size);memset(books, 0, sizeof(Book*)*size);printf(Please input the time:);scanf(%s, time);while(temp)if(str
14、cmp(temp->sale_time, time)=0) booksi+ = temp;temp = temp->next;return books;Book* sort_by_timeBook* books = malloc(sizeof(Book*)*size);int i = 0, j;Book* temp = head;while(temp)booksi+ = temp;temp = temp->next;for(i=0; i<size-1; i+)for(j=1; j<size-i; j+)if(strcmp(booksj->sale_time, books
15、j-1->sale_time)<0)Book* t = booksj;booksj = booksj-1;booksj-1 = t;return books;Book* get_by_salesmanint i = 0;char salesman32;Book* temp = head;Book* books = malloc(sizeof(Book*)*size);memset(books, 0, sizeof(Book*)*size);printf(Please input the salesman:);scanf(%s, salesman);while(temp)if(strcm
16、p(temp->salesman, salesman)=0) booksi+ = temp;temp = temp->next;return books;Book* sort_by_salesmanBook* books = malloc(sizeof(Book*)*size);int i = 0, j;Book* temp = head;while(temp)booksi+ = temp;temp = temp->next;for(i=0; i<size-1; i+)for(j=1; j<size-i; j+)if(strcmp(booksj->salesman, b
17、ooksj-1->salesman)<0)Book* t = booksj;booksj = booksj-1;booksj-1 = t;return books;/* 向屏幕输出一项数据 */void put_item(char* name, char* value)printf(%s:t%sn, name, value);/* 向屏幕输出一条记录 */void output(Book* book)put_item(id, book->id);put_item(sn, book->sn);put_item(name, book->name);put_item(author
18、, book->author);put_item(type, book->type);put_item(press, book->press);put_item(publish time, book->publish_time);put_item(price, book->price);put_item(sale time, book->sale_time);put_item(salesman, book->salesman);put_item(number, book->number);printf(n);/* 输出整个链表内容 */void output_l
19、istBook* temp = head;while(temp)output(temp);temp = temp->next;/system(pause);getch;/* 输出查询或排序结果的数组 */void output_array(Book* books)int i;for(i=0; i<size; i+)if(booksi=0)break;output(booksi);/system(pause);getch;/* 显示菜单 */int menuint key = 0;/system(cls);clrscr;printf(ttttBook Sale Systemn);prin
20、tf(ttt1.Add new record n);printf(ttt2.Delete record n);printf(ttt3.Browse all recordsn);printf(ttt4.Query by namen);printf(ttt5.Query by authorn);printf(ttt6.Query by sale timen);printf(ttt7.Query by salesmann);printf(ttt8.Sort by namen);printf(ttt9.Sort by authorn);printf(ttt10.Sort by sale timen);
21、printf(ttt11.Sort by salesmann);printf(ttt12.Quitn);fflush(stdin);scanf(%d, &key);return key;/* 删除一条数据 */void delete_recordchar id32;Book* book = head;printf(Please input the id of record:);scanf(%s, id);while(book)if(strcmp(book->id, id)=0)remove_record(book);break;book = book->next;/* 主函数 */vo
22、id mainload;while(1)int key = menu;switch(key)case 1:add_record(new_record);size+;save;break;case 2:delete_record;save;break;case 3:output_list;break;case 4:output_array(get_by_name);break;case 5:output_array(get_by_author);break;case 6:output_array(get_by_time);break;case 7:output_array(get_by_salesman);break;case 8:output_array(sort_by_name);break;case 9:output_array(sort_by_author);break;case 10:output_array(sort_by_time);break;case 11:output_array(sort_by_salesman);break;case 12:return;break;default:break;