《C语言程序设计教程》(主编黄迪明、余勤)第九章课后习题答案

xiaoxiao2021-02-28  91

       在阅读代码前,先说一下这本书的在版编目(CIP)数据:C语言程序设计教程/黄迪明、余勤主编.--北京:国防工业出版社,2006.5    ISBN 7-118-04516-0

       所有代码均在 VS2013中成功运行。第一次写博客,若有不妥之处,非常欢迎您提出问题,若能提出改进意见,不胜感激!

9.1  编写把从键盘输入的信息存入到指定的文件中去的程序,要求文件名用命令行参数确定。

#include<stdio.h> int main() { printf(" 编写把从键盘输入的信息存入到指定的文件中去的程序,要求文件名用命令行参数确定。\n\n"); char c1[400],file_name[40]; printf("请输入要存储的信息:"); gets(c1); printf("输入要建立的文件名:"); gets(file_name); FILE *fp = fopen(file_name,"r+"); if (fp==NULL) { printf("Can't open the file!"); return; } fprintf(fp,"%s",c1); fclose(fp); return 0; }

9.2  编写将字符串"Data Structure","Operating System","Computer Graphics","Software Engeerning"写入文件中去的程序。

#include<stdio.h> int main() { printf("编写将字符串\"Data Structure\",\"Operating System\",\"Computer Graphics\",\"Software Engeerning\"写入文件中去的程序。"); char c1[40], c2[40], c3[40], c4[40]; printf("请输入第一个字符串:"); gets(c1); printf("请输入第二个字符串:"); gets(c2); printf("请输入第三个字符串:"); gets(c3); printf("请输入第四个字符串:"); gets(c4); char file_name[256] = { '\0' }; printf("输入要建立的文件名:"); gets(file_name); FILE *fp = fopen(file_name, "at+"); if (fp == NULL) { printf("Can't open the file!"); return; } fprintf(fp, "字符串1:%s 字符串2:%s 字符串3:%s 字符串4:%s", c1, c2, c3, c4); fclose(fp); return 0; }

9.3  重写9.5.cpp,将在显示屏上输出文件内容改为计算文件中的字符个数。

//将输出文件内容改为计算字符个数 #include<stdio.h> #define size 80 void main() { printf(" 重写9.5.cpp,将在显示屏上输出文件内容改为计算文件中的字符个数。"); char file_name[40]; printf("请输入已有的文件名:"); gets(file_name); FILE *fp = fopen(file_name, "r"); if (fp==NULL) { printf("Can't open the file."); return; } int n = 0,c; while ((c=getc(fp))!=EOF) { n = n + 1; } printf("文件中字符的个数为:%d",n); fclose(fp); }

9.4  重写9.5.cpp,把要处理的行文文件内容全部改为大写后,写入一个新文件中。

#include<stdio.h> #define size 80 void main() { printf(" 重写9.5.cpp,把要处理的行文文件内容全部改为大写后,写入一个新文件中。\n\n"); char c1[40] = { '\0' }, c2[40] = {'\0'}; printf("请输入需要修改的文件名:"); gets(c1); FILE *fp1 = fopen(c1, "w"); if (fp1 == NULL) { printf("Can't open the file!"); return; } char cc[100] = {"\0"}; printf("请输入文件的内容:"); gets(cc); fprintf(fp1,"%s",cc); fclose(fp1); fp1 = fopen(c1, "r"); printf("请输入新建的文件名:"); gets(c2); FILE *fp2 = fopen(c2, "w"); if (fp2 == NULL) { printf("Can't open the file!"); return; } char cc1; while ((cc1 = getc(fp1)) != EOF) { if (cc1 >= 'a'&&cc1 <= 'z') { cc1 = cc1 - 32; } putc(cc1, fp2); } fclose(fp2); fp2 = fopen(c2,"r"); printf("文件的内容全部改为大写后的结果如下:\n"); char cc2; while ((cc2 = getc(fp2)) != EOF) { putchar(cc2); } fclose(fp2); fclose(fp1); getch(); }

9.5    重写9.5.cpp,将由命令行指定的文件在显示屏上输出,计算并输出文件包含的行数和字符个数。

#include<stdio.h> void main() { printf("重写9.5.cpp,将由命令行指定的文件在显示屏上输出,计算并输出文件包含的行数和字符个数。\n\n"); char cc[100] = { "\0" }, c; int n = 0, line = 1; printf("请输入文件名:"); gets(cc); FILE *fp = fopen(cc,"r"); if (fp==NULL) { printf("文件打开失败!"); return; } while ((c=getc(fp))!=EOF) { n = n + 1; if (c=='\n') { n = n - 1; line = line + 1; } } printf("该文件的行数为:%d,字符个数为:%d",line,n); getch(); fclose(fp); }

9.6  使用I/O重定向,把9.5.cpp程序改写成一个拷贝文件的命令。

#include<stdio.h> void main() { printf(" 使用I/O重定向,把9.5.cpp程序改写成一个拷贝文件的命令。\n\n"); FILE *fp1, *fp2; char cc1[100] = " ", cc2[100] = " ",c; printf("请输入要拷贝的文件名:"); gets(cc1); fp1 = fopen(cc1,"r"); if (fp1==NULL) { printf("文件打开失败!"); return; } printf("请创建被拷贝的文件名:"); gets(cc2); fp2 = fopen(cc2,"w"); if (fp2==NULL) { printf("文件打开失败!"); return; } while ((c=getc(fp1))!=EOF) { putc(c,fp2); } return 0; fclose(fp1); fclose(fp2); }

9.7  编写统计由命令行参数指定的文件中最长行所具有的字符个数的程序。

#include<stdio.h> void main() { printf(" 编写统计由命令行参数指定的文件中最长行所具有的字符个数的程序。\n\n"); char cc[100] = " ",c; int temp = 0,n = 0; printf("请输入文件名:"); gets(cc); FILE *fp = fopen(cc,"r"); if (fp==NULL) { printf("文件打开失败!"); return; } while ((c=getc(fp))!=EOF) { if (c!='\n') { temp = temp + 1; } else { if (n<temp) { n = temp; temp = 0; } } } printf("该文件中最长行所具有的字符个数为:%d",n); getch(); fclose(fp); }

9.8  编写一个比较两个文件的程序,要求显示两个文件中不同行的行号以及该行中不相同的字符的开始位置。

#include<stdio.h> #include<string.h> #define size 100 int open_and_computehang(FILE *fp); void compare(FILE *fp1, FILE *fp2, int n1, int n2); int open_and_computehang(FILE *fp) { if (fp == NULL) { printf("文件打开失败!\n"); return; } int n = 1; char c; while ((c = getc(fp)) != EOF) { if (c == '\n') { n = n + 1; } } fclose(fp); return n; } void compare(FILE *fp1, FILE *fp2,int n1,int n2) { int n = 0; if (n1 == n2) { char c1[100] = { '\0' }, c2[100] = { '\0' }; while (fgets(c1, size, fp1)) { while (fgets(c2, size, fp2)) { n = n + 1; int len1 = 0, len2 = 0; len1 = strlen(c1); len2 = strlen(c2); int max = (len1 > len2 ? len1 : len2); int i = 0; for (i = 0; i < max; i++) { if (c1[i] - c2[i] != 0) { printf("第 %d 行两者的第 %d 个字符开始不相同"); break; } } } } } else { char c1[100] = { '\0' }, c2[100] = { '\0' }; while (fgets(c1, size, fp1)) { while (fgets(c2, size, fp2)) { n = n + 1; int len1 = 0, len2 = 0; len1 = strlen(c1); len2 = strlen(c2); int max = (len1 > len2 ? len1 : len2); int i = 0; for (i = 0; i < max; i++) { if (c1[i] - c2[i] != 0) { printf("第 %d 行两者的第 %d 个字符开始不相同\n", n, i + 1); break; } } } } } getch(); fclose(fp1); fclose(fp2); } void main() { printf("编写一个比较两个文件的程序,要求显示两个文件中不同行的行号以及该行中不相同的字符的开始位置。\n\n"); FILE *fp1, *fp2; int n1 = 1, n2 = 1; printf("请输入要比较的文件\n"); printf("请输入第一个文件:"); char wj1[100] = { "\0" }; gets(wj1); fp1 = fopen(wj1, "r"); n1=open_and_computehang(fp1); fp1 = fopen(wj1, "r"); printf("请输入第二个文件:"); char wj2[100] = { "\0" }; gets(wj2); fp2 = fopen(wj2, "r"); n2 = open_and_computehang(fp2); fp2 = fopen(wj2, "r"); compare(fp1,fp2, n1, n2); }

9.9  编写将指定文件的m行到n行的每一行写到显示屏上的程序,m和n的值由键盘输入。

#include<stdio.h> void main() { printf("编写将指定文件的m行到n行的每一行写到显示屏上的程序,m和n的值由键盘输入。\n\n"); FILE *fp1, *fp2; char c1[50] = { '\0' }, c2[50] = { '\0' }; printf("\n请输入第一个文件:"); gets(c1); fp1 = fopen(c1,"at+"); if (fp1==NULL) { printf("\n文件打开失败!"); } printf("\n请输入第二个文件:"); gets(c2); fp2 = fopen(c2, "r"); if (fp2 == NULL) { printf("\n文件打开失败!"); } char c; if ((c=getc(fp2))!=EOF) { putc('\n', fp1); } while ((c=getc(fp2))!=EOF) { putc(c,fp1); } getch(); fclose(fp1); fclose(fp2); }

 

9.10 编写将指定文件的m行到n行的每一行写到显示屏上的程序,m和n的值由键盘输入。 #include<stdio.h> #define size 80 void main() { printf("编写将指定文件的m行到n行的每一行写到显示屏上的程序,m和n的值由键盘输入。\n\n"); char wj[40]; printf("请输入文件名:"); gets(wj); FILE *fp = fopen(wj,"r"); if (fp==NULL) { printf("文件打开失败!"); return; } char c; int sum = 1,m=0,n=0; while ((c=getc(fp))!=EOF) { if (c=='\n') { sum = sum + 1; } } fclose(fp); fp = fopen(wj, "r"); printf("\n该文件的总行数为:%d",sum); printf("\n请输入该文件中行的上限:"); scanf("%d",&m); printf("\n请输入该文件中行的下限:"); scanf("%d", &n); char line[size] = {'\0'}; int temp = 1; while (fgets(line,size,fp)!=EOF) { if (m==temp) { puts(line); temp = temp + 1; while (fgets(line, size, fp)) { puts(line); if (n==temp) { break; } temp = temp + 1; } break; } temp = temp + 1; } getch(); }

9.11  编写一个能在终端显示一个文件的内容的程序,要求一次显示20行,在20行的结尾,程序等待从键盘键入一个字符。如果该字符为q,则程序将停止显示文件内容;如果是其他字符,则显示该文件的下20行内容。

#include<stdio.h> #define size 80 void main() { printf("编写一个能在终端显示一个文件的内容的程序,要求一次显示20行,在20行的结尾,程序等待从键盘键入一个字符。\n如果该字符为q,则程序将停止显示文件内容;如果是其他字符,则显示该文件的下20行内容。\n\n"); char wj[40]; printf("请输入文件名:"); gets(wj); FILE *fp = fopen(wj, "r"); if (fp == NULL) { printf("文件打开失败!"); return; } char c; int sum = 1, m = 0, n = 0; while ((c = getc(fp)) != EOF) { if (c == '\n') { sum = sum + 1; } } fclose(fp); fp = fopen(wj, "r"); char line[size] = { '\0' };//存储每行的字符串 int temp = 1;//统计行数 while (fgets(line, size, fp) != EOF) { puts(line); if (temp % 20 == 0) { printf("\n请输入一个字符(若为q,则程序结束,其他字符程序将继续输出):"); scanf("%c", &c); if (c == 'q') { break; } } if (temp==sum) { break; } temp = temp + 1; } getch(); }

                                                                                业精于勤荒于嬉,行成于思毁于随。撸代码的你,好好加油吧!

转载请注明原文地址: https://www.6miu.com/read-72553.html

最新回复(0)