#include<iostream.h>
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#include"conio.h"
#include"string.h"
typedef struct car_info
{
int car_num;
char stud_name[15];
char stud_sex[5];
char stud_major[20];
int stud_grade;
int stud_class;
struct car_info *next;
}Card; /*定义卡号中包含的信息*/
typedef struct lend_info
{
int car_num;
char lend_book[15];
struct lend_info *next;
}Lend; /*记录学生借书信息*/
typedef struct book_info
{
char book_name[15];
char book_author[15];
int tage; /*标志 0为该书已经借出 1表示该书未被借出*/
struct book_info *next;
}Book;
/*从记录卡号的文件读取数据建立链表*/
Card *load_1()
{
FILE *fp;
Card *head,*p,*q;
fp=fopen("card","rb");
if(fp==NULL){printf("error!\n");return 0;}
head=p=q=(Card*)malloc(sizeof(Card));
fread(p,sizeof(Card),1,fp);
while(!feof(fp))
{q=p;
p=(Card*)malloc(sizeof(Card));
fread(p,sizeof(Card),1,fp);
q->next=p;
}
q->next=NULL;
fclose(fp);
return head;
}
/*将修改或删除后的卡号信息存进文件*/
void save_1(Card *head)
{
FILE *fp;
fp=fopen("Card","wb");
while(head!=NULL)
{ fwrite(head,sizeof(Card),1,fp);
head=head->next;
}
fclose(fp);
}
/*添加卡号函数*/
void Add_card()
{
Card p0;
FILE *fp;
char chiose='y';
fp=fopen("card","rb");
if(fp==NULL)fp=fopen("card","wb");
else
{fclose(fp);
fp=fopen("card","ab+");
}
while(chiose=='y'||chiose=='Y')
{
cout<<"卡号 学生姓名 性别 专业 年级 班级!"<<endl;
fflush(stdin);
cin>>p0.car_num>>p0.stud_name>>p0.stud_sex>>p0.stud_major>>p0.stud_grade>>p0.stud_class;
p0.next=NULL;
fwrite(&p0,sizeof(Card),1,fp);
cout<<"添加结束!是否继续:是y或Y,否者结束!"<<endl;
fflush(stdin);
cin>>&chiose;
}
system("cls");
fclose(fp);
}
/*删除卡号信息*/
void del_card()
{
Card *head,*p,*q;
char chiose='y';
int tage=0;
int car_num;
head=load_1(); /*调用函数*/
while(chiose=='y'||chiose=='Y')
{printf("\ninput the delete card num!\n");
cin>>car_num;
p=head;
while(p!=NULL)
{if(p->car_num==car_num)
{tage=1;
if(p==head)head=head->next;
else q->next=p->next;
free(p);break;
}
q=p;p=p->next;
}
if(tage==0)printf("要删除的卡号不存在!\n");
tage=0;
printf("删除结束!是否继续:y或Y继续,否者退出!\n");
fflush(stdin);
cin>>&chiose;
}
save_1(head);
system("cls");
return;
}
/*输出卡号信息*/
void print_card()
{
Card card1;
FILE *fp;
fp=fopen("card","rb");
if(fp==NULL)
{printf("error!\n");return;}
printf("cardinfo:\n卡号 学生姓名 性别 专业 年级 班级!\n");
while(fread(&card1,sizeof(Card),1,fp))
cout<<card1.car_num<<card1.stud_name<<card1.stud_sex<<card1.stud_major<<card1.stud_grade<<card1.stud_class<<endl;
fclose(fp);
}
/*从记录图书文件读取数据建立链表函数*/
Book *load_2()
{
FILE *fp;
Book *head,*p,*q;
fp=fopen("book","rb");
if(fp==NULL){printf("error!\n");return 0;}
head=p=q=(Book*)malloc(sizeof(Book));
fread(p,sizeof(Book),1,fp);
while(!feof(fp))
{q=p;
p=(Book*)malloc(sizeof(Book));
fread(p,sizeof(Book),1,fp);
q->next=p;
}
q->next=NULL;
free(p);
fclose(fp);
return head;
}
/*将修改或删除后的图书信息存进文件*/
void save_2(Book *head)
{
FILE *fp;
fp=fopen("book","wb");
while(head!=NULL)
{fwrite(head,sizeof(Book),1,fp);
head=head->next;
}
fclose(fp);
}
/*添加图书函数*/
void Add_book()
{
Book p0;
FILE *fp;
char chiose='y'; /*对连续添加设置用的判断字符*/
fp=fopen("book","rb");
if(fp==NULL)fp=fopen("book","wb");
else
{fclose(fp);
fp=fopen("book","ab+");
}
while(chiose=='y'||chiose=='Y')
{cout<<"请输入图书信息:bookname bookauthor tage"<<endl;
cin>>p0.book_name>>p0.book_author>>p0.tage;
p0.next=NULL;
fflush(stdin); /*清除缓存*/
fwrite(&p0,sizeof(Book),1,fp);
cout<<"是否继续添加!请选择y或Y继续,否者退出!"<<endl;
chiose=getchar();
fflush(stdin); /*清除缓存*/
}
system("cls");
fclose(fp);
}
/*按书名删除图书函数*/
void del_book_name()
{
Book *head,*p,*q;
char chiose='y';
int tage=0; //标志0表示未找到
char bookname[15]; /*定义要删除的书*/
head=load_2(); /*调用函数,把文件中类容拿来建立链表*/
while(chiose=='y'||chiose=='Y')
{cout<<"input the delete book name!"<<endl;
fflush(stdin);
cin>>bookname; /*要删除的书名*/
q=p=head;
while(p!=NULL)
{if(strcmp(p->book_name,bookname)==0)
{tage=1;
if(p==head)head=head->next;
else q->next=p->next;
free(p);
break;
}
q=p;p=p->next;
}
if(tage==0)printf("要删除的书不存在!\n");
cout<<"删除结束!是否继续:y或Y继续,否者结束!"<<endl;
fflush(stdin);
scanf("%c",&chiose);
}
save_2(head);
system("cls");
}
/*按书名查找图书*/
int find_book()
{
FILE *fp;
Book book1;
char chiose='y';
char bookname[15];
int flag=0; /*1作为找到书的标志,0为没有找到*/
fp=fopen("book","rb");
if(fp==NULL)
{cout<<"error!"<<endl;
return 0;
}
while(chiose=='y'||chiose=='Y')
{cout<<"input the finding book!"<<endl;
cin>>bookname;
while(!feof(fp)) /*判断文件是否结束,真为1假为0.文件未结束就执行下面循环语句*/
{fread(&book1,sizeof(Book),1,fp);
if(strcmp(book1.book_name,bookname)==0)
{flag=1;
cout<<"要查找的书籍信息!"<<endl;
cout<<book1.book_name<< book1.book_author<< book1.tage<<endl;
break;
}
}
if(flag==0)cout<<"NO FIND!"<<endl;
flag=0;
cout<<"查找结束!是否继续:y或Y继续,否则结束!"<<endl;
rewind(fp);
fflush(stdin);
cin>>&chiose;
}
fclose(fp);
return 0;
}
/*输出图书信息*/
void print_book()
{
Book book2;
FILE *fp;
fp=fopen("book","rb");
if(fp==NULL)
{printf("error!\n");return;}
cout<<"书名 作者 状态!"<<endl;
while(fread(&book2,sizeof(Book),1,fp)) /*从文件中读数据到结构体变量book2中,读取成功就执行下面语句*/
cout<<book2.book_name<<book2.book_author<<book2.tage<<endl;
fclose(fp);
}
/*通过书名修改图书信息,是通过指针的修改来完成*/
void ame_book()
{
int chiose; /*用于下面选择操作用*/
Book *head,*p;
char tage='y';
int flag=0;
char bookname[15]; /*定义修改的书名*/
head=load_2(); /*调用函数,将文件的类容放进链表 然后对其修改操作*/
while(tage=='y'||tage=='Y')
{cout<<"input the change book!"<<endl;
fflush(stdin);
cin>>bookname;
p=head;
while(p!=NULL)
if(strcmp(bookname,p->book_name)==0) /*查找匹配的书名*/
{ flag=1;
cout<<" 1: <修改书名>"<<endl;
cout<<" 2: <修改作者>"<<endl;
cout<<" 3: <修改状态>"<<endl;
cout<<"please input 1---3!else illeage!"<<endl;
fflush(stdin);
cin>>chiose;
switch(chiose)
{case 1:{ char book_name[15];
cout<<"input the new name!"<<endl;
fflush(stdin);
cin>>book_name;
strcpy(p->book_name,book_name);
cout<<"\n";
break;
}
case 2:{ char book_author[15];
printf("input the new author!\n");
fflush(stdin);
cin>>book_author;
strcpy(p->book_author,book_author);
cout<<"\n";
break;
}
case 3:{int tage;
cout<<"input new state!"<<endl;
cin>>tage;
p->tage=tage;
cout<<"\n";
break;
}
default: cout<<"无此操作!"<<endl;break;
}
break;
}
else
p=p->next;
if(flag==0)cout<<"不存在该书!"<<endl;
flag=0;
cout<<"修改结束!是否继续:y或Y继续,否则结束!"<<endl;
fflush(stdin);
cin>>&tage;
}
save_2(head);
system("cls");
}
/*检查卡号是否存在函数*/
int cha_car(int card)
{
FILE *fp;
Card card1;
int flag=0;
fp=fopen("card","rb");
if(fp==NULL){printf("error!\n");return flag;}
while(!feof(fp))
{fread(&card1,sizeof(Card),1,fp);
if(card1.car_num==card)
评论0