第1个回答 2009-06-11
#include "stdio.h"
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct book{
char number[20];
char bookname[20];
int count;
int price;
char zhuanye[20];
char author[20];
char chubanshe[20];
char beizhu[100];
struct book *next;
};
struct book *creat() //创建链表
{
struct book *p,*head,*end;
head=end=p=(struct book *)malloc(sizeof(struct book));
printf("输入编号为0结束输入\n");
do
{
printf("\n教材编号:");
gets(p->number);
if(strcmp(p->number,"0")==0){ end->next=NULL; return head;}
end=p;
printf("\n书名:");
gets(p->bookname);
printf("\n数量:");
scanf("%d",&p->count);
printf("\n价格:");
scanf("%d",&p->price);
getchar();
printf("\n专业:");
gets(p->zhuanye);
printf("\n作者:");
gets(p->author);
printf("\n出版社:");
gets(p->chubanshe);
printf("\n备注:");
gets(p->beizhu);
p=(struct book *)malloc(sizeof(struct book));
end->next=p;
}while(1);
}
void print(struct book *head) /*输出链表*/
{
struct book *p;
p=head;
if(p==NULL) { printf("您未打开任何文件,无法显示"); return;}
do{
printf("\n\n教材编号:%s",p->number);
printf("\n书名:%s",p->bookname);;
printf("\n数量:%d",p->count);
printf("\n价格:%d",p->price);
printf("\n专业:%s",p->zhuanye);
printf("\n作者:%s",p->author);
printf("\n出版社:%s",p->chubanshe);
printf("\n备注:%s",p->beizhu);
p=p->next;
}while(p!=NULL);
}
void save(struct book *head) //保存链表
{FILE *fp;
struct book *p;
char filename[20];
int ch;
printf("请输入文件名(包括后缀):");
scanf("%s",filename);
if((fp=fopen(filename,"r"))!=NULL)
{
printf("\n该文件已存在,是否覆盖?1--是 2---否");
scanf("%d",&ch);
if(ch!=1)
return;
}
if((fp=fopen(filename,"w"))==NULL) { printf("\n保存文件失败");getchar();return;}
p=head;
do{
fprintf(fp,"\n%s",p->number);
fprintf(fp,"\n%s",p->bookname);;
fprintf(fp,"\n%d",p->count);
fprintf(fp,"\n%d",p->price);
fprintf(fp,"\n%s",p->zhuanye);
fprintf(fp,"\n%s",p->author);
fprintf(fp,"\n%s",p->chubanshe);
fprintf(fp,"\n%s",p->beizhu);
p=p->next;
}while(p!=NULL);
fprintf(fp," over");
printf("\n保存成功");
getchar();
getchar();
fclose(fp);
}
struct book *openfile() //打开文件
{struct book *p,*f,*head;
FILE *fp;
char filename[20];
printf("\n请输入文件名(包括路径和后缀):");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL){ printf("\n文件找不到,请检查是否有该文件和路径是否正确");getchar();return NULL;}
head=f=p=(struct book *)malloc(sizeof(struct book));
fscanf(fp,"%s%s%d%d%s%s%s%s",p->number,p->bookname,&p->count,&p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
while(!feof(fp))
{ p=(struct book *)malloc(sizeof(struct book));
f->next=p;
fscanf(fp,"%s%s%d%d%s%s%s%s",p->number,p->bookname,&p->count,&p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
if(strcmp(p->number,"over")==0){ f->next=NULL; printf("\n文件打开成功,你可以显示此信息");getchar(); return head; }
f=p;
}
return head;
}
void namesort(struct book *head) //按书名排序
{ struct book *p,*t,*f,*h;
char ch[20];
int i;
h=t=f=p=head;
if(head==NULL) {printf("您为打开任何文件");getchar();return; };
f=p->next;
for(p=head;p->next!=NULL;p=p->next)
{
for(t=head,f=t->next;t->next!=NULL;f=f->next,t=t->next)
{
if(strcmp(t->bookname,f->bookname)>0)
{
strcpy(ch,t->number );
strcpy(t->number,f->number);
strcpy(f->number,ch);
strcpy(ch,t->bookname );
strcpy(t->bookname,f->bookname);
strcpy(f->bookname,ch);
i=t->count ;
t->count=f->count;
f->count=i;
i=t->price ;
t->price=f->price;
f->price=i;
strcpy(ch,t->zhuanye);
strcpy(t->zhuanye,f->zhuanye);
strcpy(f->zhuanye,ch);
strcpy(ch,t->author);
strcpy(t->author,f->author);
strcpy(f->author,ch);
strcpy(ch,t->chubanshe);
strcpy(t->chubanshe,f->chubanshe);
strcpy(f->chubanshe,ch);
strcpy(ch,t->beizhu);
strcpy(t->beizhu,f->beizhu);
strcpy(f->beizhu,ch);
}
}
}
print(h);
}
void chubanshesort(struct book *head) //按出版社排序
{ struct book *p,*t,*f,*h;
char ch[20];
int i;
h=t=f=p=head;
if(head==NULL) {printf("您为打开任何文件");getchar();return; }
f=p->next;
for(p=head;p->next!=NULL;p=p->next)
{
for(t=head,f=t->next;t->next!=NULL;f=f->next,t=t->next)
{
if(strcmp(t->chubanshe,f->chubanshe)>0)
{
strcpy(ch,t->number );
strcpy(t->number,f->number);
strcpy(f->number,ch);
strcpy(ch,t->bookname );
strcpy(t->bookname,f->bookname);
strcpy(f->bookname,ch);
i=t->count ;
t->count=f->count;
f->count=i;
i=t->price ;
t->price=f->price;
f->price=i;
strcpy(ch,t->zhuanye);
strcpy(t->zhuanye,f->zhuanye);
strcpy(f->zhuanye,ch);
strcpy(ch,t->author);
strcpy(t->author,f->author);
strcpy(f->author,ch);
strcpy(ch,t->chubanshe);
strcpy(t->chubanshe,f->chubanshe);
strcpy(f->chubanshe,ch);
strcpy(ch,t->beizhu);
strcpy(t->beizhu,f->beizhu);
strcpy(f->beizhu,ch);
}
}
}
print(h);
}
void booknamesearch(struct book *head) //按书名查找
{ struct book *p;
char name[20];
int c;
if(head==NULL) {printf("您为打开任何文件");getchar();return ;}
printf("查找专业--1, 查找书名--2:");
scanf("%d",&c);
getchar();
if(c==2)
printf("请输入要查找的书名:");
else printf("请输入要查找的专业:");
gets(name);
p=head;
do{ if(c==2)
if(strcmp(p->bookname,name)==0)
{
printf("教材编号:%s\n书名:%s\n数量:%d\n价格:%d\n专业:%s\n作者:%s\n出版社:%s\n备注:%s\n",p->number,p->bookname,p->count,p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
}
if(c==1)
if(strcmp(p->zhuanye,name)==0)
{
printf("教材编号:%s\n书名:%s\n数量:%d\n价格:%d\n专业:%s\n作者:%s\n出版社:%s\n备注:%s\n",p->number,p->bookname,p->count,p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
}
p=p->next;
}while(p!=NULL);
printf("查找完毕");
return;
}
struct book *add(struct book *head) //增加记录
{ struct book *p,*e,*f,*h;
if(head==NULL) {printf("您为打开任何文件");getchar(); return NULL;}
h=f=e=head;
p=(struct book *)malloc(sizeof(struct book));
printf("\n教材编号:");
gets(p->number);
printf("\n书名:");
gets(p->bookname);
printf("\n数量:");
scanf("%d",&p->count);
printf("\n价格:");
scanf("%d",&p->price);
getchar();
printf("\n专业:");
gets(p->zhuanye);
printf("\n作者:");
gets(p->author);
printf("\n出版社:");
gets(p->chubanshe);
printf("\n备注:");
gets(p->beizhu);
if(strcmp(f->number,p->number)>0) { p->next=f;h=p;printf("\n添加成功");return h;}
if(f->next==NULL) { f->next=p;p->next=NULL;printf("\n添加成功");return h;}
do{
if(f->next!=NULL)
if(strcmp(f->number,p->number)>0)
{
e->next=p;p->next=f;printf("\n添加成功");return h;
}
if(f->next==NULL)
{
f->next=p;
p->next=NULL;
printf("\n添加成功");
return h;
}
f=f->next;
e=e->next;
}while(1);
}
struct book *delet(struct book *head) //删除记录
{ struct book *p,*e;
char num[20];
if(head==NULL) {printf("您为打开任何文件");getchar();return NULL;}
printf("请输入要删除的教材编号:");
scanf("%s",num);
p=e=head;
if(strcmp(p->number,num)==0) { head=head->next; print(head);return head;}
else p=p->next;
do{
if(strcmp(p->number,num)==0)
{
if(p->next!=NULL)
e->next=p->next;
if(p->next==NULL) e->next=NULL;
print(head);
return head;
}
p=p->next;
e=e->next;
}while(p!=NULL);
printf("搜索完毕,未找到该记录");
}
struct book *change(struct book *head) //修改记录
{ struct book *p;
char num[20];
if(head==NULL) {printf("您为打开任何文件");getchar();return NULL;}
printf("请输入你要修改的教材编号:");
scanf("%s",num);
getchar();
p=head;
do{
if(strcmp(p->number,num)==0)
{
printf("教材编号:%s\n书名:%s\n数量:%d\n价格:%d\n专业:%s\n作者:%s\n出版社:%s\n备注:%s\n",p->number,p->bookname,p->count,p->price,p->zhuanye,p->author,p->chubanshe,p->beizhu);
printf("\n教材编号:");
gets(p->number);
printf("\n书名:");
gets(p->bookname);
printf("\n数量:");
scanf("%d",&p->count);
printf("\n价格:");
scanf("%d",&p->price);
getchar();
printf("\n专业:");
gets(p->zhuanye);
printf("\n作者:");
gets(p->author);
printf("\n出版社:");
gets(p->chubanshe);
printf("\n备注:");
gets(p->beizhu);
printf("修改成功");
return head;
}
p=p->next;
}while(p!=NULL);
printf("未找到您要修改的记录");
return head;
}
int mima()
{ FILE *fp;
char mima1[20],mima2[20];
if((fp=fopen("mima","r"))==NULL)
{ printf("您尚未设置密码请输入:");
scanf("%s",mima1);
printf("请再次输入密码:");
scanf("%s",mima2);
if(strcmp(mima1,mima2)!=0) { printf("两次密码不一样");return 0;}
else {
fp=fopen("mima","w");
fprintf(fp,"%s",mima1);
printf("密码设置成功");
return 1;
}
}
printf("请输入初始化密码:");
scanf("%s",mima1);
fscanf(fp,"%s",mima2);
if(strcmp(mima1,mima2)==0)
{
printf("密码正确");
printf("请输入你要删除的文件名:");
scanf("%s",mima1);
if(remove(mima1)==0)
{
printf("删除成功");
getchar();
return 1;
}
else {
printf("删除失败,请检查是否存在该文件或路径是否正确");
getchar();
return 0;
}
}
else {
printf("密码错误");
getchar();
return 0;
}
}
void main()
{
struct book *head=NULL;
char i;
do{
printf(" *** 欢迎使用教材管理系统 ***\n");
printf("1---录入图书信息\n\n2---保存图书信息\n\n3---显示图书信息\n\n4---打开图书记录\n\n5---增加一个记录\n\n6---删除一个记录\n\n7---修改一个记录\n\n8---查找\n\ns---初始化系统\n\ne---退出系统\n");
printf("\n请输入你要的功能编号:");
i=getchar();
getchar();
system("cls");
switch(i)
{
case '1': head=creat();save(head);break;
case '2': save(head);break;
case '3': print(head);break;
case '4': head=openfile();break;
case '5': head=add(head);break;
case '6': head=delet(head);break;
case '7': head=change(head);break;
case '8': booknamesearch(head);break;
case 's': mima();break;
case 'e': exit(0);
default :break;
}
getchar();
system("cls");
}while(1);
}
改下就是图书管理系统了
呵呵