70,037
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define L sizeof(struct book)
void submenu1();
void submenu2();
void Overview();
void Add();
void Del();
void Amend();
void SearchISBN();
void Searchname();
void Searchpress();
void Save();
void Reload();
void End();
struct book *pt;
struct book *pt2;
struct book
{
char ISBN[13];
char bookname[50];
char bookauthor[50];
char bookpress[50];
char dateofpublication[8];
char price[6];
struct book *next;
};
struct book *creat()
{
FILE *lf;int n=0;
struct book *head;
struct book *p1,*p2;
head=NULL;
if((lf=fopen("bookdatabase.txt","r"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
p1=(p2=(struct book *)malloc(L));
if(fscanf(lf,"%s\n",&p1->ISBN)==EOF){printf("No file detected");exit(0);}
fscanf(lf,"%s\n",&p1->bookname);
fscanf(lf,"%s\n",&p1->bookauthor);
fscanf(lf,"%s\n",&p1->bookpress);
fscanf(lf,"%s\n",&p1->dateofpublication);
fscanf(lf,"%s\n",&p1->price);
while(feof(lf)==0)
{
if(n==0){head=p1;n++;}
else {p2->next=p1;}
p2=p1;
p1=(struct book *)malloc(L);
fscanf(lf,"%s\n",&p1->ISBN);
fscanf(lf,"%s\n",&p1->bookname);
fscanf(lf,"%s\n",&p1->bookauthor);
fscanf(lf,"%s\n",&p1->bookpress);
fscanf(lf,"%s\n",&p1->dateofpublication);
fscanf(lf,"%s\n",&p1->price);
}
p2->next=NULL;
pt2=p2;
printf("Loaded file successfully\n");
fclose(lf);
return head;
}
int login(char password[])
{
char temp[20];int i=0;
for(i;i<20;i++){temp[i]='\0';}
printf("Please input your password\n");
gets(temp);
if(strcmp(password,temp)==0)
{
printf("Login successfully,welcome!\n");
return 1;
}
else return -1;
}
void setpassword(char password[])
{
char temp[20];int i=0;int j=0;
FILE *p;
system("cls");
if((p=fopen("pwf.txt","w"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
for(i;i<20;i++){temp[i]='\0';}
printf("Please input your new password\n");
getchar();
gets(temp);
fputs(temp,p);
printf("This is your new password,please remember it\n");
puts(temp);
fclose(p);
}
void menu()
{
char choose;
system("cls");
printf("1.Revise(add,del,amend)\n2.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':submenu1();break;
case '2':exit(0);
}
}
void submenu1()
{
char choose;
system("cls");
printf("1.Add\n2.Del\n3.Amend\n4.Menu\n5.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':Add();break;
case '2':Del();break;
case '3':Amend();break;
case '4':menu();
case '5':exit(0);
}
}
void Add()
{
struct book *p0;char yn;
system("cls");
p0=(struct book *)malloc(L);
printf("please input following this order,ending with enter:\n");
printf("ISBN:");scanf("%s",&p0->ISBN);
printf("Name of the book:");scanf("%s",&p0->bookname);
printf("Author of the book:");scanf("%s",&p0->bookauthor);
printf("Press of the book:");scanf("%s",&p0->bookpress);
printf("Date of publication(eg.20120101):");scanf("%s",&p0->dateofpublication);
printf("Price(default RMB,just input the value,eg.10.0):");scanf("%s",&p0->price);getchar();
printf("Are you really want to add this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
pt2->next=p0;
p0->next=NULL;
pt2=p0;
printf("Added successfully\n");
}
End();
}
void Del()
{
struct book *p1,*p2,*p3;char yn;
system("cls");
p3=(struct book *)malloc(L);
printf("Please input the ISBN of the book you want to delete:");
scanf("%s",&p3->ISBN);getchar();
printf("Are you really want to delete this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
p1=pt;
if(strcmp(p1->ISBN,p3->ISBN)==0){pt=p1->next;printf("Deleted successfully\n");}
else
{
while(p1->next!=NULL)
{
p2=p1;p1=p1->next;
if(strcmp(p1->ISBN,p3->ISBN)==0){p2->next=p1->next;printf("Deleted successfully\n");break;}
}
}
}
End();
}
void Amend()
{
struct book *p1,*p2;char yn;
system("cls");
p2=(struct book *)malloc(L);
p1=pt;
printf("Please input the ISBN of the book you want to amend:");
scanf("%s",&p2->ISBN);getchar();
printf("Are you really want to amend this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
while(p1->next!=NULL)
{
if(strcmp(p1->ISBN,p2->ISBN)==0)
{
printf("Please input the new ISBN of the book:");scanf("%s",&p1->ISBN);
printf("Please input the name:");scanf("%s",&p1->bookname);
printf("Please input the author:");scanf("%s",&p1->bookauthor);
printf("Please input the press:");scanf("%s",&p1->bookpress);
printf("Please input the date of publication:");scanf("%s",&p1->dateofpublication);
printf("Please input price:");scanf("%s",&p1->price);
printf("Amended successfully\n");
break;
}
else p1=p1->next;
}
}
End();
}
}
void Save()
{
FILE *sf;struct book *p1;
p1=pt;
if((sf=fopen("bookdatabase.txt","w"))==NULL)
{
printf("Fatal Error:save file failed\nProgram will exit now\n");
exit(0);
}
while(p1->next!=NULL)
{
fprintf(sf,"%s\n",p1->ISBN);
fprintf(sf,"%s\n",p1->bookname);
fprintf(sf,"%s\n",p1->bookauthor);
fprintf(sf,"%s\n",p1->bookpress);
fprintf(sf,"%s\n",p1->dateofpublication);
fprintf(sf,"%f\n",p1->price);
p1=p1->next;
}
fclose(sf);
}
void Reload()
{
creat();
}
void End()
{
char key;
printf("Back to the menu,please input '1';to exit ,please input '0':");getchar();
Here:key=getchar();
switch(key)
{
case '1':menu();
case '0':exit(0);
default:printf("Invalid input,please input again:");goto Here;
}
}
main()
{
char enter;char password[20];int key=0;int i=0;
FILE *fp;
pt=creat();
if((fp=fopen("pwf.txt","r+"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
fgets(password,20,fp);
printf("Load file successed,please give the next instruction\n");
key=login(password);
if(key==-1)
{
printf("Fatal Error:password match failed\nProgram will exit now\n");
}
if(key==1)
{
system("cls");
printf("1.Menu\n2.Reset my password\n3.Exit\n");
printf("Please give your next instruction\n");
Here:enter=getchar();
switch(enter)
{
case '1':menu();
case '2':setpassword(password);
case '3':exit(0);
default:printf("Invalid input,please input again:");goto Here;
}
}
fclose(fp);
Save();
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define L sizeof(struct book)
void submenu1();
void submenu2();
void Overview();
void Add();
void Del();
void Amend();
void SearchISBN();
void Searchname();
void Searchpress();
void Save();
void Reload();
void End();
struct book *pt;
struct book *pt2;
struct book
{
char ISBN[16];
char bookname[50];
char bookauthor[50];
char bookpress[50];
char dateofpublication[10];
char price[6];
struct book *next;
};
struct book *creat()
{
FILE *lf;int n=0;
struct book *head;
struct book *p1,*p2;
head=NULL;
if((lf=fopen("bookdatabase.txt","r"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
p2=(struct book *)malloc(L);
memset(p2, 0, L);
p1=p2;
if(fscanf(lf,"%s\n",p1->ISBN)==EOF){printf("No file detected");exit(0);}
printf("%s\n",&p1->ISBN);
fscanf(lf,"%s\n",p1->bookname);
printf("%s\n",p1->bookname);
fscanf(lf,"%s\n",p1->bookauthor);
printf("%s\n",p1->bookauthor);
fscanf(lf,"%s\n",p1->bookpress);
printf("%s\n",p1->bookpress);
fscanf(lf,"%s\n",p1->dateofpublication);
printf("%s\n",p1->dateofpublication);
fscanf(lf,"%s\n",p1->price);
printf("%s\n",p1->price);
while(feof(lf)==0)
{
if(n==0){head=p1;n++;}
else {p2->next=p1;}
p2=p1;
p1=(struct book *)malloc(L);
memset(p1, 0, L);
fscanf(lf,"%s\n",p1->ISBN);
printf("%s\n",p1->ISBN);
fscanf(lf,"%s\n",p1->bookname);
printf("%s\n",p1->bookname);
fscanf(lf,"%s\n",p1->bookauthor);
printf("%s\n",p1->bookauthor);
fscanf(lf,"%s\n",p1->bookpress);
printf("%s\n",p1->bookpress);
fscanf(lf,"%s\n",&p1->dateofpublication);
printf("%s\n",&p1->dateofpublication);
fscanf(lf,"%s\n",p1->price);
printf("%s\n",p1->price);
}
p2->next=NULL;
pt2=p2;
printf("Loaded file successfully\n");
fclose(lf);
return head;
}
int login(char password[])
{
char temp[20];int i=0;
for(i=0;i<20;i++){temp[i]='\0';}
printf("Please input your password\n");
gets(temp);
if(strcmp(password,temp)==0)
{
printf("Login successfully,welcome!\n");
return 1;
}
else return -1;
}
void setpassword(char password[])
{
char temp[20];int i=0;int j=0;
FILE *p;
system("cls");
if((p=fopen("pwf.txt","w"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
for(i;i<20;i++){temp[i]='\0';}
printf("Please input your new password\n");
getchar();
gets(temp);
fputs(temp,p);
printf("This is your new password,please remember it\n");
puts(temp);
fclose(p);
}
void menu()
{
char choose;
system("cls");
printf("1.Revise(add,del,amend)\n2.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':submenu1();break;
case '2':exit(0);
}
}
void submenu1()
{
char choose;
system("cls");
printf("1.Add\n2.Del\n3.Amend\n4.Menu\n5.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':Add();break;
case '2':Del();break;
case '3':Amend();break;
case '4':menu();
case '5':exit(0);
}
}
void Add()
{
struct book *p0;char yn;
system("cls");
p0=(struct book *)malloc(L);
printf("please input following this order,ending with enter:\n");
printf("ISBN:");
scanf("%s",&p0->ISBN);
printf("Name of the book:");
scanf("%s",&p0->bookname);
printf("Author of the book:");
scanf("%s",&p0->bookauthor);
printf("Press of the book:");
scanf("%s",&p0->bookpress);
printf("Date of publication(eg.20120101):");
scanf("%s",&p0->dateofpublication);
printf("Price(default RMB,just input the value,eg.10.0):");
scanf("%s",&p0->price);getchar();
printf("Are you really want to add this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
pt2->next=p0;
p0->next=NULL;
pt2=p0;
printf("Added successfully\n");
}
End();
}
void Del()
{
struct book *p1,*p2,*p3;char yn;
system("cls");
p3=(struct book *)malloc(L);
printf("Please input the ISBN of the book you want to delete:");
scanf("%s",&p3->ISBN);getchar();
printf("Are you really want to delete this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
p1=pt;
if(strcmp(p1->ISBN,p3->ISBN)==0){pt=p1->next;printf("Deleted successfully\n");}
else
{
while(p1->next!=NULL)
{
p2=p1;p1=p1->next;
if(strcmp(p1->ISBN,p3->ISBN)==0){p2->next=p1->next;printf("Deleted successfully\n");break;}
}
}
}
End();
}
void Amend()
{
struct book *p1,*p2;char yn;
system("cls");
p2=(struct book *)malloc(L);
p1=pt;
printf("Please input the ISBN of the book you want to amend:");
scanf("%s",&p2->ISBN);getchar();
printf("Are you really want to amend this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
while(p1->next!=NULL)
{
if(strcmp(p1->ISBN,p2->ISBN)==0)
{
printf("Please input the new ISBN of the book:");scanf("%s",&p1->ISBN);
printf("Please input the name:");scanf("%s",&p1->bookname);
printf("Please input the author:");scanf("%s",&p1->bookauthor);
printf("Please input the press:");scanf("%s",&p1->bookpress);
printf("Please input the date of publication:");scanf("%s",&p1->dateofpublication);
printf("Please input price:");scanf("%s",&p1->price);
printf("Amended successfully\n");
break;
}
else p1=p1->next;
}
}
End();
}
void Save()
{
FILE *sf;struct book *p1;
p1=pt;
if((sf=fopen("bookdatabase.txt","w"))==NULL)
{
printf("Fatal Error:save file failed\nProgram will exit now\n");
exit(0);
}
while(p1->next!=NULL)
{
fprintf(sf,"%s\n",p1->ISBN);
fprintf(sf,"%s\n",p1->bookname);
fprintf(sf,"%s\n",p1->bookauthor);
fprintf(sf,"%s\n",p1->bookpress);
fprintf(sf,"%s\n",p1->dateofpublication);
fprintf(sf,"%f\n",p1->price);
p1=p1->next;
}
fclose(sf);
}
void Reload()
{
creat();
}
void End()
{
char key;
printf("Back to the menu,please input '1';to exit ,please input '0':");getchar();
Here:key=getchar();
switch(key)
{
case '1':menu();
case '0':exit(0);
default:printf("Invalid input,please input again:");goto Here;
}
}
main()
{
char enter;char password[20];int key=0;int i=0;
FILE *fp;
pt=creat();
if((fp=fopen("pwf.txt","r+"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
fgets(password,20,fp);
printf("Load file successed,please give the next instruction\n");
key=login(password);
if(key==-1)
{
printf("Fatal Error:password match failed\nProgram will exit now\n");
}
if(key==1)
{
system("cls");
printf("1.Menu\n2.Reset my password\n3.Exit\n");
printf("Please give your next instruction\n");
Here:enter=getchar();
switch(enter)
{
case '1':menu();
case '2':setpassword(password);
case '3':exit(0);
default:printf("Invalid input,please input again:");goto Here;
}
}
fclose(fp);
Save();
}
void Save()
{
FILE *sf;struct book *p1;
p1=pt;
if((sf=fopen("bookdatabase.txt","w"))==NULL)
{
printf("Fatal Error:save file failed\nProgram will exit now\n");
exit(0);
}
while(p1->next!=NULL)//这位朋友的代码会丢掉节点的最后个节...新增加的都没法保存,p1!=NULL
{
fprintf(sf,"%s\n",p1->ISBN);
fprintf(sf,"%s\n",p1->bookname);
fprintf(sf,"%s\n",p1->bookauthor);
fprintf(sf,"%s\n",p1->bookpress);
fprintf(sf,"%s\n",p1->dateofpublication);
fprintf(sf,"%f\n",p1->price);
p1=p1->next;
}
fclose(sf);
}