|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
比如进行注册后退出,然后再打开程序登陆,但登不了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[48];
float price;
struct Date date;
char publisher[48];
struct Book*pnext;
};
struct USER
{
unsigned int id;
unsigned int password;
struct USER *pnext;
};
void read_bookfile(struct Book**pphead)
{
FILE*fp;
if((fp=fopen("图书信息.txt","r"))==NULL)
{
if((fp=fopen("图书信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
fclose(fp);
if((fp=fopen("图书信息.txt","r"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
}
while(fgetc(fp)!=EOF)
{ struct Book *book=(struct Book*)malloc(sizeof(struct Book));
fscanf(fp,"书名:%s\n",book->title);
fscanf(fp,"作者:%s\n",book->author);
fscanf(fp,"售价:%f\n",&book->price);
fscanf(fp,"出版日期:%d-%d-%d\n",&book->date.year,&book->date.month,&book->date.day);
fscanf(fp,"出版社:%s\n\n",book->publisher);
book->pnext=*pphead;
*pphead=book;
}
fclose(fp);
}
void write_bookfile(struct Book*book)
{
FILE*fp;
if((fp=fopen("图书信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
while(book!=NULL)
{
fprintf(fp,"书名:%s\n",book->title);
fprintf(fp,"作者:%s\n",book->author);
fprintf(fp,"售价:%.2f\n",book->price);
fprintf(fp,"出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
fprintf(fp,"出版社:%s\n\n",book->publisher);
book=book->pnext;
}
fclose(fp);
}
void read_userfile(struct USER**pphead)
{
FILE*fp;
int count=1;
if((fp=fopen("用户信息.txt","r"))==NULL)
{
if((fp=fopen("用户信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
fclose(fp);
if((fp=fopen("用户信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
}
while(fgetc(fp)!=EOF)
{
struct USER *user=(struct USER*)malloc(sizeof(struct USER));
fscanf(fp,"--第%d位用户信息--\n 账号:%u",&count,&user->id);
fscanf(fp,"密码:%u\n",&user->password);
user->pnext=*pphead;
*pphead=user;
}
fclose(fp);
}
void write_userfile(struct USER*user)
{
FILE*fp;
if((fp=fopen("用户信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
while(user!=NULL)
{
int count=1;
fprintf(fp,"--第%d位用户信息--\n 账号:%u ",count,user->id);
fprintf(fp,"密码:%u\n",user->password);
user=user->pnext;
count++;
}
fclose(fp);
}
void print_log(void)
{
printf("\n\n----------------欢迎来到图书管理系统----------------------\n\n\n");
printf(" 1 登录\n");
printf(" 2 注册\n");
printf(" 3 退出\n\n\n");
printf(" ---请输入操作前的序号进行相应操作---\n\n");
}
void AddUser(struct USER**pphead)
{
struct USER*newuser=(struct USER*)malloc(sizeof(struct USER));
if(newuser==NULL)
{
printf("分配内存失败!\n");
exit(EXIT_FAILURE);
}
printf("--注册新账户--\n");
printf("账户:");
scanf("%u",&newuser->id);
printf("密码:");
scanf("%u",&newuser->password);
newuser->pnext=*pphead;
*pphead=newuser;
putchar('\n');
}
void log_on(unsigned int*id,unsigned int*password)
{
printf("——请输入账号和密码(未注册请先注册账户)——\n");
printf("账号:");
scanf("%u",id);
printf("密码:");
scanf("%u",password);
putchar('\n');
}
int confirm_user(struct USER*user,unsigned int id,unsigned int password)
{
while(1)
{
if(user==NULL) //要放前面
{
return -1;
}
if(id==user->id&&password==user->password)
{
return 1;
}
user=user->pnext;
}
}
int choose_num(struct USER**pphead,unsigned int *id,unsigned int *password)
{
int key=-1,kiss;
do{
printf("输入序号:");
scanf("%d",&kiss);
putchar('\n');
switch(kiss)
{
case 1:log_on(id,password);key=confirm_user(*pphead,*id,*password);
if(key!=1)
{
printf("账号或密码错误!\n\n");
}
break; //+break
case 2:AddUser(pphead);break;
case 3:write_userfile(*pphead);exit(1);break;
default:printf("输入指令错误!\n");
}
}while(key!=1);
return key;
}
void print_meau(void)
{
putchar('\n');
printf("%46c图书管理系统\n", ' ');
printf("\n\n");
printf("* * * * * * * * * * * * * * * * * * *\n");
printf("* 1 录入图书 *\n");
printf("* 2 打印已录入图书信息 *\n");
printf("* 3 搜素图书 *\n");
printf("* 4 删除已录入图书 *\n");
printf("* 5 退出程序 *\n");
printf("* * * * * * * * * * * * * * * * * * *\n");
printf(" \n\n ---输入功能前的相应序号进行操作--- \n\n");
}
void ReadBook(struct Book**pphead)
{
struct Book*book = (struct Book*)malloc(sizeof(struct Book));
printf("---开始录入书籍---\n");
getchar();
printf("请输入书名:");
gets(book->title);
printf("请输入作者:");
gets(book->author);
printf("请输入售价:");
scanf("%f", &book->price);
printf("请输入出版日期(按“ 2018-12-12 ”的格式输入):");
scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
getchar(); //重要
printf("请输入出版社:");
//scanf("%s",book->publisher);
gets(book->publisher);
if(*pphead == NULL)
{
*pphead = book;
book->pnext = NULL;
}
else
{
struct Book*temp = *pphead;
while(1)
{
if(temp->pnext == NULL)
{
break;
}
temp = temp->pnext;
}
temp->pnext = book;
book->pnext = NULL;
}
}
void print_book(struct Book*phead)
{
struct Book *book = phead;
printf("\n---开始打印图书信息---\n\n");
if(phead == NULL)
{
printf("还未录入图书!\n");
}
else
{
while(1)
{
if(book == NULL)
{
break;
}
else
{
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
printf("售价:%.2f", book->price);
printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
printf("出版社:%s\n", book->publisher);
putchar('\n');
book = book->pnext;
}
}
}
printf("\n---打印完毕---\n");
}
void scanf_search(char inform[])
{
printf("请输入搜索图书的的书名或作者:");
getchar(); //没有的话会异常退出
gets(inform);
}
struct Book*SearchBook(struct Book*book,char inform[]) //搜索图书并返回指向该图书信息的指针,未找到则返回NULL
{
while(1)
{
if(book == NULL)
{
return book;
}
if(!strcmp(book->title, inform) || !strcmp(book->author, inform))
{
return book;
}
book = book->pnext;
}
}
void print_search(struct Book*book,char inform[]) //打印被搜索的图书信息
{
if(book == NULL)
{
printf("未找到图书信息!\n");
}
else
{
putchar('\n');
printf("---已找到被搜索图书,图书信息如下---\n\n");
do{
printf("书名:%s\n",book->title);
printf("作者:%s\n",book->author);
printf("售价:%.2f\n",book->price);
printf("出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
printf("出版社:%s\n",book->publisher);
putchar('\n');
}while((book=SearchBook(book->pnext,inform))!=NULL);
}
}
void cutoff_book(struct Book**pphead) //删除被搜索的图书信息 ,并释放内存
{
char cutbook[100];
struct Book *pervious = NULL;
struct Book *current = *pphead;
printf("请输入你要删除的图书的书名或作者:");
getchar();
gets(cutbook);
/* if(current == NULL)
{
printf("未找到需要删除的图书\n");
return; //返回
}*/
while(1)
{
if(current == NULL)
{
break;
}
if(!strcmp(current->title, cutbook) || !strcmp(current->author, cutbook))
{
break;
}
pervious = current;
current = current->pnext;
}
if(current == NULL)
{
printf("未找到需要删除的图书!\n");
}
else
{
if(pervious==NULL)
{
*pphead=current->pnext;
}
else
{
pervious->pnext=current->pnext;
}
free(current);
}
}
void releasebook(struct Book*book) //释放链表空间
{
struct Book *temp = NULL;
while(book != NULL)
{
temp = book;
book = book->pnext;
free(temp);
}
}
void releaseuser(struct USER*user)
{
struct USER *temp = NULL;
while(user!= NULL)
{
temp = user;
user = user->pnext;
free(temp);
}
}
int main(void)
{
int N,enter=-1;
char inform[128];
struct Book*phead = NULL;
struct USER *pheaduser=NULL;
unsigned int id,password;
read_bookfile(&phead);
read_userfile(&pheaduser);
print_log();
enter= choose_num(&pheaduser,&id,&password);
if(enter==1)
{
printf("---登录成功---\n\n\n");
print_meau();
while(1)
{
printf("请输入指令:");
scanf("%d", &N);
putchar('\n');
switch(N)
{
case 1:ReadBook(&phead); break;
case 2:print_book(phead); break;
case 3:scanf_search(inform);print_search(SearchBook(phead,inform),inform); break;
case 4:cutoff_book(&phead); break;
case 5:write_bookfile(phead);write_userfile(pheaduser);releasebook(phead);releaseuser(pheaduser);exit(1); break;
default:printf("指令错误\n"); break;
}
putchar('\n');
}
}
return 0;
}
|
|