C语言上机实验 答案(6)

2019-07-27 10:17

五、实验调试记录:

六、参考答案:

1、#include #include struct person

{ 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 #define N 3 struct student { int stunum;

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 void main( )

{ 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);

}


C语言上机实验 答案(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:上海市14校2017届联考高三英语试卷

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: