C语言程序设计的试题及答案(8)

时间:2020-08-06 10:50:24 试题 我要投稿

C语言程序设计的试题及答案

  fun(s); printf("The string after deleted: "); puts(s); printf("\n\n"); return 0; fclose(fp); }

  2. 下列给定程序中,函数 fun()的功能是:从字符串 s 中,将大写字母转换成小写字母。程序 的功能是从键盘读入一个字符串,进行如上处理后,写入 D 盘上指定文件中。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构。 #include void fun(char s[]) { int i; for(i=0;s[i]!='\n';i++) if(s[i]>='A'&& s[i]<='Z') s[i]=s[i]+32; s[i]='\0'; } int main() { char *s; FILE fp; gets(s); if((fp=fopen("d:\\content.txt", "w"))=NULL) { printf("Cannot open file\n Press any key to exit!"); return 1; } fun(s); fputs(fp, s); printf("\n\n"); fclose(fp); return 0; } 3. 程序功能说明:该程序完成从文本文件中读入 10 名学生信息(学号、姓名、成绩),并用 格式输出显示到屏幕上。 #include #define N=10 struct stud_type { char name[10];

  int num; float score; } void main() { int i; struct stud_type stu[N]; FILE *fp; If((fp=fopen(''d:\\student.txt'', ''w'')) ==NULL) { printf(''Cannot open file\n Press any key to exit!''); getch(); } for(i=0;i #define N 10 struct stud_type { char *name; int num; float score; } void main() { int i; struct stud_type stu[N]; FILE *fp; If((fp=fopen(''d:\student.txt'', ''w'')) ==NULL) { printf(''Cannot open file\n Press any key to exit!''); getch(); } for(i=0;i

  请改正 。 #include #define N 10 struct student { char name[10]; int num; int age; char addr[15]; } s[N]; void save() int main() { int i; printf("please input student information:\n"); for(i=0;i

  6. 以下程序中有五处错误,请改正。 #include #include

  int main( ) { FILE fp; char ch; if((fp = fopen("d:\test.txt","w+")) == NULL) { printf("Cannot open file\n Press any key exit!"); getch( ); return 1; } printf("input a string:\n"); ch = getchar( ); while(ch != \n) { fputs(ch,fp); ch = getchar( ); } rewind(fp); ch = fgetc(fp); while(ch != "EOF") { putchar(ch); ch = fgetc(fp); } printf("\n"); fclose(fp); return 0; } 7. 以下程序中有五处错误,请改正。 #include #include int main( ) { FILE *fp; char ch; if(fp = fopen("d:\\test.txt","w+") == NULL) { printf("Cannot open file\n Press any key exit!"); getch( ); return 1; } printf("input a string:\n"); ch = getchar( );

  while(ch != ‘\0’) { fputc(fp, ch); ch = getchar( ); } rewind(fp); ch = fgetc(fp); while(ch !=end) { putchar(ch); ch = fgetc(fp); } printf("\n"); fclose( ); return 0; } 8. 将 d:\lang\tc 文件夹下的文件 deposit.c 内容输出到屏幕上。有 5 处错误,请改正。 #include #include int main( ) { char ch; FILE fp; if ((fp=fopen(“d:\\lang\\tc\\deposit.c”,”r”))=NULL) , printf(“Cannot open file \n”); break; } while ((ch=getchar())!=EOF) putchar(ch); fclose(ch); return 0; } 9. 将 d:\lang\tc 文件夹下的文件 deposit.c 内容全部以小写字母输出到屏幕上。 #include #include void main( ) { char ch; FILE *fp; if ((fp=fopen("d:\lang\tc\deposit.c","r"))==NULL) { printf("Cannot open file \n"); return 1; } while (ch=fgetc(fp)!=EOF) {

  if ((ch>='A'||ch <='Z')) } fclose(fp); return 0;

  ch=ch+32;

  puts(ch);

  }

  10. 以下程序中用户由键盘输入一个文件名,然后输入一串字符(用#结束输入)存放到此 文 件文件中形成文本文件,并将字符的个数写到文件尾部。 #include #include void main() { FILE *fp; char ch,fname[32]; int count=0; printf("Input the filename :"); scanf("%s",&fname[32]); if ((fp=fopen("fname","w+"))==NULL) { printf("Can't open file:%s \n",fname); exit(0); } printf("Enter data:\n"); while ((ch=getchar())!="#") { fputc(fp, ch); count++; } fprintf("\n%d\n",count); fclose(fp); }

  11. 下面程序的功能是从 filename1 中将文件拷贝到 filename2,其中源文件和目标文件名均 从键盘输入。有五处错误,请改正。 #include #include void main() { FILE *fp1,fp2; char ch,*fname1,*fname2; "); scanf("%s",fname1); if ((fp1=fopen(fname1,"r"))==NULL)

  printf("Input the source file name :

  { printf("Can't open source file:%s \n",fname1); exit(0); } printf("Input the targate file name :"); scanf("%s",fname2); if ((fp2=fopen(fname2,"w"))==NULL) { printf("Can't open targate file:%s \n",*fname2); exit(0); } while((ch=fgetc(*fp1))=EOF) fputc(ch,fp2); fclose(fp1); fclose(fp2); }

  12. 下面程序的功能是从 filename1 中将内容读出,显示在屏幕上。其中文件名由键盘输入。 有五处错误,请改正。

  #include #include void main() { FILE fp1; char ch,*fname1; printf("Input the file name :"); scanf("%s",fname1); if ((fp1=fopen(fname1,’r’))==NULL) { printf("Can't open source file:%s \n",fname1); exit(0); } while(ch=fgetc(fp1)!=EOF) put(ch); fclose(fp1); }

  13. 下列程序中存在 5 处 C 语言语法错误,请识别并改正。程序的功能是在已建立的文件 write string.txt 中追加一个字符串,并将文件整个内容输出。 #include #include int main { FILE *fp; char ch,st[20]; if((fp=fopen("d:\\lang\\tc\\writestring.txt","w"))==NULL) { printf("Cannot open file strike any key exit!"); getchar(); return 1; } printf("input a string:\n"); scanf("%c",st); fgets(fp, st); rewind(); while(ch=fputc(fp)!=EOF) putchar(ch); printf("\n"); fclose(fp); return 0; }

  14. 以下程序的功能:把文件 source.txt 的内容输出到屏幕上。 (共 5 个错误点) #include #include int main() { char *fp; char ch; fp=fopen("source.txt", "w"); if(fp=NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp);

  if(ch!=EOF) { printf("%c",ch); ch=fgetc(fp); } fclose(*fp); } 15. 以下程序的功能:把文件 source1.txt 和文件 source2.txt 的内容相连,输出到屏幕上。 共 5 个错误点,请改正。 #include #include int main() { FILE *fp1,fp2; char ch; fp1=fopen("source1.txt", "w"); fp2=fopen("source2.txt", "r"); if(fp1==NULL&& fp2==NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp2); if(ch!=EOF) { fputc(ch,fp1); ch=fgetc(fp2); } fclose(fp1,fp2); return 0; } 16. 下面程序的功能是统计文件中字符的个数, 其中有 5 处错误, 请查找并改正 (每个 2 分) #include #include void main() { FILE *fp; int count; char ch; *fp=fopen("source.txt", "w"); if(*fp=NULL) {

  printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp); while(ch!=EOF) { count++; ch=fgetc(fp); } printf("\n 字符个数=%f\n",count); fclose(fp); return 0; } 17. 以下程序的功能:把文件 source.txt 的内容读出,压缩掉其中的空格,存入文件 target. txt 中,其中有 5 出错误,请检查并改正。 #include #include int main() { FILE *fp1,fp2; char ch; fp1=fopen("source.txt", "r"); fp2=fopen("target.txt", "w"); if(fp1=NULL|| fp2=NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } ch=fgetc(fp1); if (ch!=EOF) { while (ch!=' ') fputc(ch,fp2); ch=fgetc(fp1); } fclose(fp1,fp2); return 0; }

  18. 程序功能说明:该程序完成从文本文件中读入 10 名学生信息(学号、姓名、成绩),求 成 学生平均分,显示在屏幕上。其中有 5 处错误,请检查并改正。 #include #define N 10; struct stud_type { char name[10]; int num; float score; }; void main() { int i; struct stud_type stu[N]; float s=0; FILE *fp; If((fp=fopen(''d:\\student.txt'', ''w'')) ==NULL) { printf(''Cannot open file\n Press any key to exit!''); getch(); } for(i=0;i

  19. 以下程序的功能:把字符串“hello world”及文件 source.txt 的内容相连,输出到文件 target.txt 中。 (共 5 个错误点,请改正) #include #include int main() { FILE *fp1,*fp2; char ch; char str= "hello world"; fp1=fopen("d:\source.txt", "r"); fp2=fopen("d:\target.txt", "w");

  if(fp1==NULL&& fp2==NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); exit(1); } fputs(str, fp2); while(ch=fgetc(fp1)!=EOF) { fputc(ch,fp2); } fclose(fp1); fclose(fp2); return 0; } 20. 下面程序的功能是统计文件中大写字符的个数,其中有 5 处错误,请查找并改正。 #include #include int main() { FILE *fp; int count; char ch; *fp=fopen("source.txt", "r"); if(*fp=NULL) { printf("\nCannot open file\nStrike any key exit!\n"); getchar(); return 1; } ch=fgetc(fp); if (ch!=EOF) { if (ch>=’A’&&ch<=’Z’) count++; ch=fgetc(fp); } printf("\n 大写字符个数=%f\n",count); fclose(); return 0; } 解答: 1. (1)if((fp=fopen(''d:\\content.txt'', ''r''))==NULL) (2)void fun(char *s) (3)for(i=j=0; s[i]!= ?\0?;i++)

  (4)if(s[i]>='A'&& s[i]<='Z') (5) s[j++]=s[i]; 2. (1)for(i=0;s[i]!='\0';i++) (2)char s[80]; (3)FILE *fp; (4)if((fp=fopen("d:\\content.txt", "w"))==NULL) (5)fputs( s,fp); 3. (1)#define N 10 (2)struct stud_type { char name[10]; int num; float score; }; (3) If((fp=fopen(''d:\\student.txt'', ''r'')) ==NULL) (4), (5) fscanf(fp, "%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score); 4. (1)char name[30] (2)struct stud_type { char name[10]; int num; float score; }; (3) If((fp=fopen(''d:\\student.txt'', ''w'')) ==NULL) (4) scanf("%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score); (5) fprintf(fp,"%8s%5d%8.2f\n",stu[i].name,stu[i].num,stu[i].score); 5. void save(); scanf("%s%d%d%s",&s[i].name,&s[i].num,&s[i].age,s[i].addr); FILE *fp; if (fwrite(&s[i],sizeof(struct student),1,fp)=0) fclose(); 6. ① FILE *fp; ② if((fp = fopen("d:\\test.txt","w+")) == NULL) ③ while(ch !=’ \n’) ④ fputc(ch,fp); ⑤ while(ch != EOF) 7. if((fp = fopen("d:\\test.txt","w+") )== NULL) while(ch != ‘\n’) fputc(ch, fp); while(ch !=EOF) fclose(fp );

  8. FILE *fp; if ((fp=fopen("d:\\lang\\tc\\deposit.c","r"))==NULL) return 1; while ((ch=fgetc(fp))!=EOF) fclose(fp); 9. int main( ) if ((fp=fopen("d:\\lang\\tc\\deposit.c","r"))==NULL) while ((ch=fgetc(fp))!=EOF) if ((ch>='A'&&ch <='Z')) ch=ch+32; putchar(ch); 10. scanf("%s",fname); if ((fp=fopen( fname,"w+"))==NULL) while ((ch=getchar())!='#') fputc(ch,fp); fprintf( fp,"\n%d\n",count); 11. 1、FILE *fp1,*fp2; 2、char ch,fname1[32],fname2[32]; 3、printf("Can't open targate file:%s \n",fname2); 4、5、while((ch=fgetc(fp1))!=EOF) 12. 1、FILE *fp1 ; 2、char ch,fname1[32]; 3、if ((fp1=fopen(fname1, "r"))==NULL) 4、while((ch=fgetc(fp1))!=EOF) 5、putchar(ch); 13. 1、if((fp=fopen("d:\\lang\\tc\\writestring.txt","a+"))==NULL) ; 2、scanf("%s",st); 3、fgets(st, fp); 4、rewind(fp); 5、while((ch=fputc(fp))!=EOF) 14. FILE *fp; fp=fopen("source.txt", "r"); if(fp= =NULL) while(ch!=EOF) fclose(fp); 15. FILE *fp1,*fp2; fp1=fopen("source1.txt", "a+"); if(fp1==NULL|| fp2==NULL) while(ch!=EOF) fclose(fp1); fclose(fp2); 16. int main()

  int count=0; fp=fopen("source.txt", "r"); if(fp==NULL) printf("\n 字符个数=%d\n",count); 17. FILE *fp1,*fp2; if(fp1==NULL|| fp2==NULL) while(ch!=EOF) if (ch!=' ') fputc(ch,fp2); fclose(fp1); fclose(fp2); 18. (1)#define N 10 (2)If((fp=fopen(''d:\\student.txt'', ''r'')) ==NULL) (3)fscanf(fp, "%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score); (4) s=s+stu[i].score; (5) fclose(fp); 19. char str[]= "hello world"; fp1=fopen("d:\\source.txt", "r"); fp2=fopen("d:\\target.txt", "w"); if(fp1==NULL|| fp2==NULL) while((ch=fgetc(fp1))!=EOF) 20. fp=fopen("source.txt", "r"); if(fp==NULL) while(ch!=EOF) printf("\n 大写字符个数=%d\n",count); fclose(fp); 五.编程一 解答: 六.编程二 解答:

【C语言程序设计的试题及答案】相关文章:

1.C语言面试笔试题

2.c语言结构体程序设计心得体会 学习c语言的心得体会_心得体会

3.jsp试题及答案

4.护士试题及答案

5.《看戏》的阅读试题及答案

6.秘书基础试题及答案

7.医保培训试题及答案

8.地理试题及答案

9.工程材料试题及答案