user-0 提问: 哪个大佬有空解释一下吗？急

回复:
user: user-0
content: 引用 2 楼 金。金。金。的回复:#include<stdio.h> #include<stdlib.h> #include<string.h>  #define max 100 #define len sizeof(Book) typedef struct Book{ 	char title[10]; 	char outher[10]; 	int time; 	int price; }Book; Book book[max]; int n; void use()////从information.dat中调出信息到book[max] { 	FILE *fp; 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fread(&book[i],len,1,fp)!=0); 	} 	fclose(fp); } void input()//录入图书信息到book[max] { 	printf("请输入要录入的图书个数n=");     scanf("%d",&n); 	printf("请输入图书信息：\n书名 作者名 出版时间 价格\n"); 	for(int i=0;i<n;i++){ 		scanf("%s%s%d%d",&book[i].title,&book[i].outher,&book[i].time,&book[i].price); 	} }  void save() //把book[max]的信息存入information.dat文件里 { 	FILE *fp; 	if((fp=fopen("information.dat","wb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fwrite(&book[i],len,1,fp)!=1) 			printf("file write error"); 	} 	fclose(fp); 	printf("已存入图书信息!\n"); } void output()//从information.dat中调出信息到book[max]并输出 { 	FILE *fp; 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fread(&book[i],len,1,fp)!=0) 			printf("%-10s %-10s %d %d\n",book[i].title,book[i].outher,book[i].time,book[i].price); 	} 	fclose(fp); } void search()//从information.dat中调出信息到book[max]并查找 { 	Book b[max]; 	FILE *fp; 	char bookname[10]; 	printf("请输入要查找的书名：\n"); 	scanf("%s",&bookname); 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if((fread(&b[i],len,1,fp)!=0)&&(strcmp(b[i].title,bookname)==0)) 			printf("%-10s %-10s %d %d\n",b[i].title,b[i].outher,b[i].time,b[i].price); 	} 	fclose(fp); } void sort()//从information.dat中调出信息到在book[max]中排序后再输出 { 	use(); 	printf("按照时间排序如下：\n"); 	Book t; 	for(int i=0;i<n;i++) 		for(int j=i+1;j<n;j++) 			if(book[i].time<book[j].time){ 				t=book[i]; 				book[i]=book[j]; 				book[j]=t; 			} 	for(int k=0;k<n;k++){ 		printf("%-10s %-10s %d %d\n",book[k].title,book[k].outher,book[k].time,book[k].price); 	}  }  void dele()// { 	Bo 排序那里没问题了，之前的错误是	for(int k=0;k<n;k++){ 		printf("%-10s %-10s %d %d\n",book[i].title,book[i].outher,book[i].time,book[i].price); for括号里面是k，但是printf里面是i，已经改了

user: user-0
content: 引用 2 楼 金。金。金。的回复:#include<stdio.h> #include<stdlib.h> #include<string.h>  #define max 100 #define len sizeof(Book) typedef struct Book{ 	char title[10]; 	char outher[10]; 	int time; 	int price; }Book; Book book[max]; int n; void use()////从information.dat中调出信息到book[max] { 	FILE *fp; 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fread(&book[i],len,1,fp)!=0); 	} 	fclose(fp); } void input()//录入图书信息到book[max] { 	printf("请输入要录入的图书个数n=");     scanf("%d",&n); 	printf("请输入图书信息：\n书名 作者名 出版时间 价格\n"); 	for(int i=0;i<n;i++){ 		scanf("%s%s%d%d",&book[i].title,&book[i].outher,&book[i].time,&book[i].price); 	} }  void save() //把book[max]的信息存入information.dat文件里 { 	FILE *fp; 	if((fp=fopen("information.dat","wb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fwrite(&book[i],len,1,fp)!=1) 			printf("file write error"); 	} 	fclose(fp); 	printf("已存入图书信息!\n"); } void output()//从information.dat中调出信息到book[max]并输出 { 	FILE *fp; 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fread(&book[i],len,1,fp)!=0) 			printf("%-10s %-10s %d %d\n",book[i].title,book[i].outher,book[i].time,book[i].price); 	} 	fclose(fp); } void search()//从information.dat中调出信息到book[max]并查找 { 	Book b[max]; 	FILE *fp; 	char bookname[10]; 	printf("请输入要查找的书名：\n"); 	scanf("%s",&bookname); 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if((fread(&b[i],len,1,fp)!=0)&&(strcmp(b[i].title,bookname)==0)) 			printf("%-10s %-10s %d %d\n",b[i].title,b[i].outher,b[i].time,b[i].price); 	} 	fclose(fp); } void sort()//从information.dat中调出信息到在book[max]中排序后再输出 { 	use(); 	printf("按照时间排序如下：\n"); 	Book t; 	for(int i=0;i<n;i++) 		for(int j=i+1;j<n;j++) 			if(book[i].time<book[j].time){ 				t=book[i]; 				book[i]=book[j]; 				book[j]=t; 			} 	for(int k=0;k<n;k++){ 		printf("%-10s %-10s %d %d\n",book[k].title,book[k].outher,book[k].time,book[k].price); 	}  }  void dele()// { 	Bo void dele()// { 	Book b[max]; 	FILE *fp; 	char bookname[10]; 	printf("请输入要删除的书名：\n");                       	scanf("%s",&bookname); 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	int j=0; 	for(int i=0;i<n;i++) 		if(fread(&book[i],len,1,fp)!=0) 			printf("%-10s %-10s %d %d\n",book[i].title,book[i].outher,book[i].time,book[i].price); 		 	for(i=0;i<n;i++){ 		if(strcmp(book[i].title,bookname)==0) 		{ 			printf("已找到待删除书名i=%d\n",i); 			j=i; 			n=n-1; 		} 	}//找到待删除的记录  	for(i=j;i<n;i++) 		book[i]=book[i+1]; //	for(int t=0;t<n;t++) //		printf("%-10s %-10s %d %d\n",book[t].title,book[t].outher,book[t].time,book[t].price);//输出修改后book  	//（上文）删除源文件内容        	               //（下文）删除后保存到ne文件中     FILE *fpnew; 	if((fpnew=fopen("ne.dat","wb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int k=0;k<n;k++){ 		if(fwrite(&book[i],len,1,fpnew)!=1) 			printf("file write error"); 	} 	fclose(fpnew);  	if(remove("information.dat")==0) 		printf("have removed");//删除原文件 	if(rename("ne.dat","information.dat")==0) 		printf("filename change");//把临时文件名改为原文件名 	output(); } void main() { 	int login=180070120; 	int l; 	char function; 	printf("请输入登录号：(180070120)"); 	for(int i=1;i<=3;i++){ 		scanf("%d",&l); 		if(l!=login){ 			printf("密码错误！"); 		} 		else 			break; 	} 	if(i==4) 		return; 	printf("\n"); 	printf("......录入.图书信息 请输入A\n"); 	printf("......保存.图书信息 请输入B\n"); 	printf("......浏览.图书信息 请输入C\n"); 	printf("......按书名查询    请输入D\n"); 	printf("......按作者名查询  请输入E\n"); 	printf("......排序.图书信息 请输入F\n"); 	printf("......删除.图书信息 请输入G\n"); 	printf("......修改.图书信息 请输入H\n"); 	printf("......结束 请输入#\n"); 	getchar();     while(function!='#'){ 		printf("请输入指令："); 		scanf("%c",&function); 		switch(function){ 		case 'A':  input();break; 		case 'B':  save();break; 		case 'C':  output();break; 		case 'D':  search();break; 		case 'E':  sort();break; 		case 'F':  dele();break; 		case 'G':  ;break; 		case 'H':  ;break; 		} 	} }

user: user-0
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  #define max 100 #define len sizeof(Book) typedef struct Book{ 	char title[10]; 	char outher[10]; 	int time; 	int price; }Book; Book book[max]; int n; void use()////从information.dat中调出信息到book[max] { 	FILE *fp; 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fread(&book[i],len,1,fp)!=0); 	} 	fclose(fp); } void input()//录入图书信息到book[max] { 	printf("请输入要录入的图书个数n=");     scanf("%d",&n); 	printf("请输入图书信息：\n书名 作者名 出版时间 价格\n"); 	for(int i=0;i<n;i++){ 		scanf("%s%s%d%d",&book[i].title,&book[i].outher,&book[i].time,&book[i].price); 	} }  void save() //把book[max]的信息存入information.dat文件里 { 	FILE *fp; 	if((fp=fopen("information.dat","wb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fwrite(&book[i],len,1,fp)!=1) 			printf("file write error"); 	} 	fclose(fp); 	printf("已存入图书信息!\n"); } void output()//从information.dat中调出信息到book[max]并输出 { 	FILE *fp; 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if(fread(&book[i],len,1,fp)!=0) 			printf("%-10s %-10s %d %d\n",book[i].title,book[i].outher,book[i].time,book[i].price); 	} 	fclose(fp); } void search()//从information.dat中调出信息到book[max]并查找 { 	Book b[max]; 	FILE *fp; 	char bookname[10]; 	printf("请输入要查找的书名：\n"); 	scanf("%s",&bookname); 	if((fp=fopen("information.dat","rb"))==NULL){ 		printf("open file error"); 		return; 	} 	for(int i=0;i<n;i++){ 		if((fread(&b[i],len,1,fp)!=0)&&(strcmp(b[i].title,bookname)==0)) 			printf("%-10s %-10s %d %d\n",b[i].title,b[i].outher,b[i].time,b[i].price); 	} 	fclose(fp); } void sort()//从information.dat中调出信息到在book[max]中排序后再输出 { 	use(); 	printf("按照时间排序如下：\n"); 	Book t; 	for(int i=0;i<n;i++) 		for(int j=i+1;j<n;j++) 			if(book[i].time<book[j].time){ 				t=book[i]; 				book[i]=book[j]; 				book[j]=t; 			} 	for(int k=0;k<n;k++){ 		printf("%-10s %-10s %d %d\n",book[k].title,book[k].outher,book[k].time,book[k].price); 	}  }  void dele()// { 	Bo

user: user-1
content: 你这什么东西啊，太绕了，你要把错误和问题写详细啊，好像排序哪里用一个循环就够了，两层虽然墨迹了，但是没问题的话也不会报错，删除那里应该是相等删除吧！=0是不相等的时候了啊，还有下面那1到24又是啥

user: user-1
content: for(i=0;i<n;i++){ if(strcmp(book[i].title,bookname)==0) { printf("已找到待删除书名i=%d\n",i); j=i; n=n-1; } }//找到待删除的记录  for(i=j;i<n;i++) book[i]=book[i+1]; // for(int t=0;t<n;t++) // printf("%-10s %-10s %d %d\n",book[t].title,book[t].outher,book[t].time,book[t].price);//输出   改： for(i=0;i<n;i++){ if(strcmp(book[i].title,bookname)==0) { printf("已找到待删除书名i=%d\n",i); j=i; } }//找到待删除的记录  for(i=j;i<n;i++){ book[i]=book[n-1]; n=n-1; } // for(int t=0;t<n;t++) // printf("%-10s %-10s %d %d\n",book[t].title,book[t].outher,book[t].time,book[t].price);//输出 你试试行不行，直接把最后的位置的内容替换掉你要删的，然后再把数量减一，直接不显示最后的 我只看了这一段附近的，n要是全局变量，你要确保变量值这里都是对的 如果可以用，建议你简化一下代码，很多一个循环能搞定的就不要用太多循环了 for(i=0;i<n;i++){ if(strcmp(book[i].title,bookname)==0) { printf("已找到待删除书名i=%d\n",i); book[i]=book[n-1];   //最后一本移到这个位置，删除当前书 n=n-1;    //位置前移一个 } }//找到待删除的记录