C语言程序设计题。。急!!!帮忙改下错误,完美运行以后追加100分!!!
#include<stdio.h>#include<stdlib.h>#include<string.h>intN;structTeacher{intcode;charn...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int N;
struct Teacher
{int code;
char name[20];
char position[20];
char sex;
float score;
}teacher[100];
struct Teacher temp;
int main()
{
int choice1,choice2,a,i;
char b[20],c[20];
float d;
printf("请输入教师记录:职工号,姓名,职称,性别,评分。以逗号分隔,输入句号停止录入。\n");
for(i=0;i<100;i++)
{
if(getchar()=='.')
N=i;
break;
if(getchar()!='.')
scanf("%d,%s,%s,%c,%f",&teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,&teacher[i].score);
}
save();
print();
printf("按职工号排序请按1,按姓名排序请按2,按职称排序请按3,按评分排序请按4。\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:case1();break;
case 2:case2();break;
case 3:case3();break;
case 4:case4();break;
default:printf("输入不合法!\n");
}
printf("按职工号查找请按1,按姓名查找请按2,按职称查找请按3,按评分查找请按4。\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1:caseA();break;
case 2:caseB();break;
case 3:caseC();break;
case 4:caseD();break;
default:printf("输入不合法!\n");
}
return 0;
}
void save()
{FILE *fp;
int i;
if((fp=fopen("teacher.dat","wb"))==NULL)
{
printf("cannot open file.\n");
return;
}
for(i=0;i<100;i++)
if(fwrite(&teacher[i],sizeof(struct Teacher),1,fp)!=1)
printf("file write error.\n");
fclose(fp);
}
void print()
{FILE *fp;
int i;
if((fp=fopen("teacher.dat","rb"))==NULL)
{printf("cannot open file.\n");
exit(0);
}
for(i=0;i<=N;i++,fp++)
{fread(&teacher[i],sizeof(struct Teacher),1,fp);
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
fclose(fp);
}
void case1()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].code>teacher[i+1].code)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case2()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].name>teacher[i+1].name)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case3()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].position>teacher[i+1].position)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case4()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].score>teacher[i+1].score)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
} 展开
#include<stdlib.h>
#include<string.h>
int N;
struct Teacher
{int code;
char name[20];
char position[20];
char sex;
float score;
}teacher[100];
struct Teacher temp;
int main()
{
int choice1,choice2,a,i;
char b[20],c[20];
float d;
printf("请输入教师记录:职工号,姓名,职称,性别,评分。以逗号分隔,输入句号停止录入。\n");
for(i=0;i<100;i++)
{
if(getchar()=='.')
N=i;
break;
if(getchar()!='.')
scanf("%d,%s,%s,%c,%f",&teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,&teacher[i].score);
}
save();
print();
printf("按职工号排序请按1,按姓名排序请按2,按职称排序请按3,按评分排序请按4。\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:case1();break;
case 2:case2();break;
case 3:case3();break;
case 4:case4();break;
default:printf("输入不合法!\n");
}
printf("按职工号查找请按1,按姓名查找请按2,按职称查找请按3,按评分查找请按4。\n");
scanf("%d",&choice2);
switch(choice2)
{
case 1:caseA();break;
case 2:caseB();break;
case 3:caseC();break;
case 4:caseD();break;
default:printf("输入不合法!\n");
}
return 0;
}
void save()
{FILE *fp;
int i;
if((fp=fopen("teacher.dat","wb"))==NULL)
{
printf("cannot open file.\n");
return;
}
for(i=0;i<100;i++)
if(fwrite(&teacher[i],sizeof(struct Teacher),1,fp)!=1)
printf("file write error.\n");
fclose(fp);
}
void print()
{FILE *fp;
int i;
if((fp=fopen("teacher.dat","rb"))==NULL)
{printf("cannot open file.\n");
exit(0);
}
for(i=0;i<=N;i++,fp++)
{fread(&teacher[i],sizeof(struct Teacher),1,fp);
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
fclose(fp);
}
void case1()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].code>teacher[i+1].code)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case2()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].name>teacher[i+1].name)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case3()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].position>teacher[i+1].position)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case4()
{
int i;
for(i=0;i<=N;i++)
if(teacher[i].score>teacher[i+1].score)
{temp=teacher[i+1];teacher[i+1]=teacher[i];teacher[i]=temp;}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
} 展开
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int N;
struct Teacher
{
int code;
char name[20];
char position[20];
char sex[5];
float score;
}teacher[100];
struct Teacher temp;
void save()
{
FILE *fp;
int i;
if((fp=fopen("teacher.dat","w+"))==NULL)
{
printf("cannot open file.\n");
exit(0);
}
for(i=0;i<=N;i++)
fprintf(fp,"%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
fclose(fp);
}
void print()
{
FILE *fp;
int i;
if((fp=fopen("teacher.dat","rb"))==NULL)
{
printf("cannot open file.\n");
exit(0);
}
for(i=0;i<=N;i++,fp++)
{
fscanf(fp,"%d\t%s\t%s\t%s\t%f\n",&teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,&teacher[i].score);
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
}
fclose(fp);
}
void case1()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].code>teacher[i+1].code)
{
temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,
teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case2()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].name>teacher[i+1].name)
{
temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,
teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case3()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].position>teacher[i+1].position)
{temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case4()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].score>teacher[i+1].score)
{
temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
}
int main()
{
int choice1,choice2,a,i=-1,temp;
char ch,b[20],c[20];
float d;
while(1)
{
i++;
printf("请输入第%d个教师记录:职工号,姓名,职称,性别,评分。\n说明:以,隔开\n",i+1);
scanf("%d%s%s%s%f",&teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,&teacher[i].score);
ch=getchar();
printf("继续输入请输入1回车\n输入完成输入0回车!\n");
scanf("%d",&temp);
if(temp!=1)
{
N=i;
break;
}
}
save();
print();
printf("按职工号排序请按1,按姓名排序请按2,按职称排序请按3,按评分排序请按4。\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:case1();break;
case 2:case2();break;
case 3:case3();break;
case 4:case4();break;
default:printf("输入不合法!\n");
}
printf("按职工号查找请按1,按姓名查找请按2,按职称查找请按3,按评分查找请按4。\n");
scanf("%d",&choice2);
return 0;
}
//你的查找函数还没有写吧,上面能正常运行保存、输出、排序功能,你把查找函数写下就行了
#include<stdlib.h>
#include<string.h>
int N;
struct Teacher
{
int code;
char name[20];
char position[20];
char sex[5];
float score;
}teacher[100];
struct Teacher temp;
void save()
{
FILE *fp;
int i;
if((fp=fopen("teacher.dat","w+"))==NULL)
{
printf("cannot open file.\n");
exit(0);
}
for(i=0;i<=N;i++)
fprintf(fp,"%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
fclose(fp);
}
void print()
{
FILE *fp;
int i;
if((fp=fopen("teacher.dat","rb"))==NULL)
{
printf("cannot open file.\n");
exit(0);
}
for(i=0;i<=N;i++,fp++)
{
fscanf(fp,"%d\t%s\t%s\t%s\t%f\n",&teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,&teacher[i].score);
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
}
fclose(fp);
}
void case1()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].code>teacher[i+1].code)
{
temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,
teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case2()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].name>teacher[i+1].name)
{
temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,
teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case3()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].position>teacher[i+1].position)
{temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
}
void case4()
{
int i;
for(i=0;i<N;i++)
if(teacher[i].score>teacher[i+1].score)
{
temp=teacher[i+1];
teacher[i+1]=teacher[i];
teacher[i]=temp;
}
for(i=0;i<=N;i++)
printf("%d\t%s\t%s\t%s\t%f\n",teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,teacher[i].score);
}
int main()
{
int choice1,choice2,a,i=-1,temp;
char ch,b[20],c[20];
float d;
while(1)
{
i++;
printf("请输入第%d个教师记录:职工号,姓名,职称,性别,评分。\n说明:以,隔开\n",i+1);
scanf("%d%s%s%s%f",&teacher[i].code,teacher[i].name,
teacher[i].position,teacher[i].sex,&teacher[i].score);
ch=getchar();
printf("继续输入请输入1回车\n输入完成输入0回车!\n");
scanf("%d",&temp);
if(temp!=1)
{
N=i;
break;
}
}
save();
print();
printf("按职工号排序请按1,按姓名排序请按2,按职称排序请按3,按评分排序请按4。\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1:case1();break;
case 2:case2();break;
case 3:case3();break;
case 4:case4();break;
default:printf("输入不合法!\n");
}
printf("按职工号查找请按1,按姓名查找请按2,按职称查找请按3,按评分查找请按4。\n");
scanf("%d",&choice2);
return 0;
}
//你的查找函数还没有写吧,上面能正常运行保存、输出、排序功能,你把查找函数写下就行了
追问
知道里发不下了,能帮忙看看查找函数有什么错误么?感谢~!
void caseA()
{
int i,a;
printf("请输入职工号:\n");
scanf("%d",&a);
for(i=0;i<=N;i++)
if(a==teacher[i].code)
printf("%d\t%s\t%s\t%c\t%f\n",teacher[i].code,teacher[i].name,teacher[i].position,teacher[i].sex,teacher[i].score);
}
只能发第一个。。。
追答
查找到后加个break结束for循环就行了
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void save();
void print();
void case1();
void case2();
void case3();
void case4();
/*增加下面4个函数的实现*/
void caseA();
void caseB();
void caseC();
void caseD();
/*在文件的最后面实现下列函数内容*/
void caseA(){}
void caseB(){}
void caseC(){}
void caseD(){}
#include<stdlib.h>
#include<string.h>
void save();
void print();
void case1();
void case2();
void case3();
void case4();
/*增加下面4个函数的实现*/
void caseA();
void caseB();
void caseC();
void caseD();
/*在文件的最后面实现下列函数内容*/
void caseA(){}
void caseB(){}
void caseC(){}
void caseD(){}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
case 1:caseA();break;
case 2:caseB();break;
case 3:caseC();break;
case 4:caseD();break;
这里,你这四个函数没有啊,所以程序没法改啊,在哪拷的啊?
case 2:caseB();break;
case 3:caseC();break;
case 4:caseD();break;
这里,你这四个函数没有啊,所以程序没法改啊,在哪拷的啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误有点多
1、save 和 print 函数在主函数中没有声明就调用,编译器在编译的时候会出现错误
2、还有就是代码不全,caseA,caseB。。。。在函数中没有实现
3、case1 ,。。。放的错误和1,一样
你先改改吧
1、save 和 print 函数在主函数中没有声明就调用,编译器在编译的时候会出现错误
2、还有就是代码不全,caseA,caseB。。。。在函数中没有实现
3、case1 ,。。。放的错误和1,一样
你先改改吧
追问
问一下,用scanf进行结构体输入时,系统会自动把逗号后面的内容赋给name,换成空格又会出错。该怎么解决呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询