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

我要VC6能编译的,不要C++,谢谢

第1个回答  推荐于2016-09-06
#include <iostream>
#include <fstream>
#include <string>

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<<"请输图书信息:"<<endl;
for(int i=0;i<n;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<<"请输入图书信息:"<<endl;
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<<"系统没有储存任何图书信息,请输入图书信息后再进行其他操作!"<<endl;
}
else
{
cout<<"*********下面是所有的图书信息**********"<<endl;
while(p!=NULL)
{
cout<<" 图书"<<i<<": "<<p->book.bookname<<endl;
cout<<"登录名:"<<p->book.loginname;
cout<<"书名:"<<p->book.bookname;
cout<<"作者名:"<<p->book.author;
cout<<"分类号:"<<p->book.number;
cout<<"出版日期:"<<p->book.date;
cout<<"价格:"<<p->book.price;
cout<<endl;
p=p->next;
i++;
cout<<endl;
}
}

cout<<"请按回车键返回菜单。"<<endl;
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<<"查找的图书不存在!"<<endl;
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<<"查找的图书不存在!"<<endl;
return count;
}

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

int Find(Node *head)
{
int n,num;
system("cls");
if(head->next == NULL)
{
cout<<"系统没有储存任何图书信息,请输入图书信息后再进行其他操作!"<<endl;
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<<"输入有误,请重新输入!"<<endl;
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<<"系统没有储存任何图书信息,请输入图书信息后再进行其他操作!"<<endl;
else
{
cout<<"请输入需要更正信息的图书名:"<<endl;
n = findbookname(head);
Showarray(n);
loop:
{
cout<<"请选择对第几本书的信息进行修改:";
}
cin>>i;
cin.clear();
cin.sync();
if(i > 0 & i <= n)
{
cout<<"******修改第"<<i<<"本书的其他信息******"<<endl;
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<<"系统没有任何图书信息!"<<endl;
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<<"没有找到该图书信息!"<<endl;
else
cout<<"图书信息已经删除!"<<endl;
}
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<<"打开文件失败"<<endl;
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键退出!"<<endl;
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<<"打开文件失败"<<endl;
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<<" 图书信息管理系统          "<<endl;
cout<<"*****************************************************"<<endl;
cout<<" 1.输入图书信息 2.输出图书信息 "<<endl;
cout<<" 3.查找图书信息 4.修改图书信息 "<<endl;
cout<<" 5.删除图书信息 6.保存图书信息 "<<endl;
cout<<" 7.退出系统 "<<endl;
cout<<"*****************************************************"<<endl;
}
int main()
{

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

你这个是C++版本的,有没有C版本的,最好是VC6

追答

在vc6上能运行,你只要把 像head = new node 改成 head = (node *)malloc(sizeof(node)),输入输出改一下就行了啊

本回答被提问者采纳
相似回答