为什么代码没错,却不行啊!?
#include<stdio.h> #include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct
{
char roomnum[4];
char num[10];
char name[20];
}Student;
Student room1[1000];
int menu()
{
char c;
do
{system("cls");
printf("\t\t★★★★★★学生宿舍管理系统★★★★★★\n");
printf("\t\t§ 1. 输入记录 §\n");
printf("\t\t§ 2. 显示记录 §\n");
printf("\t\t§ 3. 按姓名排序并显示 §\n");
printf("\t\t§ 4. 按房间号排序并显示 §\n");
printf("\t\t§ 5. 按学号排序并显示 §\n");
printf("\t\t§ 6. 按姓名查找并显示 §\n");
printf("\t\t§ 7. 按房间号查找并显示 §\n");
printf("\t\t§ 8. 按学号查找并显示 §\n");
printf("\t\t§ 9. 插入一条记录按学号排序并显示 §\n");
printf("\t\t§10. 从文件中读入数据 §\n");
printf("\t\t§11. 循环写入数据 §\n");
printf("\t\t§ 0. 结束程序 §\n");
printf("\t\t★★★★★★★★★★★★★★★★★★★★\n");
printf("\t\t请选择您要运行的选项按(0-11):");
c=getchar();
}
while(c<'0'||c>'11');
return(c-'0');
}
int Input(Student room[],int n)
{
int i=0;
char sign;
while(sign!='n' && sign!='N')
{
printf("\t\t姓名:");
scanf("\t\t%s", &room[n+i].name);
printf("\t\t学号:");
scanf("\t\t%s", &room[n+i].num);
printf("\t\t房间号:");
scanf("\t\t%s",&room[n+i].roomnum);
printf("\t\t是否继续输入?(Y/N)");
scanf("\t\t%c",&sign);
i++;
}
return(n+i);
}
void Display_all_record(Student room[],int n)
{
int i;
printf("\t\t----------------------------------------\n");
printf("\t\t房间号 学号 姓名 \n");
printf("\t\t----------------------------------------\n");
for(i=1;i<n+1;i++)
{
printf("\t\t%-10s%-10s%-10s\n",room[i-1].roomnum,room[i-1].num,room[i-1].name);
}
printf("\t\t");
system("pause");
}
void Display_a_record(Student room[],int n)
{
printf("\t\t----------------------------------------\n");
printf("\t\t房间号 学号 姓名 \n");
printf("\t\t----------------------------------------\n");
printf("\t\t%-10s%-10s%-10s\n",room[n].roomnum,room[n].num,room[n].name); /*输出所查找的一条记录*/
printf("\n");
printf("\t\t");
}
void Sort_by_name(Student room[],int n)
{
int i,j;
char t[10];
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(strcmp(room[j].name,room[j+1].name)>0)
{
strcpy(t,room[j+1].name);
strcpy(room[j+1].name,room[j].name);
strcpy(room[j].name,t);
strcpy(t,room[j+1].num);
strcpy(room[j+1].num,room[j].num);
strcpy(room[j].num,t);
strcpy(t,room[j+1].roomnum);
strcpy(room[j+1].roomnum,room[j].roomnum);
strcpy(room[j].roomnum,t);
}
}
void Sort_by_num(Student room[],int n)
{
int i,j;
char t[10];
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(strcmp(room[j].num,room[j+1].num)>0)
{
strcpy(t,room[j+1].roomnum);
strcpy(room[j+1].roomnum,room[j].roomnum);
strcpy(room[j].roomnum,t);
strcpy(t,room[j+1].num);
strcpy(room[j+1].num,room[j].num);
strcpy(room[j].num,t);
strcpy(t,room[j+1].name);
strcpy(room[j+1].name,room[j].name);
strcpy(room[j].name,t);
}
}
void Sort_by_roomnum(Student room[],int n)
{
int i,j;
char t[10];
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(strcmp(room[j].roomnum,room[j+1].roomnum)>0)
{
strcpy(t,room[j+1].num);
strcpy(room[j+1].num,room[j].num);
strcpy(room[j].num,t);
strcpy(t,room[j+1].roomnum);
strcpy(room[j+1].roomnum,room[j].roomnum);
strcpy(room[j].roomnum,t);
strcpy(t,room[j+1].name);
strcpy(room[j+1].name,room[j].name);
strcpy(room[j].name,t);
}
}
void Find_record_name(Student room[],int n)
{
char s[10];
int i=0;
printf("\t\t输入要查找的姓名:");
scanf("%s",s);
while(strcmp(room[i].name,s)!=0 && i<n) i++;
if(i==n)
{
printf("\t\t对不起没有找到该学生!\n");
}
else
Display_a_record(room1,i);
}
void Find_record_rommnum(Student room[],int n) {
char s[20];
int i=0;
printf("\t\t输入要查找的房间号:");
scanf("%s",s);
while(strcmp(room[i].roomnum,s)!=0 && i<n) i++;
if(i==n)
{
printf("\t\t对不起没有找到该学生!\n");
}
else
Display_a_record(room1,i);
}
void Find_record_num(Student room[],int n)
{
char s[20];
int i=0;
printf("\t\t输入要查找的学号:");
scanf("%s",s);
while(strcmp(room[i].num,s)!=0 && i<n) i++;
if(i==n)
{
printf("\t\t对不起没有找到该学生!\n");
}
else
Display_a_record(room1,i);
}
int Insert_a_record(Student room[],int n)
{printf("\t\t学生姓名:");
scanf("\t\t%s",&room[n].name);
printf("\t\t学生学号:");
scanf("\t\t%s",&room[n].num);
printf("\t\t学生房间号:");
scanf("\t\t%s",&room[n].roomnum);
n++;
Sort_by_num(room,n);
printf("\t\t插入记录成功!\n");
return(n);
}
int AddfromText(Student room[],int n)
{
int i=0,num;
FILE *fp;
char filename[20];
printf("\t\t输入文件名:");
scanf("\t\t%s",&filename);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\t\t无法打开该文件\n");
printf("\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num);
while(i<num)
{ fscanf(fp,"%s%s%s",room[n+i].roomnum,room[n+i].num,room[n+i].name);
i++;
}
n+=num;
fclose(fp);
printf("\t\t读入成功!\n");
printf("\t\t");
system("pause");
return(n);
}
void WritetoText(Student room[],int n)
{
int i=0;
FILE *fp;
char filename[100];
printf("\t\t循环写入数据\n");
printf("\t\t输入文件名:");
scanf("\t\t%s",&filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\t\t无法打开该文件\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n);
while(i<n)
{
fprintf(fp,"%-10s%-10s%-10s\n",room[i].roomnum,room[i].num,room[i].name);
i++;
}
fclose(fp);
printf("写入成功!\n");
}
int main()
{
int n=0;
for(;;)
{
switch(menu())
{
case 1:
printf("\t\t输入记录\n");
n=Input(room1,n);
break;
case 2:
printf("\t\t显示记录\n");
Display_all_record(room1,n);
break;
case 3:
printf("\t\t按姓名排序并显示\n");
Sort_by_name(room1,n);
printf("\t\t排序成功!\n");
Display_all_record(room1,n);
printf("\t\t");
break;
case 4:
printf("\t\t按房间号排序并显示\n");
Sort_by_roomnum(room1,n);
printf("\t\t排序成功!\n");
Display_all_record(room1,n);
printf("\t\t");
break;
case 5:
printf("\t\t按学号排序并显示\n");
Sort_by_num(room1,n);
printf("\t\t\t排序成功!\n");
Display_all_record(room1,n);
printf("\t\t");
break;
case 6:
printf("\t\t按姓名查找\n");
Find_record_name(room1,n);
printf("\t\t");
system("pause");
break;
case 7:
printf("\t\t按房间号查找\n");
Find_record_rommnum(room1,n);
printf("\t\t");
system("pause");
break;
case 8:
printf("\t\t按学号查找\n");
Find_record_num(room1,n);
printf("\t\t");
system("pause");
break;
case 9:
printf("\t\t插入一条记录按学号排序并显示\n");
n=Insert_a_record(room1,n); Display_all_record(room1,n);
printf("\t\t");
break;
case 10:
printf("\t\t从文件中读入数据\n");
AddfromText(room1,n);
printf("\t\t");
system("pause");
break;
case 11:
printf("\t\t循环写入数据\n");
WritetoText(room1,n);
printf("\t\t");
system("pause");
break;
case 0:
printf("\t\t谢谢使用,祝您好运,再见!\n");
printf("\t\t");
system("pause");
exit(0);
}
}
return 0;
}
第十和第十一出错了