急:求教,图书管理系统怎么删除图书信息啊?
#include "stdio.h"#include "stdlib.h"
#include "string.h"
/*学生信息*/
typedef struct{
char name[20];//姓名
char num[12];//学号
char chass[10];//班级
char sex[5];//性别
}student;
typedef struct{
int stucount;
student stu[100];
}mstu;
mstu studata;
/*图书信息*/
typedef struct{
int booknumber; //图书编号
char bookname[20]; //图书名称
char bookwriter[20]; //图书作者
int status; //图书的状态,有多少本
float price; //图书的价格
}book;
typedef struct{
int count;
book book[100];
}mlibrary;
mlibrary library;//存储图书信息
void save_book()//保存图书信息。
{
FILE *fp;
if((fp=fopen("library.doc","w"))==NULL)
{
printf("\n不能保存图书信息\n");
return;
}
fwrite(&library.count,sizeof(int),1,fp);
fwrite(library.book,sizeof(library.book[0]),library.count,fp);
}
void save_stu(){
FILE *fp;
if((fp=fopen("student.doc","w"))==NULL){
printf("\n不能保存用户注册信息\n");
return;
}
fwrite(&studata.stucount,sizeof(int),1,fp);
fwrite(studata.stu,sizeof(studata.stu[0]),studata.stucount,fp);
}
void function2();
int num_seek(int a){
int i,flag;
flag=0;
for(i=0;i<=library.count;i++)
if(a==library.book[i].booknumber){
printf("图书编号\t书名\t\t作者\t数量\t价格\n");
printf("%d\t\t%s\t%s\t%d\t%f\n",
library.book[i].booknumber,library.book[i].bookname,library.book[i].bookwriter,library.book[i].status,library.book[i].price);
flag=1;}
if(flag==0)
printf("没有找到相符合的书");
return flag;
}
int name_seek(char name[20]){
int flag,i;
flag=0;
for(i=0;i<=library.count;i++)
if(!strcmp(name,library.book[i].bookname)){
printf("图书编号\t书名\t\t作者\t数量\t价格\n");
printf("%d\t\t%s\t%s\t%d\t%f\n",
library.book[i].booknumber,library.book[i].bookname,library.book[i].bookwriter,library.book[i].status,library.book[i].price);
flag=1;}
if(flag==0)
printf("没有找到相符合的书");
return flag;
}
int author_seek(char writer[]){
int flag,i;
flag=0;
for(i=0;i<=library.count;i++)
if(!strcmp(writer,library.book[i].bookwriter)){
printf("图书编号\t书名\t\t作者\t数量\t价格\n");
printf("%d\t\t%s\t%s\t%d\t%f\n",
library.book[i].booknumber,library.book[i].bookname,library.book[i].bookwriter,library.book[i].status,library.book[i].price);
flag=1;}
if(flag==0)
printf("没有找到相符合的书");
return flag;
}
/*新进图书信息输入*/
void function1(){
int y=1;
system("cls");
while(y)
{
printf("\t请输入您的书籍的书号:");
scanf("%d",&library.book[library.count].booknumber);
printf("\t请输入图书名称:");
scanf("%s",library.book[library.count].bookname);
printf("\t请输入作者名:");
scanf("%s",library.book[library.count].bookwriter);
printf("\t请输入图书的数量:");
scanf("%d",&library.book[library.count].status);
printf("\t请输入该书的价格:");
fflush(stdin);
scanf("%f",&library.book[library.count].price);
library.count++;
printf("\n是否继续录入(1.继续;0,退出录入):");
scanf("%d",&y);
if (y!=1)
{
y=0;
break;
}
}
save_book();
}
/*图书信息查询*/
void function2(){
int a,i;
system("cls");
printf("请选择查找方式:\n1.图书编号 \n2.图书名称 \n3.图书作者\t");
scanf("%d",&a);
switch(a){
case 1:{
system("cls");
int a;
printf("\t\t**********按编号查找**********\n");
printf("请输入图书编号:");
scanf("%d",&a);
num_seek(a);
break;}
case 2:{
system("cls");
char search[20];
printf("\t\t**********按图书名称查找**********\n");
printf("输入图书名称:\t");
scanf("%s",search);
name_seek(search);
break;}
case 3:{
system("cls");
char writer[20];
printf("\t\t**********按图书作者查找**********\n");
printf("输入图书作者:\t");
scanf("%s",writer);
author_seek(writer);
break;}
case 0:exit(0);
}
printf("\n是否继续查找?1:继续 0:退出");
scanf("%d",&i);
if(i==1)
function2();
else
return;
}
/*图书信息的删除*/
void function3(){
int number;
system("cls");
printf("\n 输入你想要删除书的编号:");
fflush(stdin);
scanf("%d",&number);
int i;
for (i=0;i<library.count;i++)
{
if(number==library.book[i].booknumber) break;
}
if (i!=library.count)
{
for(;i<library.count-1;i++)
{
strcpy(library.book[i].bookname,library.book[i+1].bookname);
strcpy(library.book[i].bookwriter,library.book[i+1].bookwriter);
library.book[i].booknumber=library.book[i+1].booknumber;
library.book[i].status=library.book[i+1].status;
library.book[i].price=library.book[i+1].price;
}
library.count--;
printf("删除成功,重启程序后生效");
}
else
printf("\n cannot found!\n");
save_book();
fflush(stdin);
getchar();
}
void function4(){
system("cls");
printf("编号\t书名\t\t作者\t状态\t价格\n");
for(int i=0;i<library.count;i++)
printf("%d\t%s\t%s\t%d\t%f\n",library.book[i].booknumber,library.book[i].bookname,library.book[i].bookwriter,library.book[i].status,library.book[i].price);
printf("\n");printf("\n");
printf("\t\t\t按任意键退出");
getchar();
getchar();
return;
}
void main(){
FILE *fp;
if((fp=fopen("library.doc","r"))==NULL){
printf("\n图书信息库不存在\n");
fp=fopen("library.doc","w+");
printf("\n初始化图书信息库,请重启程序\n");
library.count=0;
fwrite(&library.count,sizeof(int),1,fp);
fclose(fp);
return;
}
fread(&library.count,sizeof(int),1,fp);
fread(library.book,sizeof(library.book[0]),library.count,fp);
fclose(fp);
char choice;
do{
system("cls");
printf("\t\t&&&&&&&&图书信息管理系统&&&&&&&&\n");
printf("\n");
printf("\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("\n");
printf("\t\t@^6^ 1:输入图书信息 ^6^@\n");
printf("\t\t@ @\n");
printf("\t\t@^6^ 2:查询图书信息 ^6^@\n");
printf("\t\t@ @\n");
printf("\t\t@^6^ 3:删除图书信息 ^6^@\n");
printf("\t\t@ @\n");
printf("\t\t@^6^ 4:全部图书浏览 ^6^@\n");
printf("\t\t@ @\n");
printf("\t\t@^6^ 0:退出服务 ^6^@\n");
printf("\n");
printf("\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("\n");
printf("\t\t请在上述功能中选择您需要的服务:");
choice=getchar();
switch(choice){
case '1':function1();break;//图书信息输入
case '2':function2();break;//图书信息查询
case '3':function3();break;//图书信息删除
case '4':function4();break;//全部图书浏览
case '0':exit(0);
}
}while(1);
save_book();
save_stu();
return;
}
我删了还是能查到那本书