C語(yǔ)言程序設(shè)計(jì)的試題及答案
C語(yǔ)言程序設(shè)計(jì)的試題及答案
fun(s); printf("The string after deleted: "); puts(s); printf("\n\n"); return 0; fclose(fp); }
2. 下列給定程序中,函數(shù) fun()的功能是:從字符串 s 中,將大寫(xiě)字母轉(zhuǎn)換成小寫(xiě)字母。程序 的功能是從鍵盤讀入一個(gè)字符串,進(jìn)行如上處理后,寫(xiě)入 D 盤上指定文件中。 注意:不要改動(dòng) main 函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。 #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. 程序功能說(shuō)明:該程序完成從文本文件中讀入 10 名學(xué)生信息(學(xué)號(hào)、姓名、成績(jī)),并用 格式輸出顯示到屏幕上。 #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
請(qǐng)改正 。 #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. 以下程序中有五處錯(cuò)誤,請(qǐng)改正。 #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. 以下程序中有五處錯(cuò)誤,請(qǐng)改正。 #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 內(nèi)容輸出到屏幕上。有 5 處錯(cuò)誤,請(qǐng)改正。 #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 內(nèi)容全部以小寫(xiě)字母輸出到屏幕上。 #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. 以下程序中用戶由鍵盤輸入一個(gè)文件名,然后輸入一串字符(用#結(jié)束輸入)存放到此 文 件文件中形成文本文件,并將字符的個(gè)數(shù)寫(xiě)到文件尾部。 #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,其中源文件和目標(biāo)文件名均 從鍵盤輸入。有五處錯(cuò)誤,請(qǐng)改正。 #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 中將內(nèi)容讀出,顯示在屏幕上。其中文件名由鍵盤輸入。 有五處錯(cuò)誤,請(qǐng)改正。
#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 語(yǔ)言語(yǔ)法錯(cuò)誤,請(qǐng)識(shí)別并改正。程序的功能是在已建立的文件 write string.txt 中追加一個(gè)字符串,并將文件整個(gè)內(nèi)容輸出。 #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 的內(nèi)容輸出到屏幕上。 (共 5 個(gè)錯(cuò)誤點(diǎn)) #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 的內(nèi)容相連,輸出到屏幕上。 共 5 個(gè)錯(cuò)誤點(diǎn),請(qǐng)改正。 #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. 下面程序的功能是統(tǒng)計(jì)文件中字符的個(gè)數(shù), 其中有 5 處錯(cuò)誤, 請(qǐng)查找并改正 (每個(gè) 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 字符個(gè)數(shù)=%f\n",count); fclose(fp); return 0; } 17. 以下程序的功能:把文件 source.txt 的內(nèi)容讀出,壓縮掉其中的空格,存入文件 target. txt 中,其中有 5 出錯(cuò)誤,請(qǐng)檢查并改正。 #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. 程序功能說(shuō)明:該程序完成從文本文件中讀入 10 名學(xué)生信息(學(xué)號(hào)、姓名、成績(jī)),求 成 學(xué)生平均分,顯示在屏幕上。其中有 5 處錯(cuò)誤,請(qǐng)檢查并改正。 #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 的內(nèi)容相連,輸出到文件 target.txt 中。 (共 5 個(gè)錯(cuò)誤點(diǎn),請(qǐng)改正) #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. 下面程序的功能是統(tǒng)計(jì)文件中大寫(xiě)字符的個(gè)數(shù),其中有 5 處錯(cuò)誤,請(qǐng)查找并改正。 #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 大寫(xiě)字符個(gè)數(shù)=%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 字符個(gè)數(shù)=%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 大寫(xiě)字符個(gè)數(shù)=%d\n",count); fclose(fp); 五.編程一 解答: 六.編程二 解答: