久久一区二区三区精品-久久一区二区明星换脸-久久一区二区精品-久久一区不卡中文字幕-91精品国产爱久久久久久-91精品国产福利尤物免费

C語言程序設計的試題及答案

雕龍文庫 分享 時間: 收藏本文

C語言程序設計的試題及答案

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); 五.編程一 解答: 六.編程二 解答:

主站蜘蛛池模板: 日本人成在线视频免费播放 | 欧洲乱码伦视频免费 | 高清毛片免费看 | 精品一区二区高清在线观看 | 很黄很色的摸下面的视频 | 亚洲大片免费 | 456亚洲视频 | 成人国产永久福利看片 | 欧美视频一区二区三区精品 | 国产免费一区不卡在线 | 精品一区二区三区在线观看l | 日本一级特黄在线播放 | 欧美在线一区二区三区不卡 | 日韩在线三级 | 国产亚洲精品午夜高清影院 | japanesehd国产在线无毒不卡 | 日本b站一卡二不卡 | 欧美极品欧美精品欧美视频 | 欧美精品国产一区二区三区 | 亚洲精品98久久久久久中文字幕 | 欧美α一级毛片 | 亚洲欧美日韩高清在线看 | 一级毛片成人免费看免费不卡 | 国产一级一国产一级毛片 | 欧美亚洲第一区 | 国产一区二区三区不卡免费观看 | 一级毛片真人免费观看 | 欧美日韩中文国产一区二区三区 | 99精品视频在线观看免费播放 | 欧美一级片在线视频 | 亚洲国产精品综合久久网络 | 久久毛片免费看 | 久久国产亚洲精品 | 欧美大尺度xxxxx视频 | 国产精品九九久久一区hh | 在线不卡国产 | 91四虎国自产在线播放线 | 国产一区二区三区影院 | 97国产在线视频公开免费 | 欧美成人www在线观看网页 | 91情侣在线偷精品国产 |