
一个简单的图书借阅程序 实验:一个简单的图书借阅程序。 要求:图书信息包含以下数据项:a)图书编号b)图书名c出版社d)出版时间e)是否已 被借阅。 自己根据以上信息定义图书的结构体类型book 假定该图书馆有图书5本(为简化调试,输入5本图书信息为例),定义该结构体类型 数组,程序运行时先从键盘上输入图书信息,建立该图书信息库。 由用户从键盘上输入所借阅的“图书编号“或"图书名",程序根据输入信息,查找有无该 图书,如果没有则显示"没有该图书":如果有该书,则查看该书是否己被借阅(最后一个成 员值),如果已借阅则反馈信息为"该书已借出,不能借阅":如果没被借阅,则将该书借出 (借阅标志变为Y)并显示"借阅成功!"。 学生程序1: #include #include struct book int num; char name[50]; char publish[40]; struct int year; int month; }day; char borrow; }books[5]; main() char name[50]; int i,num; int flag=0; fori=0;ik2;it+)】
一个简单的图书借阅程序 实验:一个简单的图书借阅程序。 要求:图书信息包含以下数据项:a)图书编号 b)图书名 c)出版社 d)出版时间 e)是否已 被借阅。 自己根据以上信息定义图书的结构体类型 book 假定该图书馆有图书 5 本(为简化调试,输入 5 本图书信息为例),定义该结构体类型 数组,程序运行时先从键盘上输入图书信息,建立该图书信息库。 由用户从键盘上输入所借阅的"图书编号"或"图书名",程序根据输入信息,查找有无该 图书,如果没有则显示"没有该图书";如果有该书,则查看该书是否已被借阅(最后一个成 员值),如果已借阅则反馈信息为"该书已借出,不能借阅";如果没被借阅,则将该书借出 (借阅标志变为'Y')并显示"借阅成功!"。 学生程序 1: #include #include struct book { int num; char name[50]; char publish[40]; struct { int year; int month; }day; char borrow; }books[5]; main() { char name[50]; int i,num; int flag=0; for(i=0;i<2;i++)

printf("input book's num,name,publish,day:\n"); scanf("%d",&books[i].num); scanf("%s\n",books [i].name); gets(books[i].publish); scanf("%d%d",&books[i].day.year,&books[i].day.month); books[i].borrow='n'; } printf("-- -n" printf("output the book's information:n"); printf("num name publish publishday borrow:\n"); for(i=0;ik2;it+)】 printf("% -10d%-16s%-12s%6d,%d%9c\n",books [i].num,books [i].name,books[i].publish,books[i].day.year,b ooks[i].day.month,books [i].borrow); printf(" -n"方 printf("Now you can input your book name:\n"); scanf("%s",name); for(i=0;i<2;i++) { if(strcmp(books[i].name,name)==0) flag=1; if(books [i].borrow=='n') books[i].borrow='y'; printf("success!")i 3 else printf("The book has been borrowed.You can't borrow it."); } } if(flag=0】 printf("We can't find this book.");
{ printf("input book's num,name,publish,day:\n"); scanf("%d",&books[i].num); scanf("%s\n",books[i].name); gets(books[i].publish); scanf("%d%d",&books[i].day.year,&books[i].day.month); books[i].borrow='n'; } printf("---------------------------------------\n"); printf("output the book's information:\n"); printf(" num name publish publishday borrow:\n"); for(i=0;i<2;i++) printf("% -10d%-16s%-12s%6d,%d%9c\n",books[i].num,books[i].name,books[i].publish,books[i].day.year,b ooks[i].day.month,books[i].borrow); printf("------------------------------------------\n"); printf("Now you can input your book name:\n"); scanf("%s",name); for(i=0;i<2;i++) { if(strcmp(books[i].name,name)==0) { flag=1; if(books[i].borrow=='n') { books[i].borrow='y'; printf("success!"); } else printf("The book has been borrowed.You can't borrow it."); } } if(flag==0) printf("We can't find this book.");

} 学生程序2: #include #include struct book int num; char name[50]; char publish[40]; struct int year; int month; }day; char borrow; books[5]={101,"math","xinhua'",2000,2,'n', (102,"english","gaojiao",2002,5,y), {103,"novel","xinhua",2000,11,'n}, (104,"science","xidian",1998,6,'y), (105,"computer","youdian",1990,9,'n')); main() char name[50]; int i,num; int flag=0; printf(". n"方: printf("output the book's information:\n"); printf("num name publish publishday borrow:\n"); for(i=0;i<5;i++) printf("% -10d%-16s%-12s%6d,%-9d%c\n",books [i].num,books[i].name,books [i].publish,books[i].day.year,b ooks[i].day.month,books [i].borrow); printf(" -n
} 学生程序 2: #include #include struct book { int num; char name[50]; char publish[40]; struct { int year; int month; }day; char borrow; }books[5]={{101,"math","xinhua",2000,2,'n'}, {102,"english","gaojiao",2002,5,'y'}, {103,"novel","xinhua",2000,11,'n'}, {104,"science","xidian",1998,6,'y'}, {105,"computer","youdian",1990,9,'n'}}; main() { char name[50]; int i,num; int flag=0; printf("---------------------------------------\n"); printf("output the book's information:\n"); printf(" num name publish publishday borrow:\n"); for(i=0;i<5;i++) printf("% -10d%-16s%-12s%6d,%-9d%c\n",books[i].num,books[i].name,books[i].publish,books[i].day.year,b ooks[i].day.month,books[i].borrow); printf("------------------------------------------\n");

printf("Now you can input your book name:\n"); scanf("%s",name); for(i=0;i<5;i++) { if(strcmp(books [i].name,name)==0) {flag=1; if(books [i].borrow=='n') books[i].borrow='y'; printf("success!"); } else printf("The book has been borrowed.You can't borrow it."); } } if(flag==0) printf("We can't find this book."); } 实验:一个简单的图书借阅程序(续)。 要求: 一、将上例中的“输入图书信息”和“图书借阅”用函数实现,函数名分别为input和 borrow。input函数的参数有一个结构体类型的指针(或结构体类型的数组):borrow函数 的参数为2个,一个为结构体指针,一个为图书书名。 二、再添加一个“还书”函数back,其参数为两个,第一个是结构体数组(或指针), 第二个参数是图书的书名。提示:将该书的“借阅”成员置N'。 学生程序: #include #include #define N 2 /N is the number of the books * struct book int num;
printf("Now you can input your book name:\n"); scanf("%s",name); for(i=0;i<5;i++) { if(strcmp(books[i].name,name)==0) { flag=1; if(books[i].borrow=='n') { books[i].borrow='y'; printf("success!"); } else printf("The book has been borrowed.You can't borrow it."); } } if(flag==0) printf("We can't find this book."); } 实验:一个简单的图书借阅程序(续)。 要求: 一、将上例中的“输入图书信息”和“图书借阅”用函数实现,函数名分别为 input 和 borrow。input 函数的参数有一个结构体类型的指针(或结构体类型的数组);borrow 函数 的参数为 2 个,一个为结构体指针,一个为图书书名。 二、再添加一个“还书”函数 back,其参数为两个,第一个是结构体数组(或指针), 第二个参数是图书的书名。提示:将该书的“借阅”成员置'N'。 学生程序: #include #include #define N 2 /* N is the number of the books */ struct book { int num;

char name[50]; char publish[40]; struct int year; int month; day; char borrow; 方 void input(struct book *b);/*Same as:void input(struct book b[]);*/ void output(struct book *b); void borrow(struct book *b,char name[]); void back(struct book *b,char *name); main() {struct book books[N]; char name[50]; int i,num; clrscr(); input(books); output(books); printf("Now you can input the book name you are going to borrow:\n"); scanf("%s",name); borrow(books,name); output(books); printf("Now you can input the book name you are going to return:\n"); scanf("%s",name); back(books,name); output(books); getch(); } void input(struct book *b)/*void input(struct book b[]);*/
char name[50]; char publish[40]; struct { int year; int month; }day; char borrow; }; void input(struct book *b); /* Same as: void input(struct book b[]);*/ void output(struct book *b); void borrow(struct book *b, char name[]); void back(struct book *b, char *name); main() { struct book books[N]; char name[50]; int i,num; clrscr(); input(books); output(books); printf("Now you can input the book name you are going to borrow:\n"); scanf("%s",name); borrow(books,name); output(books); printf("Now you can input the book name you are going to return:\n"); scanf("%s",name); back(books,name); output(books); getch(); } void input(struct book *b) /* void input(struct book b[]);*/

{int i; printf("Input book's num,name,publish,day,borrow:\n"); printf("Be caution:you should input the to seperate every item!\n"); for(i=0;i{printf("number %d:",i+1); scanf("%d",&b->num); scanf("%s %s",b->name,b->publish); scanf("%d%d %c",&b->day.year,&b->day.month,&b->borrow); } } void output(struct book *b) {int i; printf("\n Output the book's information:\n"); printf("-----n"方i printf("num name publish publishday borrow:\n\n"); for(i=0;i printf("% -10d%-16s%-12s%6d,%d%9c\n",b->num,b->name,b->publish,b->day.year,b->day.month,b->borro w); printf("-- --In\n") } void borrow(struct book *b,char name[]) int i,flag=0; for(i=0;i{ if(strcmp(b->name,name)==0) flag=1; if(b->borrow=='n') b->borrow='y'; printf("success!\n"); } else printf("The book has been borrowed.You can't borrow it.\n"); }
{ int i; printf("Input book's num,name,publish,day,borrow:\n"); printf("Be caution: you should input the to seperate every item!\n"); for(i=0;i { printf("number %d:",i+1); scanf("%d",&b->num); scanf("%s %s",b->name,b->publish); scanf("%d%d %c",&b->day.year,&b->day.month,&b->borrow); } } void output(struct book *b) { int i; printf("\n Output the book's information:\n"); printf("---------------------------------\n"); printf(" num name publish publishday borrow:\n\n"); for(i=0;i printf("% -10d%-16s%-12s%6d,%d%9c\n",b->num,b->name,b->publish,b->day.year,b->day.month,b->borro w); printf("----------------------------------------------------\n\n"); } void borrow(struct book *b, char name[]) { int i,flag=0; for(i=0;i { if(strcmp(b->name,name)==0) { flag=1; if(b->borrow=='n') { b->borrow='y'; printf("success!\n"); } else printf("The book has been borrowed.You can't borrow it.\n"); }

} if(flag==0) printf("We can't find this book.\n"); } void back(struct book *b,char *name) int i,flag=0; for(i=0;i{ if(strcmp(b->name,name)==0) flag=1; b->borrow='n'; printf("The book has been returned.\n"); } } if(flag==0) printf("Can't find this book!The book doesn't belong to our library.\n"); } 实验:一个简单的图书借阅程序(续)。 要求: 一、将上章结构体实验中(图书借阅程序)的图书信息写入到文件book.dat中保存起 来,以二进制方式写入。 二、将上章中的图书借阅程序完善,首先打开图书信息文件book.dat,然后将图书信 息读入数组books中,再调用借阅函数borrow和还书函数back,将修改后的图书信息写回 到文件中保存起来。 学生程序: 建立图书磁盘文件程序 #include #include #include main() {struct book books[N];
} if(flag==0) printf("We can't find this book.\n"); } void back(struct book *b, char *name) { int i,flag=0; for(i=0;i { if(strcmp(b->name,name)==0) { flag=1; b->borrow='n'; printf("The book has been returned.\n"); } } if(flag==0) printf("Can't find this book! The book doesn't belong to our library.\n"); } 实验:一个简单的图书借阅程序(续)。 要求: 一、 将上章结构体实验中(图书借阅程序)的图书信息写入到文件 book.dat 中保存起 来,以二进制方式写入。 二、 将上章中的图书借阅程序完善,首先打开图书信息文件 book.dat,然后将图书信 息读入数组 books 中,再调用借阅函数 borrow 和还书函数 back,将修改后的图书信息写回 到文件中保存起来。 学生程序: 建立图书磁盘文件程序 #include #include #include main() { struct book books[N];

char name[50] int i,num; FILE *fp; clrscr(); fp=fopen("books.dat","wb+"); printf("Input book's num,name,publish,day,borrow:\n"); printf("Be caution:you should input the to seperate every item!\n"); for(i=O;inum,b->name,b->publish,b->day.year,b->day.month,b->borro w方 printf(" --In\n"); } 图书借阅程序(对已有的图书文件进行操作)
char name[50]; int i,num; FILE *fp; clrscr(); fp=fopen("books.dat","wb+"); printf("Input book's num,name,publish,day,borrow:\n"); printf("Be caution: you should input the to seperate every item!\n"); for(i=0;inum,b->name,b->publish,b->day.year,b->day.month,b->borro w); printf("----------------------------------------------------\n\n"); } 图书借阅程序(对已有的图书文件进行操作)

#include #include #include main() struct book books[N]; char name[50] int i,num; FILE *fp; fp=fopen("books.dat","rb+"); clrscr(); fread(books,sizeof(struct book),N,fp); output(books); printf("Now you can input the book name you are going to borrow:\n"); scanf("%s",name); borrow(books,name); rewind(fp); fwrite(books,sizeof(struct book),N,fp); output(books); printf("Now you can input the book name you are going to return:\n"); scanf("%s",name); back(books,name); rewind(fp); fwrite(books,sizeof(struct book),N,fp); output(books); fclose(fp); getch(); } void input(struct book *b)/*void input(struct book b[]);* int i; printf("Input book's num,name,publish,day,borrow:\n");
#include #include #include main() { struct book books[N]; char name[50]; int i,num; FILE *fp; fp=fopen("books.dat","rb+"); clrscr(); fread(books,sizeof(struct book),N,fp); output(books); printf("Now you can input the book name you are going to borrow:\n"); scanf("%s",name); borrow(books,name); rewind(fp); fwrite(books,sizeof(struct book),N,fp); output(books); printf("Now you can input the book name you are going to return:\n"); scanf("%s",name); back(books,name); rewind(fp); fwrite(books,sizeof(struct book),N,fp); output(books); fclose(fp); getch(); } void input(struct book *b) /* void input(struct book b[]);*/ { int i; printf("Input book's num,name,publish,day,borrow:\n");

printf("Be caution:you should input the to seperate every item!\n"); for(i=0;inum); scanf("%s %s",b->name,b->publish); scanf("%d%d %c",&b->day.year,&b->day.month,&b->borrow); } void output(struct book *b) int i; printf("\n Output the book's information:\n"); printf("- -n"方 printf("num name publish publishday borrow:\n\n"); for(i=0;inum,b->name,b->publish,b->day.year,b->day.month,b->borro w). printf("- --InIn"); } void borrow(struct book *b,char name[]) int i,flag=0; for(i=0;iname,name)==0) flag=1; if(b->borrow=='n') b->borrow='y'; printf("success!\n"); } else printf("The book has been borrowed.You can't borrow it.\n");
printf("Be caution: you should input the to seperate every item!\n"); for(i=0;inum); scanf("%s %s",b->name,b->publish); scanf("%d%d %c",&b->day.year,&b->day.month,&b->borrow); } } void output(struct book *b) { int i; printf("\n Output the book's information:\n"); printf("---------------------------------\n"); printf(" num name publish publishday borrow:\n\n"); for(i=0;inum,b->name,b->publish,b->day.year,b->day.month,b->borro w); printf("----------------------------------------------------\n\n"); } void borrow(struct book *b, char name[]) { int i,flag=0; for(i=0;iname,name)==0) { flag=1; if(b->borrow=='n') { b->borrow='y'; printf("success!\n"); } else printf("The book has been borrowed.You can't borrow it.\n");