求指正,这是我写的一个图书馆管理系统。其中指针用的比较混乱,但我不知道哪里错了
#include<stdio.h>#include<stdlib.h>
#include <string.h>
#include <conio.h>
#define N sizeof(struct books)
typedef struct books
{
long number;
int gress;
int price;
char name[60];
char writer[30];
char press[30];
char kind[30];
struct books *next;
}BOOK;
typedef struct id
{
long num;
char name[30];
char grade[30];
char major[30];
char bkhave[11];
long bknum[11];
int i;
/*char key[9];*/
struct id *next;
}ID;
BOOK* creatlink(void)
{
BOOK *p1,*p2,*head;
int i=1;
char cho;
head=p2=(BOOK *)malloc(N);
head->next=NULL;
do
{
system("cls");
p1=(BOOK *)malloc(N);
printf("\n请输入第%d本书的索书号 \n",i);
scanf("%ld",&p1->number);
printf("\n请输入第%d本书的总量 \n",i);
scanf("%d",&p1->gress);
printf("\n请输入第%d本书的单价 \n",i);
scanf("%d",&p1->price);
printf("\n请输入第%d本书的书名 \n",i);
scanf("%s",p1->name);
printf("\n请输入第%d本书的作者 \n",i);
scanf("%s",p1->writer);
printf("\n请输入第%d本书的出版社 \n",i);
scanf("%s",p1->press);
printf("\n请输入第%d本书的种类 \n",i);
scanf("%s",p1->kind);
p2->next=p1;
p2=p1;
printf("\n 是否继续?(Y/N):");
system("cls");
cho=getche();
printf("\n");
++i;
}while(cho=='y'||cho=='Y');
p2->next=NULL;
free(p1);
return head;
}
ID* creatst(void)
{
ID *p1,*p2,*head;
int i=1;
char cho;
head=p2=(ID *)malloc(N);
head->next=NULL;
do
{
system("cls");
p1=(ID *)malloc(N);
printf("\n请输入第%d个学生的学号 \n",i);
scanf("%ld",&p1->num);
printf("\n请输入第%d个学生的姓名 \n",i);
scanf("%s",&p1->name);
printf("\n请输入第%d个学生的年级 \n",i);
scanf("%s",&p1->grade);
printf("\n请输入第%d个学生的专业 \n",i);
scanf("%s",p1->major);
p2->next=p1;
p2=p1;
printf("\n 是否继续?(Y/N):");
system("cls");
cho=getche();
printf("\n");
++i;
}while(cho=='y'||cho=='Y');
p2->next=NULL;
free(p1);
return head;
}
void savest(ID *b)
{
ID *p;
FILE *fp;
char filename[30];
printf("\n请输入要保存的文件名...");
scanf("%s",filename);
if((fp=fopen(filename,"wt"))==NULL)
{
printf("写文件出错...按任意键退出!");
getche();
exit(1);
}
for(p=b->next;p!=NULL;p=p->next)
fprintf(fp,"%ld%s%s%s%s",&p->num,&p->name,&p->grade,p->major);
printf("文件已成功保存...按任意键返回");
getche();
fclose(fp);
}
ID * openst(void)
{
ID *head,*p,*q;
FILE *fp;
char filename[30];
printf("请输入要打开的文件名..");
scanf("%s",filename);
if((fp=fopen(filename,"rt"))==NULL)
{
printf("读文件出错..按任意键退出");
getche();
exit(1);
}
head=q=(ID * )malloc(N);
head->next=NULL;
while(!feof(fp))
{
p=(ID *)malloc(N);
fscanf(fp,"%ld%s%s%s%s",&p->num,&p->name,&p->grade,p->major);
q->next=p;
q=p;
}
q->next=NULL;
fclose(fp);
printf("文件以正确读出..按任意键返回");
getch();
return head;
}
int findid( )
{
long num;
ID *p=creatst();
system("cls");
printf("请输入你的的学号: ");
scanf("%ld",&num);
while(p->next!=NULL)
{
p=p->next;
if(p->num==num)
{
printf("学号正确");
return 1;
}
}
return 0;
}
void modifyst(ID *head)
{
long num;
char choice;
ID *p;
p=creatst();
system("cls");
printf("请输入要修改的书的索书号");
scanf("%ld",&num);
while(head->next!=NULL)
{
p=p->next;
if(p->num==num)
{
printf("要修改的书的学号是%ld,姓名是%s,年级是%s,专业是%s,",&p->num,&p->name,&p->grade,p->major);
printf("是否修改?Y/N");
scanf("%c",&choice);
if(choice=='Y'||choice=='y')
{
printf("请输入修改后的信息");
printf("学号");
scanf("%ld",&p->num);
printf("姓名");
scanf("%s",p->name);
printf("年级");
scanf("%s",p->grade);
printf("专业");
scanf("%s",p->major);
printf("文件修改成功!");
}
}
}
}
ID * findst( )
{
long num;
ID *p=creatst();
system("cls");
printf("请输入要查找的学生的学号: ");
scanf("%ld",&num);
while(p->next!=NULL)
{
p=p->next;
if(p->num==num)
{
printf("你所查找的学生的学号是%ld,姓名是%s,年级是%s,专业是%s,",&p->num,&p->name,&p->grade,p->major);
exit(1);
}
}
return p;
}
void borrowbk()
{
long num;
BOOK *p=creatlink();
ID *st=findst();
char want;
system("cls");
printf("请输入要查找的书籍的索书号: ");
scanf("%ld",&num);
while(p->next!=NULL)
{
p=p->next;
if(p->number==num)
{
printf("你所查找的书书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
printf("你要借这本书吗? Y/N");
want=getch();
if(want=='Y'||want=='y')
{ int i=1;
p->gress=p->gress-1;
if(p->gress==0)
{
printf("对不起,您想借的书全部借出。您可以寻找同类书替代");
exit(1);
}
strcpy(st->bkhave+i,p->name);
printf("借书成功!");
++i;
exit(1);
}
}
}
}
void returnbk()
{
long num;
BOOK *p=creatlink();
ID *q=findst();
char want;
system("cls");
printf("请输入要归还的书籍的索书号: ");
scanf("%ld",&num);
while(p->next!=NULL)
{ int i=1;
p=p->next;
if(p->number==num)
{
printf("你所查找的书书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
printf("你要借这本书吗? Y/N");
want=getch();
if(want=='Y'||want=='y')
{
p->gress=p->gress+1;
strcpy(q->bkhave+i,0);
printf("还书成功!");
i--;
exit(1);
}
}
}
}
void savefile(BOOK *b)
{
BOOK *p;
FILE *fp;
char filename[30];
system("cls");
printf("\n请输入要保存的文件名...");
scanf("%s",filename);
if((fp=fopen(filename,"wt"))==NULL)
{
printf("写文件出错...按任意键退出!");
getche();
exit(1);
}
for(p=b->next;p!=NULL;p=p->next)
fprintf(fp,"%ld%d%d%s%s%s%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
printf("文件已成功保存...按任意键返回");
getche();
fclose(fp);
}
BOOK * openlink(void)
{
BOOK *head,*p,*q;
FILE *fp;
char filename[30];
system("cls");
printf("请输入要打开的文件名..");
scanf("%s",filename);
if((fp=fopen(filename,"rt"))==NULL)
{
printf("读文件出错..按任意键退出");
getche();
exit(1);
}
head=q=(BOOK * )malloc(N);
head->next=NULL;
while(!feof(fp))
{
p=(BOOK *)malloc(N);
fscanf(fp,"%ld%d%d%s%s%s%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
q->next=p;
q=p;
}
q->next=NULL;
fclose(fp);
printf("文件以正确读出..按任意键返回");
getch();
return head;
}
void deletebook(BOOK * head)
{
long num=0;
char choice;
BOOK * p;
p=creatlink();
p=p->next;
system("cls");
if(p==NULL)
{
printf("信息出错,按任意键退出");
getch();
exit(1);
}
printf("请输入要删除的书的索书号:");
scanf("%l",num);
while(p!=NULL)
{
if(p->number==num)
{
printf("你要删除的书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
printf("是否删除?Y/N");
scanf("%c",&choice);
if(choice=='Y'||choice=='y')
{
head->next=p->next;
free(p);
printf("文件删除成功!");
}
head=p;
p=p->next;
}
}
}
void modifybk(BOOK *head)
{
long num;
char choice;
BOOK *p;
p=creatlink();
system("cls");
printf("请输入要修改的书的索书号");
scanf("%ld",&num);
while(head->next!=NULL)
{
p=p->next;
if(p->number==num)
{
printf("要修改的书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
printf("是否修改?Y/N");
scanf("%c",&choice);
if(choice=='Y'||choice=='y')
{
printf("请输入修改后的信息");
printf("索书号");
scanf("%ld",&p->number);
printf("总量");
scanf("%d",&p->gress);
printf("单价");
scanf("%d",&p->price);
printf("书名");
scanf("%s",p->name);
printf("作者");
scanf("%s",p->writer);
printf("出版社");
scanf("%s",p->press);
printf("种类");
scanf("%s",p->kind);
printf("文件修改成功!");
}
}
}
}
void fdnum(BOOK *head )
{
long num;
BOOK *p;
system("cls");
printf("请输入要查找的书籍的索书号: ");
scanf("%ld",&num);
while(p->next!=NULL)
{
p=p->next;
if(p->number==num)
{
printf("你所查找的书书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
exit(1);
}
}
}
void fdmane(BOOK *head)
{
char bkname[60];
BOOK *p;
system("cls");
printf("请输入要查找的书籍的书名: ");
scanf("%s",&bkname);
while(p->next!=NULL)
{
p=p->next;
if(strcmp(p->name,bkname))
{
printf("你所查找的书书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
exit(1);
}
}
}
void fdwrier(BOOK *head)
{
char bkwriter[30];
BOOK *p;
system("cls");
printf("请输入要查找的书籍的作者: ");
scanf("%s",&bkwriter);
while(p->next!=NULL)
{
p=p->next;
if(strcmp(p->writer,bkwriter))
{
printf("你所查找的书书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
exit(1);
}
}
}
void fdpress(BOOK *head)
{
char bkpress[30];
BOOK *p;
system("cls");
printf("请输入要查找的书籍的出版社: ");
scanf("%s",&bkpress);
while(p->next!=NULL)
{
p=p->next;
if(strcmp(p->press,bkpress))
{
printf("你所查找的书书的索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
exit(1);
}
}
}
void findbook()
{
int choice;
BOOK *h;
h=creatlink();
do
{
system("cls");
printf("请输入查找方式");
printf("\n按索书号查找请按1--------\n");
printf("\n按书名查找请按2----------\n");
printf("\n按作者查找请按3----------\n");
printf("\n按出版社查找请按4--------\n");
scanf("%d",&choice);
switch(choice)
{
case 1: fdnum(h);break;
case 2: fdmane(h);break;
case 3: fdwrier(h);break;
case 4: fdpress(h);break;
case 0: break;
}
}while(choice=0);
}
void print(BOOK *p)
{
p=p->next;
while(p!=NULL)
{
printf("索书号是%ld,总量有%d,单价为%d,书名是%s,作者是%s,出版社是%s,种类是%s",&p->number,&p->gress,&p->price,p->name,p->writer,p->press,p->kind);
p=p->next;
system("cls");
}
printf("信息已全部输出!");
}
void managesystem()
{
char pass[9]="0",i;
char a[]="password";
printf("请输入密码解锁(8位,自动结束):");
for(i=0;i<8;++i)
{
pass[i]=getch();
printf("%c",2);
}
if(!strcmp(a,pass))
{
int choice=9,i,way1=9,way2=9;
BOOK *headbk;
ID *headst;
for(i=1;choice!=0;++i)
{
system("cls");
printf("\n------欢迎来到管理员界面!\n\n");
printf("\n--------1创建存储书籍信息的文件\n");
printf("\n--------2创建存储学生信息的文件\n");
printf("\n--------3取出书籍信息原文件,并进行后续操作\n");
printf("\n--------4取出学生信息原文件,并进行后续操作\n");
printf("\n--------0退出系统\n");
scanf("%d",choice);
if(choice==1)
{
system("cls");
headbk=creatlink();
savefile(headbk);
}
if(choice==2)
{
system("cls");
headst=creatst();
savest(headst);
}
if(choice==3)
{
headbk=openlink();
for(i=0;way1!=0;++i)
{
system("cls");
printf("欢迎来到书籍管理界面");
/*printf("\n1.添加书籍信息\n");*/
printf("\n2.删除书籍信息\n");
printf("\n3.修改书籍信息\n");
printf("\n4.查询书籍信息\n");
printf("\n5.显示书籍信息\n");
printf("\n0.退出\n");
printf("\n请选择要进行的步骤\n");
scanf("%d",&way1);
switch(way1)
{
/*case 1:h=insert(head);
case 2:savefile(h);break;*/
case 3:deletebook(headbk);
case 4:savefile(headbk);break;
case 5:modifybk(headbk);
case 6:savefile(headbk);break;
case 7:findbook();break;
case 8:print(headbk);break;
case 0:break;
}
}
if(choice==4)
{
system("cls");
headst=openst();
printf("欢迎来到学生管理界面");
printf("1修改学生信息");
printf("2查找学生信息");
scanf("%d",way2);
switch(way2)
{
case 1:modifyst(headst);break;
case 2:findst();break;
}
}
}
}
}
}
void usesystem()
{
ID *headst,*q;
q=findst();
system("cls");
printf("欢迎来到图书馆!\n");
headst=openst();
if(findid())
{
int fashion;
printf("\n借书请按1\n");
printf("\n还书请按2\n");
scanf("%d",&fashion);
switch(fashion)
{
case 1:borrowbk();break;
case 2:returnbk();break;
}
}
}
void main(void)
{
int i;
printf("-----------------管理员请按1-------------\n");
printf("\n-----------------用户请按2---------------\n");
printf("\n-----------------按0结束-----------------\n");
scanf("%d",&i);
switch(i)
{
case 1:managesystem();break;
case 2:usesystem();break;
case 0: break;
}
}