C语言课程设计:图书管理系统。求完整的代码参考。

我要VC6能编译的,不要C++,谢谢
2024-12-21 18:13:42
推荐回答(1个)
回答1:

#include
#include
#include

using namespace std;

struct BookInfo
{
char loginname[10];
char bookname[20];
char author[20];
char number[5];
char date[10];
double price;
};

struct Node
{
struct BookInfo book;
struct Node *next;
};

void option();
void select();
Node *head;
Node *pt[10];
FILE *fp;

//创建链表
/*
Node *CrtNode(int n)
{
Node *head; //声明头指针head
Node *p,*s;
head=new Node; //创建头结点由head指向(空的头结点)
s=head;
cout<<"请输图书信息:"< for(int i=0;i {
p=new Node; //创建一个结点
cout<<"登录名:";
cin.getline(p->book.loginname,10);
strcat(p->book.loginname,"\n");
cout<<"书名:";
cin.getline(p->book.bookname,20);
strcat(p->book.bookname,"\n");
cout<<"作者名:";
cin.getline(p->book.author,20);
strcat(p->book.author,"\n");
cout<<"分类号:";
cin.getline(p->book.number,5);
strcat(p->book.number,"\n");
cout<<"出版日期:";
cin>>p->book.date;
strcat(p->book.date,"\n");
cout<<"价格:";
cin>>p->book.price;
s->next=p; //把创建的结点由s的next指向
s=p; //指针s的指向向后移一个结点
cin.clear();
cin.sync();
}
p->next=NULL; //最后一个结点的next指向空
return head; //返回头指针
}
*/
/*
strcat(p->book.loginname,"\n");
strcat(p->book.bookname,"\n");
strcat(p->book.author,"\n");
strcat(p->book.number,"\n");
strcat(p->book.date,"\n");
*/

//1.插入
void Insert(Node *head)
{
Node *p,*s;
s = head;
cout<<"请输入图书信息:"< p=new Node;
cin.clear();
cout<<"登录名:";
cin.getline(p->book.loginname,10);
strcat(p->book.loginname,"\n");
cin.clear();
cin.sync();
cout<<"书名:";
cin.getline(p->book.bookname,20);
strcat(p->book.bookname,"\n");
cin.clear();
cin.sync();
cout<<"作者名:";
cin.getline(p->book.author,20);
strcat(p->book.author,"\n");
cin.clear();
cin.sync();
cout<<"分类号:";
cin.getline(p->book.number,5);
strcat(p->book.number,"\n");
cin.clear();
cin.sync();
cout<<"出版日期(yyyy-mm-dd):";
cin>>p->book.date;
strcat(p->book.date,"\n");
cout<<"价格:";
cin>>p->book.price;
while(s->next)
s = s->next;
s->next = p;
p->next = NULL;
}

//初始化
Node *Initial()
{
Node *head;
head = new Node;
head->next = NULL;
return head;
}

//2.显示所有信息
void Show(Node *head)
{
int i = 1;
Node *p;
//显示除头结点以后所有结点(因为创建时头结点为空)
p=head->next;
if(p == NULL)
{
cout<<"系统没有储存任何图书信息,请输入图书信息后再进行其他操作!"< }
else
{
cout<<"*********下面是所有的图书信息**********"< while(p!=NULL)
{
cout<<" 图书"<book.bookname< cout<<"登录名:"<book.loginname;
cout<<"书名:"<book.bookname;
cout<<"作者名:"<book.author;
cout<<"分类号:"<book.number;
cout<<"出版日期:"<book.date;
cout<<"价格:"<book.price;
cout< p=p->next;
i++;
cout< }
}

cout<<"请按回车键返回菜单。"< cin.get();
}

//3.查找
int findauthor(const Node *head)
{
Node *ps;
char author[20];
int count = 0;
ps = head->next;
cout<<"请输入作者名:";
cin.getline(author,20);
strcat(author,"\n");
while(ps)
{
if(strcmp(ps->book.author,author) == 0)
{
pt[0] = ps;
count++;
}
ps = ps->next;
}
if(count == 0)
cout<<"查找的图书不存在!"< return count;
}

int findbookname(const Node *head)
{
Node *ps;
char bookname[20];
int count = 0;
int i = 0;
ps = head->next;
cout<<"请输入书名:";
cin.getline(bookname,20);
strcat(bookname,"\n");
while(ps)
{
if(strcmp(ps->book.bookname,bookname) == 0)
{
pt[i] = ps;
count++;
i++;
}
if(ps->next ==NULL)
break;
ps = ps->next;
}
if(count == 0)
cout<<"查找的图书不存在!"< return count;
}

void Showarray(int n)
{
cout<<"*********下面是所查找的图书信息**********"< for(int i=0;i {
cout<<" 图书"<book.bookname;
cout<<"登录名:"<book.loginname;
cout<<"书名:"<book.bookname;
cout<<"作者名:"<book.author;
cout<<"分类号:"<book.number;
cout<<"出版日期:"<book.date;
cout<<"价格:"<book.price;
cout< }
cin.get();
}

int Find(Node *head)
{
int n,num;
system("cls");
if(head->next == NULL)
{
cout<<"系统没有储存任何图书信息,请输入图书信息后再进行其他操作!"< cin.get();
}
else
{
cout<<"请选择查找方式(1.按作者名查找 2.按书名查找):";
cin>>n;
cin.clear();
cin.sync();
switch(n)
{
case 1:
num = findauthor(head);
if(num != 0)
Showarray(num);
break;
case 2:
num = findbookname(head);
if(num !=0)
Showarray(num);
break;
default:
cout<<"输入有误,请重新输入!"< cin.get();
system("cls");
option();
select();
}
}
return num;
}

//4.修改图书信息

void Modify(Node *head)
{
Node *p,*q,*s;
p = head->next;
int i,n;
if(p == NULL)
cout<<"系统没有储存任何图书信息,请输入图书信息后再进行其他操作!"< else
{
cout<<"请输入需要更正信息的图书名:"< n = findbookname(head);
Showarray(n);
loop:
{
cout<<"请选择对第几本书的信息进行修改:";
}
cin>>i;
cin.clear();
cin.sync();
if(i > 0 & i <= n)
{
cout<<"******修改第"< cout<<"登录名:";
cin.getline(pt[i-1]->book.loginname,10);
strcat(pt[i-1]->book.loginname,"\n");
cin.clear();
cin.sync();
cout<<"作者名:";
cin.getline(pt[i-1]->book.author,20);
strcat(pt[i-1]->book.author,"\n");
cin.clear();
cin.sync();
cout<<"分类号:";
cin.getline(pt[i-1]->book.number,5);
strcat(pt[i-1]->book.number,"\n");
cin.clear();
cin.sync();
cout<<"出版日期(yyyy-mm-dd):";
cin>>pt[i-1]->book.date;
strcat(pt[i-1]->book.date,"\n");
cout<<"价格:";
cin>>pt[i-1]->book.price;
}
else
{
cout<<"选择的书的序号有误!";
goto loop;
}
}
cin.get();
}

//5.删除图书信息
void Deletebook(Node *head)
{
Node *p,*q;
char bookname[20];
int count = 0;
int i = 0;
p = head;
q = p->next;
if(q == NULL)
cout<<"系统没有任何图书信息!"< else
{
cout<<"请输入书名:";
cin.getline(bookname,20);
strcat(bookname,"\n");
while(q != NULL)
{
q = p->next;
if(strcmp(q->book.bookname,bookname) == 0)
{
p->next = q->next;
delete q;
count++;
}
p = q;
q = q->next;
}
if(count == 0)
cout<<"没有找到该图书信息!"< else
cout<<"图书信息已经删除!"< }
cin.get();
}

//链表长度
int Len(const Node *head)
{
Node *ps;
int count = 0;
ps = head->next;
while(ps)
{
count++;
ps = ps->next;
}
return count;
}

//6.保存文件

void save(Node *head)
{
Node *p;
char st[20];
p = head->next;
if((fp=fopen("book.dat", "w"))==NULL)
{
cout<<"打开文件失败"< return;
}
char t[255];

//将 L1.listlen() 赋予字符串中的数字
int lenth = Len(head);
fwrite(&lenth,sizeof(int),1,fp);
while(p)
{
fwrite(p,sizeof(struct Node),1,fp);
p = p->next;
}
fclose(fp);
cout<<"文件已经保存,请按ENTER键退出!"< cin.get();
}

//8.删除所有图书信息
void Free_List(Node *head)
{
Node *p;
p = head->next;
while(p)
{
delete p;
p = head->next;
}
}

/*
void readstr(FILE *f,char *string)
{
do
{
//①: 先读入一行文本
fgets(string, 255, f); //fgets(): 从文件 f 读入长度为 255-1 的字符串
// 并存入到 string 中
} while ((string[0] == '/') || (string[0] == '\n'));

return;
}

*/

//读取文件

Node *Load()
{
char c[255];
int lenth;
Node *p,*q;
head = new Node;
p = head;
if((fp=fopen("book.dat", "r"))==NULL)
{
cout<<"打开文件失败"< return head;
}
else
fread(&lenth,sizeof(int),1,fp);
for(int i = 0;i < lenth;i++)
{
q = new Node;
fread(q,sizeof(struct Node),1,fp);
p->next = q;
p = q;
}
p->next = NULL;
fclose(fp);
return head;
}

//9.菜单选项
void select()
{
int m,n;
cout<<"请输入以下数字来选择所需要的操作(1~8):";
cin.clear();
cin.sync();
cin>>n;
cin.clear();
cin.sync();
switch(n)
{
case 1:
system("cls");
Insert(head);
break;
case 2:
system("cls");
Show(head);
break;
case 3:
system("cls");
Find(head);
break;
case 4:
system("cls");
Modify(head);
break;
case 5:
system("cls");
Deletebook(head);
break;
case 6:
save(head);
break;
case 7:
exit(0);
break;
case 8:
Free_List(head);
break;
default:
break;
}

}

//10.菜单界面
void option()
{
cout<<" 图书信息管理系统          "< cout<<"*****************************************************"< cout<<" 1.输入图书信息 2.输出图书信息 "< cout<<" 3.查找图书信息 4.修改图书信息 "< cout<<" 5.删除图书信息 6.保存图书信息 "< cout<<" 7.退出系统 "< cout<<"*****************************************************"<}
int main()
{

//head = Initial();
head = Load();
while(1)
{
system("cls");
option();
select();
}
return 0;
}