哎!!求帮助,我给您源代码,求在vc上运行,输入一下

altairheat 2012-03-03 11:33:15

#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();
}



等下还有!!!!2个txt
...全文
111 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
面包大师 2012-03-04
  • 打赏
  • 举报
回复
#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();
}

楼主可以自己学着添加点打印,比如在输入文本文件的前边再打印一下,看看是不是自己想要的结果,设置断点,单步调试下
altairheat 2012-03-04
  • 打赏
  • 举报
回复
就是太多输出错误了。。。。有事空白,有事乱输出
duke56 2012-03-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 czh3642210 的回复:]
C/C++ code
#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 SearchIS……
[/Quote]


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);
}
altairheat 2012-03-04
  • 打赏
  • 举报
回复
大恩不言谢
jxnuzhouguohong 2012-03-04
  • 打赏
  • 举报
回复
我添加数据进去没有保存在文本下哦,还是我操作失误?
altairheat 2012-03-03
  • 打赏
  • 举报
回复
请帮忙的人打开程序按照提示试一下add,del,amend(修改)这三个功能,他的输出会有很大问题,数据乱了,求帮助,谢谢了,我都试了3天了
altairheat 2012-03-03
  • 打赏
  • 举报
回复
11111111111111
a
b
c
20120101
10.0
11111111111112
a
b
c
20120101
10.0
11111111111113
a
b
c
20120101
10.0
请按这样的格式保存为bookdatabase.txt,谢了
请按:
123456
的格式保存为pwf.txt
放在源文件哪里,请各位高人编译运行一下,由于源代码过长,我把一些不必要的函数删除了,如果这个源代码,变异错误,请稍作修改

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧