将写代码过程比较好的代码备份一下,下边代码是关于C语言实现的文件型图书管理系统的代码。
#include
#include
#include
struct book{
int num;
char bname[50];
char wname[20];
char press[50];
char sort[50];
int time;
float price;
};
int main()
{
int choice,n,x,y=1,c,c1=1234;
char a,d,b[10],b1[10]=“yjk”;
while(y){
system("cls");
printf("nnnnnnn");
printf("nn");
printf(" ============1-用户登录===========n");
printf(" ============0-退出系统===========n");
printf(" 请输入您的选择:");
scanf("%d",&n);
printf("n");
getchar();
switch(n){
case 0:
y=0;
break;
case 1:
printf(" 请输入您的用户名:");
gets(b);
printf("n");
printf(" 请输入您的密码:");
scanf("%d",&c);
printf("n");
if(strcmp(b,b1)!=0||c!=c1){
printf(" 验证失败,请重新输入!n");
scanf("%c",&d);
getchar();
system("cls");
}
else{
printf(" 验证通过!请按Enter键进入!n");
scanf("%c",&d);
getchar();
x=1;
while(x){
system("cls");
printf(" ------------------n");
printf(" ------------------nn");
printf(" || 1-添加图书 2-删除图书 ||nn");
printf(" || 3-图书列表 4-图书排序 ||nn");
printf(" || 5-查询图书 6-修改图书 ||nn");
printf(" || 7-录入数据 0-退出系统 ||nn");
printf("请输入所选择的序号:");
scanf("%d",&choice);
getchar();
system("cls");
switch(choice)
{
case 0:
x=0;break;
case 1:
head=load();
if(head==NULL){
printf("文件为空,请先录入数据!n");
getchar();
break;
}
else{
head=addbook(head);
printf("添加成功!n");
printf("是否将新信息保存到文件?(y/n)n");
scanf("%c",&a);
getchar();
switch(a){
case 'n':
break;
case 'y':
fprint(head);
printf("保存成功!n");
getchar();
break;
}
break;
}
case 2:
head=load();
if(head==NULL){
printf("文件为空,请先录入数据!n");
getchar();
break;
}
else{
deletebook(head);
getchar();
break;
}
break;
case 3:
head=load();
if(head==NULL){
printf("文件为空,请先录入数据!n");
getchar();
break;
}
else{
print_book(head);
getchar();
break;
}
case 4:
head=load();
if(head==NULL){
printf("文件为空,请先录入数据!n");
getchar();
break;
}
else{
paixu(head);
getchar();
}
break;
case 5:
head=load();
if(head==NULL){
printf("文件为空,请先录入数据!n");
getchar();
break;
}
else{
chaxun(head);
getchar();
}
break;
case 6:
head=load();
if(head==NULL){
printf("文件为空,请先录入数据!n");
getchar();
break;
}
else{
xiugai(head);
getchar();
break;
}
break;
case 7:
printf("注意:输入图书编码为0时结束!n");
head=creatbook();
printf("是否将输入的信息保存到文件以覆盖文件中已存在的信息?(y/n)n");
getchar();
scanf("%c",&a);
getchar();
switch(a){
case 'n':
break;
case 'y':
fprint(head);
printf("保存成功!n");
getchar();
break;
}
break;
default:
printf("您的输入有误,请重新输入!n");
getchar();
break;
}
}
}
break;
default:
printf(" 您的输入有误! 请重新输入!n");
getchar();
break;
}
}
}
{
int num,time,n;
char bname[50],wname[20],press[50],sort[50];
float price;
int size=sizeof(struct book);
head=tail=NULL;
printf(“请输入图书编号:”);
scanf("%d",&num);
printf(“请输入图书名:”);
scanf("%s",bname);
getchar();
printf(“请输入作者名:”);
scanf("%s",wname);
getchar();
printf(“请输入出版社:”);
scanf("%s",press);
getchar();
printf(“请输入类别:”);
scanf("%s",sort);
getchar();
printf(“请输入出版时间:”);
scanf("%d",&time);
getchar();
printf(“请输入价格:”);
scanf("%f",&price);
getchar();
while(1){
p->num=num;
strcpy(p->bname,bname);
strcpy(p->wname,wname);
strcpy(p->press,press);
strcpy(p->sort,sort);
p->time=time;
p->price=price;
p->next=NULL;
if(headNULL)
head=p;
else
tail->next=p;
tail=p;
do{
printf(“请输入图书编号:”);
scanf("%d",&num);
n=yanzheng(head,num);
if(n0)
break;
else
printf(“您输入的编号已存在,请重新输入!n”);
}while(1);
if(num==0)
break;
else
{
printf(“请输入图书名:”);
scanf("%s",bname);
getchar();
printf(“请输入作者名:”);
scanf("%s",wname);
getchar();
printf(“请输入出版社:”);
scanf("%s",press);
getchar();
printf(“请输入类别:”);
scanf("%s",sort);
getchar();
printf(“请输入出版时间:”);
scanf("%d",&time);
getchar();
printf(“请输入价格:”);
scanf("%f",&price);
getchar();
}
}
return head;
}
{
char bname[50],wname[20],press[50],sort[50];
int size=sizeof(struct book);
int num,time,n=1;
float price;
do{
printf("请输入图书编号:");
scanf("%d",&num);
n=yanzheng(head,num);
if(n==0)
break;
else
printf("您输入的编号已存在,请重新输入!n");
}while(1);
printf("请输入图书名:");
scanf("%s",bname);
getchar();
printf("请输入作者名:");
scanf("%s",wname);
getchar();
printf("请输入出版社:");
scanf("%s",press);
getchar();
printf("请输入类别:");
scanf("%s",sort);
getchar();
printf("请输入出版时间:");
scanf("%d",&time);
getchar();
printf("请输入价格:");
scanf("%f",&price);
getchar();
p->num=num;
strcpy(p->bname,bname);
strcpy(p->wname,wname);
strcpy(p->press,press);
strcpy(p->sort,sort);
p->time=time;
p->price=price;
p2=head;
ptr=p;
while((ptr->num>p2->num)&&(p2->next!=NULL)){
p1=p2;
p2=p2->next;
}
if(ptr->num<=p2->num){
if(head==p2)
head=ptr;
else{
p1->next=ptr;
p->next=p2;
}
}
else{
p2->next=ptr;
p->next=NULL;
}
return head;
}
{
p=head;
while(p!=NULL){
if(p->numm)
break;
p=p->next;
}
if(pNULL)
return 0;
else
return 1;
}
{
char ch=‘1’;
if((fp=fopen(“f1.txt”,“w”))==NULL){
printf(“File open error!n”);
exit(0);
}
fputc(ch,fp);
for(p1=head;p1;p1=p1->next){
fprintf(fp,"%d %s %s %s %s %d %fn",p1->num,p1->bname,p1->wname,p1->press,p1->sort,p1->time,p1->price);
}
fclose(fp);
}
{
char ch;
head=tail=NULL;
if((fp=fopen(“f1.txt”,“r”))NULL){
printf(“File open error!n”);
exit(0);
}
ch=fgetc(fp);
if(ch’1’){
while(!feof(fp)){
fscanf(fp,"%d%s%s%s%s%d%fn",&p1->num,p1->bname,p1->wname,p1->press,p1->sort,&p1->time,&p1->price);
if(head==NULL)
head=p1;
else
tail->next=p1;
tail=p1;
}
tail->next=NULL;
fclose(fp);
return head;
}
else
return NULL;
}
{
if(head==NULL){
printf(“n没有信息!n”);
return;
}
printf(" 图书信息列表如下n");
printf(" ==========================================================n");
printf(" 编号 图书名 作者名 出版社 类别 出版时间 价格n");
for(ptr=head;ptr;ptr=ptr->next)
printf(" %d %s %s %s %s %d %.2fn",ptr->num,ptr->bname,ptr->wname,ptr->press,ptr->sort,ptr->time,ptr->price);
printf(" ==========================================================n");
}
{
int a;
char b,ch=‘1’;
printf(“请输入要删除的图书编号:”);
scanf("%d",&a);
p1=head;
printf(“是否清空文件!(y/n)n”);
getchar();
scanf("%c",&b);
getchar();
switch(b){
case ‘n’:
break;
case ‘y’:
if((fp=fopen("f1.txt","w"))==NULL){
printf("File open error!n");
exit(0);
}
fclose(fp);
printf("文件已清空!n");
}
}
else{
while(p1->num!=a&&p1->next!=NULL){
p2=p1;
p1=p1->next;
}
if(p1->next==NULL){
if(p1->num==a){
p2->next=NULL;
printf("是否确定从文件中彻底删除该图书?(y/n)n");
getchar();
scanf("%c",&b);
switch(b){
case 'n':
break;
case 'y':
fprint(head);
printf("删除成功!n");
getchar();
break;
}
}
else{
printf("没有找到要删除的数据!n");
getchar();
}
}
else if(p1==head){
head=p1->next;
printf("是否确定从文件中彻底删除该图书?(y/n)n");
getchar();
scanf("%c",&b);
switch(b){
case 'n':
break;
case 'y':
fprint(head);
printf("删除成功!n");
getchar();
break;
}
}
else{
p2->next=p1->next;
printf("是否确定从文件中彻底删除该图书?(y/n)n");
getchar();
scanf("%c",&b);
switch(b){
case 'n':
break;
case 'y':
fprint(head);
printf("删除成功!n");
getchar();
break;
}
}
}
}
{
int a;
printf(" ==========================================================n");
printf(" ========================================================n");
printf(“请输入所选择的编号:”);
scanf("%d",&a);
getchar();
switch(a){
case 0:
break;
case 1:
num_chaxun(head);
break;
case 2:
bname_chaxun(head);
break;
case 3:
sort_chaxun(head);
break;
case 4:
wname_chaxun(head);
break;
case 5:
time_chaxun(head);
break;
default:
printf(“您的输入有误!n”);
break;
}
}
{
int a;
printf(“请选择您要查询的图书编号:”);
scanf("%d",&a);
getchar();
p=head;
while(p!=NULL){
if(p->numa)
break;
p=p->next;
}
if(p==NULL){
printf("没有找到该编号的图书!n");
}
else{
printf(" 你所查询的图书信息如下n");
printf(" =========================================================================n");
printf(" =========================================================================n");
}
}
{
char a[50];
int flag=0;
printf(“请选择您要查询的图书名:”);
gets(a);
p=head;
while(p!=NULL){
if(strcmp(p->bname,a)0){
flag=1;
break;
}
p=p->next;
}
if(flag0){
printf(“没有找到该图书名的图书!n”);
}
else{
printf(" 你所查询的图书信息如下n");
printf(" =========================================================================n");
while(p!=NULL){
if(strcmp(p->bname,a)==0){
}
p=p->next;
}
printf(" =========================================================================n");
}
}
{
char a[50];
int flag=0;
printf(“请选择您要查询的图书作者名:”);
gets(a);
p=head;
while(p!=NULL){
if(strcmp(p->wname,a)0){
flag=1;
break;
}
p=p->next;
}
if(flag0){
printf(“没有找到该图书名的图书!n”);
}
else{
printf(" 你所查询的图书信息如下n");
printf(" =========================================================================n");
while(p!=NULL){
if(strcmp(p->wname,a)==0){
flag=1;
}
p=p->next;
}
printf(" =========================================================================n");
}
}
{
char a[50];
int flag=0;
printf(“请选择您要查询的图书类别:”);
gets(a);
p=head;
while(p!=NULL){
if(strcmp(p->sort,a)0){
flag=1;
break;
}
p=p->next;
}
if(flag0){
printf(“没有找到该图书名的图书!n”);
}
else{
printf(" 你所查询的图书信息如下n");
printf(" =========================================================================n");
while(p!=NULL){
if(strcmp(p->sort,a)==0){
flag=1;
}
p=p->next;
}
printf(" =========================================================================n");
}
}
{
int a,flag=0;
printf(“请选择您要查询的图书出版时间:”);
scanf("%d",&a);
getchar();
p=head;
while(p!=NULL){
if(p->timea){
flag=1;
break;
}
p=p->next;
}
if(flag0){
printf(“没有找到该图书名的图书!n”);
}
else{
printf(" 你所查询的图书信息如下n");
printf(" =========================================================================n");
while(p!=NULL){
if(p->time==a){
flag=1;
}
p=p->next;
}
printf(" =========================================================================n");
}
}
{
int a,b;
char c;
printf(“请输入要修改的图书编号:”);
scanf("%d",&a);
p=head;
while(p!=NULL){
if(p->numa)
break;
p=p->next;
}
if(pNULL){
printf(“没有找到该编号的图书!n”);
getchar();
}
else{
printf(" ============================================================n");
printf(" ============================================================n");
printf(“请选择你要修改的信息编号:”);
scanf("%d",&b);
getchar();
switch(b){
case 1:
printf(“请输入新编号:”);
scanf("%d",&p->num);
printf(“修改成功!n”);
getchar();
break;
case 2:
printf(“请输入新图书名:”);
gets(p->bname);
printf(“修改成功!n”);
break;
case 3:
printf(“请输入新作者名:”);
gets(p->wname);
printf(“修改成功!n”);
break;
case 4:
printf(“请输入新出版社:”);
gets(p->press);
printf(“修改成功!n”);
break;
case 5:
printf(“请输入新类别:”);
gets(p->sort);
printf(“修改成功!n”);
break;
case 6:
printf(“请输入新出版时间:”);
scanf("%d",&p->time);
printf(“修改成功!n”);
getchar();
break;
case 7:
printf(“请输入新价格:”);
scanf("%f",&p->price);
printf(“修改成功!n”);
getchar();
break;
case 8:
printf(“请输入新图书编号:”);
scanf("%d",&p->num);
printf(“请输入新图书名:”);
scanf("%s",p->bname);
getchar();
printf(“请输入新作者名:”);
scanf("%s",p->wname);
getchar();
printf(“请输入新出版社:”);
scanf("%s",p->press);
getchar();
printf(“请输入新类别:”);
scanf("%s",p->sort);
getchar();
printf(“请输入新出版时间:”);
scanf("%d",&p->time);
getchar();
printf(“请输入新价格:”);
scanf("%f",&p->price);
getchar();
printf(“修改成功!n”);
getchar();
break;
case 0:
break;
default :
printf(“您的输入有误!n”);
break;
}
printf(“是否将修改后的信息保存到文件中?(y/n)n”);
scanf("%c",&c);
getchar();
switch©{
case ‘n’:
break;
case ‘y’:
fprint(head);
printf(“保存成功!n”);
getchar();
break;
}
}
}
{
int a;
printf(" ================================================================n");
printf(" ================================================================n");
printf(“请输入您选择的编号:”);
scanf("%d",&a);
getchar();
switch(a){
case 0:
break;
case 1:
num_paixu(head);
break;
case 2:
time_paixu(head);
break;
case 3:
price_paixu(head);
break;
case 4:
bname_paixu(head);
break;
case 5:
wname_paixu(head);
break;
default:
printf(“您的输入有误!n”);
break;
}
}
{
int i,k,index,n=0;
char b;
p1=head;
for(p=head;p;p=p->next)
n++;
for(i=0;i
p1=p1->next;
}
for(k=0;k
for(i=k+1;i
index=i;
}
temp=a[index];
a[index]=a[k];
a[k]=temp;
}
printf(“排序成功!n”);
printf(“是否显示排序结果?(y/n)n”);
scanf("%s",&b);
getchar();
switch(b){
case ‘n’:
break;
case ‘y’:
printf(" ================================================================n");
for(i=0;i
printf(" ================================================================n");
break;
default:
printf(“您的输入有误!n”);
break;
}
}
{
int i,k,index,n=0;
char b;
p1=head;
for(p=head;p;p=p->next)
n++;
for(i=0;i
p1=p1->next;
}
for(k=0;k
for(i=k+1;i
index=i;
}
temp=a[index];
a[index]=a[k];
a[k]=temp;
}
printf(“排序成功!n”);
printf(“是否显示排序结果?(y/n)n”);
scanf("%s",&b);
getchar();
switch(b){
case ‘n’:
break;
case ‘y’:
printf(" ===============================================================n");
for(i=0;i
printf(" ===============================================================n");
break;
default:
printf(“您的输入有误!n”);
break;
}
}
{
int i,k,index,n=0;
char b;
p1=head;
for(p=head;p;p=p->next)
n++;
for(i=0;i
p1=p1->next;
}
for(k=0;k
for(i=k+1;i
index=i;
}
temp=a[index];
a[index]=a[k];
a[k]=temp;
}
printf(“排序成功!n”);
printf(“是否显示排序结果?(y/n)n”);
scanf("%s",&b);
getchar();
switch(b){
case ‘n’:
break;
case ‘y’:
printf(" ===============================================================n");
for(i=0;i
printf(" ===============================================================n");
break;
default:
printf(“您的输入有误!n”);
break;
}
}
{
int i,k,index,n=0;
char b;
p1=head;
for(p=head;p;p=p->next)
n++;
for(i=0;i
p1=p1->next;
}
for(k=0;k
for(i=k+1;i
index=i;
}
temp=a[index];
a[index]=a[k];
a[k]=temp;
}
printf(“排序成功!n”);
printf(“是否显示排序结果?(y/n)n”);
scanf("%s",&b);
getchar();
switch(b){
case ‘n’:
break;
case ‘y’:
printf(" ===============================================================n");
for(i=0;i
printf(" ===============================================================n");
break;
default:
printf(“您的输入有误!n”);
break;
}
}
{
int i,k,index,n=0;
char b;
p1=head;
for(p=head;p;p=p->next)
n++;
for(i=0;i
p1=p1->next;
}
for(k=0;k
for(i=k+1;i
index=i;
}
temp=a[index];
a[index]=a[k];
a[k]=temp;
}
printf(“排序成功!n”);
printf(“是否显示排序结果?(y/n)n”);
scanf("%s",&b);
getchar();
switch(b){
case ‘n’:
break;
case ‘y’:
printf(" ===============================================================n");
for(i=0;i
printf(" ===============================================================n");
break;
default:
printf(“您的输入有误!n”);
break;
}
}