五、实验调试记录:
六、参考答案:
1、#include
{ char name[20]; int count;
}a[6]={“zhang”,0,“li”,0,“wang”,0,“zhao”,0,“liu”,0,“zhu”,0}; void main( )
{ int i,j;
char abc[20];
for(i=1;i<=10;i++)
{ printf(“输入候选人名字:”); scanf(“%s”,abc); for(j=0;j<6;j++)
if(strcmp(abc,a[j].name)==0) a[j].count++;
}
for(j=0;j<6;j++)
printf(“%s:%d\\n”,a[j].name,a[j].count); }
2、#include
#include
char stuname[10]; int math; int physics;
int computer; int sum; };
void printspace( ) { int i;
for(i=0;i<40;i++) printf(\ printf(\}
void printinformation(struct student stu[ ]) { int i;
printf(\ printspace( ); for(i=0;i { printf(\ stu[i].math,stu[i].physics,stu[i].computer,stu[i].sum,i+1); printf(\ } printspace( ); } void readinformation(struct student stu[ ]) { int i; printf(\ for(i=0;i { printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ stu[i].sum=stu[i].math+stu[i].physics+stu[i].computer; } } void sortbystunum(struct student stu[ ]) { int i,j; struct student t; for(i=0;i if(stu[j].stunum { t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } } void sortbystuname(struct student stu[ ]) { int i,j; struct student t; for(i=0;i if(strcmp(stu[j].stuname,stu[j+1].stuname)<0) { t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } } void sortbymath(struct student stu[ ]) { int i,j; struct student t; for(i=0;i if(stu[j].math { t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } } void sortbyphysics(struct student stu[ ]) { int i,j; struct student t; for(i=0;i if(stu[j].physics { t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } } void sortbycomputer(struct student stu[ ]) { int i,j; struct student t; for(i=0;i if(stu[j].computer { t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } } void sortbysum(struct student stu[ ]) { int i,j; struct student t; for(i=0;i for(j=0;j if(stu[j].sum { t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } } void sort(struct student stu[ ],int p) { switch(p) { case 1: sortbystunum(stu); break; case 2: sortbystuname(stu); break; case 3: sortbymath(stu); break; case 4: sortbyphysics(stu); break; case 5: sortbycomputer(stu); break; case 6: sortbysum(stu); break; default: printf(\ } } void sortselect(int *select) { printf(\ printspace( ); printf(\ printf(\ printf(\ printf(\ printspace( ); printf(“\\nYour choice:”); scanf(\} void main( ) { int selectnum; struct student stu1[N]; readinformation(stu1); sortselect(&selectnum); while(selectnum!=0) { sort(stu1,selectnum); printinformation(stu1); sortselect(&selectnum); } } 3、#include { FILE *fp_in,*fp_out; char infile[20],outfile[20]; printf(“Enter the infile name:”); scanf(“%s”,infile); printf(“Enter the outfile name:”); scanf(“%s”,outfile); if((fp_in=fopen(infile,“r”)==NULL) { printf(“Can’t open file:%s”,infile); exit(1); } if((fp_out=fopen(outfile,“w”)==NULL) { printf(“Can’t open file:%s”,outfile); exit(1); } while(!feof(fp_in)) fputc(fgetc(fp_in),fp_out); fclose(fp_in); fclose(fp_out); } 4、#include void main( ) { FILE *fp; char str[100]; int i; if((fp=fopen(“test”,“w”))==NULL) { printf(“Cannot open the file.\\n”); exit(0); } printf(“Input a string:”); gets(str); /*读入一行字符串*/ for(i=0;str[i]&&i<100;i++) /*处理该行中的每一个字符*/ { if(str[i]>=‘a’&&str[i]<=‘z’) /*若是小写字母*/ str[i]-=32; /*将小写字母转换为大写字母*/ fputc(str[i],fp); /*将转换后的字符写入文件*/ } fclose(fp); /*关闭文件*/ fp=fopen(“test”,“r”); /*以读方式打开文本文件*/ fgets(str,100,fp); /*从文件中读入一行字符串*/ printf(“%s\\n”,str); fclose(fp); }