c语言结课作业
用书:c程序设计 闫锴 张连浩
关于代码
书上程序结构混乱 也有很多bug
我也没怎么改 所以输入样例都是严格按照输入输出格式来的 就别吐槽了
另外数据通过二进制文件进行存放更新
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef struct {
char name[31];
char author[27];
char publish[31];
int store;
int total;
int usr[10];
int days[10];
int id;
}book_info;
book_info book[100],*p;
void page_title(char *menu_item);
void return_confirm();
void read_book_info(book_info book[]);
void save_book_info(book_info book[]);
void book_add();
int book_show();
int book_search();
int book_delete();
void book_borrow();
void book_return();
int book_edit();
int main(){
read_book_info(book);
menu:page_title("操作选单");
int cmd=0;
printf("用数字键选择操作\n\n");
printf("\t\t1注册新书\t\t2显示图书\n");
printf("\t\t3查询图书信息\t\t4修改图书信息\n");
printf("\t\t5删除图书\t\t6借阅图书\n");
printf("\t\t7归还图书\t\t8退出系统\n");
printf("请按数字键:\n");
scanf("%d",&cmd);
switch(cmd){
case 1:book_add();break;
case 2:book_show();break;
case 3:book_search();break;
case 4:book_edit();;break;
case 5:book_delete();break;
case 6:book_borrow();break;
case 7:book_return();break;
case 8:save_book_info(book);return 0;
}
save_book_info(book);
goto menu;
}
void read_book_info(book_info book[]){
FILE *fp=fopen("my_project","rb+");
if(!fp)
printf("open failed!/n");
int i=0;
for(i=0;i<100;i++){
if(fread(&book[i],sizeof(book_info),1,fp)!=1){
printf("Ovo");
fclose(fp);
break;
}
}
fclose(fp);
}
void
save_book_info(book_info book[]){
int i=0;
FILE *fp0=fopen("my_project","wb+");
if(fp0==NULL)
printf("open failed!");
for(i=0;i<100;i++){
if(fwrite(&book[i],sizeof(book_info),1,fp0)!=1){
printf("write failed!\n");
}
}
fclose(fp0);
}
void
page_title(char *menu_item){
printf("\t\t**********欢迎使用草滩帝国大学图书馆管理系统**********\n\n——%s——\n\n",menu_item);
}
void
return_confirm(){
printf("\n按任意键返回******\n");
getchar();
}
void
book_add(){
int i=0;
system("cls");
page_title("注册新书");
for(;i<100;i++){
if(!book[i].id) break;
}
printf("序号:");
scanf("%d",&(book[i].id));
printf("书名:");
scanf("%s",book[i].name);
printf("作者:");
scanf("%s",book[i].author);
printf("出版社:");
scanf("%s",book[i].publish);
printf("数量:");
scanf("%d",&(book[i].total));
getchar();
book[i].store=book[i].total;
return_confirm();
}
int
book_show(){
int i,flag=0;
system("cls");
for(i=0;i<100;i++){
if(book[i].id!=0){
printf("序号:%d\t",book[i].id);
printf("书名:%s\t",book[i].name);
printf("作者:%s\t",book[i].author);
printf("出版社:%s\t",book[i].publish);
printf("存数:%d\t",book[i].store);
printf("总数:%d\t\n",book[i].total);
flag=1;
}
}
if(!flag)
printf("\n没有找到相关记录\n");
return i;
}
int
book_search(){
int n,i,flag=0;
n=-1;
char nam[30];
system("cls");
page_title("图书查找");
printf("输入书名:\n");
scanf("%s",nam);
for(i=0;i<100;i++){
if(!strcmp(nam,book[i].name)){
printf("序号:%d\t",book[i].id);
printf("书名:%s\t\n",book[i].name);
printf("作者:%s\t",book[i].author);
printf("出版社:%s\t\n",book[i].publish);
printf("存数:%d\t",book[i].store);
printf("总数:%d\t\n",book[i].total);
flag=1;
n=i;
break;
}
}
fflush(stdin);
if(!flag)
printf("\n未找到相关记录\n");
return n;
}
int
book_edit(){
int i,id_change,n=-1;
system("cls");
page_title("修改图书信息");
printf("输入要修改的图书编号:");
scanf("%d",&id_change);
for(i=0;i<100;i++){
if(book[i].id==id_change){
int book_total=book[i].total;
getchar(); /*清除缓冲区*/
printf("修改书名:");
gets(book[i].name);
printf("修改作者:");
gets(book[i].author);
printf("修改出版社:");
gets(book[i].publish);
printf("修改总数:");
scanf("%d",&book[i].total);
book[i].store+=(book[i].total-book_total);
n=i;
break;
}
}
if(i==100)
printf("未找到相关记录");
return n;
}
int
book_delete(){
int i,j,num,ret;
ret=-1;
system("cls");
page_title("删除图书");
printf("请输入需要删除书的编号:");
scanf("%d",&num);
for(i=0;i<100;i++){
if(book[i].id==num){
for(j=i;j<100;j++){
book[j].id=book[j+1].id;
strcpy(book[j].name,book[j+1].name);
strcpy(book[j].publish,book[j+1].publish);
}
printf("该书已经删除!");
ret=i;
}
}
if(ret==-1)
printf("没有找到相关记录");
fflush(stdin);
return ret;
}
void
book_borrow(){
int n,s,i,d;
system("cls");
n=book_search();
page_title("借阅图书");
if(n!=-1&&book[n].store>0){
printf("输入借书证编号:");
scanf("%d",&s);
printf("输入可借阅天数:");
scanf("%d",&d);
for(i=0;i<10;i++)
if(!book[n].usr[i]){
book[n].usr[i]=s;
book[n].days[i]=d;
book[n].store--;
break;
}
}
else if(n!=-1&&book[n].store==0)
printf("此书已经全部借出。\n");
else
;
return_confirm();
fflush(stdin);
}
void
book_return(){
int n,s,i;
n=book_search();
page_title("归还图书");
if(n!=-1&&book[n].store<book[n].total){
printf("借阅者图书证列表:\n");
for(i=0;i<10;i++) if(book[n].usr[i]!=0)
printf("[%d]-%d天\n",book[n].usr[i],book[n].days[i]);
printf("输入借书证序号:");
scanf("%d",&s);
for(i=0;i<10;i++){
if(book[n].usr[i]==s){
book[n].usr[i]=0;
book[n].days[i]=0;
book[n].store++;
break;
}
}
}
if(n!=-1&&book[n].store==book[n].total)
printf("全部归还\n");
else if(n!=-1&&book[n].store<book[n].total)
printf("归还成功\n");
else
;
return_confirm();
}