1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 数据结构课程设计图书管理系统 C语言版。

数据结构课程设计图书管理系统 C语言版。

时间:2021-12-17 02:36:55

相关推荐

数据结构课程设计图书管理系统 C语言版。

目录

一、功能描述二、设计要求三、实现的功能四、代码

一、功能描述

设计一个图书管理程序满足图书馆基本业务需求。

二、设计要求

每种书的登记内容包括书号、书名、著作者、现存量和库存量等;对书号建立索引表(线性表)以提高查找效率;实现图书管理系统的主要功能描述。用数据文件存储。

三、实现的功能

对图书信息进行增、删、改、差查询所有

运行结果大概就是这个样子:

四、代码

Tip:主要运用的是链表和文件的读写,注释很详细。

#include<stdio.h>#include<stdlib.h>#include "book.h"#define len 20#define size 20/*1、建立一个链表2、将.dat文件中读出的数据放入链表中3、进行业务操作4、业务操作后将链表中数据存回.dat文件中*///定义一个图书类型struct oneBook{char isbn[size];char bookname[size];char author[size];int stocknum;float price;int flag;}bookarr[len];//定义链表typedef struct Book{char isbn[size];char bookname[size];char author[size];int stocknum;float price;int flag;struct Book* next;} ListBook;//定义一个输入字符,判断使用者需要的功能char inNeed;//编写程序首页方法void indexPage() {//首先写出程序的首页面printf("|***********************************************************|\n");printf("|---------welcome to the Library Management System----------|\n");printf("|-------------------添加图书信息请输入1---------------------|\n");printf("|-------------------删除图书信息请输入2---------------------|\n");printf("|-------------------修改图书信息请输入3---------------------|\n");printf("|-------------------查询图书信息请输入4---------------------|\n");printf("|-------------------查询书目信息请输入5---------------------|\n");printf("|-----------------退出图书管理系统请输入6-------------------|\n");printf("|***********************************************************|\n");printf("请输入要进行的操作:");}//编写读取文件内容的方法 ,每次用程序之前将数据从文件中读取,放入链表中ListBook* readFile(ListBook *head) {//定义一个指向文件的指针FILE* fp;char content;ListBook* temp,*book;//这里的r是只读的意思if ((fp = fopen("file1.dat", "r")) == NULL) {//打开文件失败printf("cannot open this file\n");exit(0);}//将文件中数据拿出来,方进书的结构体数组中for (int i = 0; i < len; i++) {fread(&bookarr[i], sizeof(struct oneBook), 1, fp);//加一个判断跳出循环if (bookarr[i].flag == 0) {break;}//把结构体数据方进链表,success,用的是头插法if (head == NULL) {book = (ListBook*)malloc(sizeof(ListBook));//字符串赋值给字符数组用strncpystrncpy(book->isbn, bookarr[i].isbn, size);strncpy(book->bookname, bookarr[i].bookname, size);strncpy(book->author, bookarr[i].author, size);book->stocknum = bookarr[i].stocknum;book->price = bookarr[i].price;book->flag = bookarr[i].flag;head = book;;head->next = NULL;}else {temp = head;book = (ListBook*)malloc(sizeof(ListBook));strncpy(book->isbn, bookarr[i].isbn, size);strncpy(book->bookname, bookarr[i].bookname, size);strncpy(book->author, bookarr[i].author, size);book->stocknum = bookarr[i].stocknum;book->price = bookarr[i].price;book->flag = bookarr[i].flag;head = book;head->next = temp;}}//关闭文件输入流fclose(fp);return head;}//编写 添加图书信息方法 ListBook* insertMessage(ListBook* head) {printf("输入想要录入的图书信息,并以#结束:\n");printf(" *录入的模板为:ISBN码 书名 著作者 库存量 金额\n");ListBook* book,*temp;//把添加的数据放入链表,用的是头插法for (int i = 0; i < len; i++) {if (head == NULL) {book = (ListBook*)malloc(sizeof(ListBook));scanf("%s %s %s %d %f", &book->isbn, &book->bookname, &book->author, &book->stocknum, &book->price);book->flag = 1;head = book;;head->next = NULL;if (getchar() == '#') {break;}}else {temp = head;book = (ListBook*)malloc(sizeof(ListBook));scanf("%s %s %s %d %f", &book->isbn, &book->bookname, &book->author, &book->stocknum, &book->price);book->flag = 1;head = book;head->next = temp;if (getchar() == '#') {break;}}}//打印链表,没啥用printf("这是刚刚添加的数据\n");temp = head;while (temp != NULL) {printf("isbn:%s\t", temp->isbn);printf("bookname:%s\t", temp->bookname);printf("author:%s\t", temp->author);printf("stocknum:%d\t", temp->stocknum);printf("price:%.2f\t", temp->price);printf("flag:%d\n", temp->flag);temp = temp->next;}printf("\n添加成功!\n");return head;}//编写储存数据方法, 每次程序使用结束,将数据存入文件中void saveFile(ListBook *book) {//先将链表中数据放回结构体数组,成功int i = 0;while (book != NULL) {strncpy(bookarr[i].isbn, book->isbn, size);strncpy(bookarr[i].bookname, book->bookname, size);strncpy(bookarr[i].author, book->author, size);bookarr[i].stocknum = book->stocknum;bookarr[i].price = book->price;bookarr[i].flag = book->flag;book = book->next;i++;}//重新写入文件FILE* fp;if ((fp = fopen("file1.dat", "wb")) == NULL) {printf("cannot open file\n");return;}for (int j = 0; j < i; j++) {if (fwrite(&bookarr[j], sizeof(struct oneBook), 1, fp) != 1) {printf("file write error\n");}}fclose(fp);printf("~程序已退出,欢迎下次使用~");return;}//编写查询所有图书信息的方法void selectAll(ListBook* book) {ListBook* print;print = book;while (print != NULL) {printf("isbn:%s\t", print->isbn);printf("bookname:%s\t", print->bookname);printf("author:%s\t", print->author);printf("stocknum:%d\t", print->stocknum);printf("price:%.2f\n", print->price);print = print->next;}}//编写删除图书信息方法ListBook* deleteMessage(ListBook* head,char isbn[size]) {ListBook* temp, * pre,*book;book = temp = head;pre = head->next;while (head != NULL) {//在头上if (0 == strcmp(head->isbn, isbn)) {head = head->next;book = head;free(temp);printf("删除成功\n");break;}//如果链表中只有一个值,但是不是要找的值,pre指针的strcmp会空指针if (head->next == NULL) {printf("没有isbn码为%s的图书\n", isbn);break;}//不在头上if (0 == strcmp(pre->isbn, isbn)) {temp = pre;head->next = pre->next;free(temp);printf("删除成功\n");break;}pre = pre->next;head = head->next;if (head == NULL || pre == NULL) {printf("没有isbn码为%s的图书\n", isbn);}}return book;}//编写修改图书信息的方法ListBook* updataMessage(ListBook* head,char isbn[size]) {ListBook* temp;temp = head;//首先把要修改的图书找出来while (head != NULL) {if (0 == strcmp(head->isbn, isbn)) {printf("请输入要修改的值:");scanf("%s %s %s %d %f", &head->isbn, &head->bookname, &head->author, &head->stocknum, &head->price);printf("修改成功");break;}head = head->next;if (head == NULL) {printf("没有isbn码为:%s的图书\n", isbn);}}return temp;}//编写查询图书信息方法void selectOne(ListBook* head,char isbn[size]) {ListBook* temp;temp = head;while (temp != NULL) {//printf("打印:%s\n", temp->isbn);if (0 == strcmp(temp->isbn, isbn)) {printf("查询结果为:\n");printf("isbn:%s\t", temp->isbn);printf("bookname:%s\t", temp->bookname);printf("author:%s\t", temp->author);printf("stocknum:%d\t", temp->stocknum);printf("price:%.2f\n", temp->price);break;}temp = temp->next;if (temp == NULL) {printf("没有isbn码为:%s的图书\n", isbn);}}}void main() {//定义一个链表ListBook* head=NULL;//调用程序首页方法indexPage();//调用读取文件内容的方法head = readFile(head);inNeed = getchar();//编写一个大循环,使程序一直跑下去while (inNeed!='6') {if (inNeed == '1') {head = insertMessage(head);//输入下一步操作printf("请输入下一步操作:\n");//用来接收最后输入的回车符inNeed = getchar();//接收下一步操作的信号inNeed = getchar();}else if (inNeed == '2') {printf("请输入要删除图书的ISBN码:\n");//调用删除方法,需要输入要删除图书的isbnchar isbn[size];scanf("%s", isbn);head = deleteMessage(head,isbn);//输入下一步操作printf("请输入下一步操作:\n");//用来接收最后输入的回车符inNeed = getchar();//接收下一步操作的信号inNeed = getchar();}else if (inNeed == '3') {printf("请输入要修改的图书的ISBN码:\n");//调用删除方法,需要输入要删除图书的isbnchar isbn[size];scanf("%s", isbn);head = updataMessage(head, isbn);//输入下一步操作printf("请输入下一步操作:\n");//用来接收最后输入的回车符inNeed = getchar();//接收下一步操作的信号inNeed = getchar();}else if (inNeed == '4') {printf("请输入要查询的图书的ISBN码:\n");//调用删除方法,需要输入要删除图书的isbnchar isbn[size];scanf("%s", isbn);selectOne(head, isbn);//输入下一步操作printf("请输入下一步操作:\n");//用来接收最后输入的回车符inNeed = getchar();//接收下一步操作的信号inNeed = getchar();}else if(inNeed == '5'){printf("查询书目信息\n");//编写查询书目方法selectAll(head);//输入下一步操作printf("请输入下一步操作:\n");//用来接收最后输入的回车符inNeed = getchar();//接收下一步操作的信号inNeed = getchar();}}//调用存数据方法,结束程序saveFile(head);return;}

不知道是因为我自己的电脑还是vs什么的原因,第一次运行特别慢 卡,但是运行成功之后,下次运行就飞快。希望对做这个课设的小伙伴有帮助吧。

欢迎指导纠错~

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。