C Primer Plus(第六版)第十三章 编程练习答案
Posted 水番正文
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C Primer Plus(第六版)第十三章 编程练习答案相关的知识,希望对你有一定的参考价值。
这章打了两天,也不知道是算久还是不久,当然不是一天写到底,这次感觉打开了程序与其他文件的路口,本来对fopen一知半解,还有关于文件该如何操作,但这章让我了解挺多!--9.28
CH13 Code answer 1:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
int main(void)
{
int ch;
FILE * fp;
unsigned long count = 0;
char name[SIZE];
printf("Please enter the file name:");
scanf("%s", name);
if ( (fp = fopen(name, "r")) == NULL )
{
printf("Can't open %s file\\n", name);
exit(EXIT_FAILURE);
}
while ( (ch = getc(fp)) != EOF )
{
putc(ch, stdout);
++count;
}
fclose(fp);
printf("\\nFile %s has %lu characters.", name, count);
return 0;
}
CH13 Code answer 2:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
#define BUFSIZE 4096 //目前对这个缓冲区的认识是一次读取多个字节会方便点?
void Copy(FILE *, FILE *);
int main(int argc, char * argv[])
{
FILE * fp1;
FILE * fp2;
char ch;
if(argc == 3)
{
if ( (fp1 = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "Can's open %s file\\n", argv[1]);
exit(EXIT_FAILURE);
}
if ( setvbuf(fp1, NULL, _IOFBF, BUFSIZE) != 0 ) //setvbuf失败时返回0 刷新满时创建
{
fputs("Can's create output buffer\\n", stderr);
exit(EXIT_FAILURE);
}
if ( (fp2 = fopen(argv[2], "w+")) == NULL ) //直接创建一个新的文件夹
{
fprintf(stderr, "Can't open %s file\\n", argv[2]);
exit(EXIT_FAILURE);
}
if( setvbuf(fp2, NULL, _IOFBF, BUFSIZE) != 0 )
{
fputs("Can't create input buffer\\n", stderr);
exit(EXIT_FAILURE);
}
}
else
{
fprintf(stderr, "Please enter source file and aim file\\n");
exit(EXIT_FAILURE);
}
// printf("%d",bytes); //跳出循环就是0了
// rewind(fp1);
// while( (ch = getc(fp1)) != EOF )
// putchar(ch);
// rewind(fp2);
// while( (ch = getc(fp2)) != EOF )
// putchar(ch);
Copy(fp1, fp2);
fclose(fp1);
fclose(fp2);
printf("Done!");
return 0;
}
void Copy(FILE * source, FILE * dest)
{
size_t bytes;
static char temp[BUFSIZE];
//下面这个while忘加括号导致没有赋值到bytes 导致下面没有读到fp2
while( (bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0 ) //目前理解是将自己的缓冲区全部复制过去
fwrite(temp, sizeof(char), bytes, dest);
}
CH13 Code answer 3:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define SIZE 20
#define NUM 100
int main(void)
{
char name[SIZE];
FILE * fp;
char str[NUM];
int i = 0;
char ch;
printf("Please enter to upper file name:");
scanf("%s",name);
if( (fp = fopen(name, "r+")) == NULL )
{
fprintf(stderr, "Can's open %s file", name);
exit(EXIT_FAILURE);
}
while( (ch = getc(fp)) != EOF)
str[i++] = ch;
for(i = 0; i < strlen(str); i++)
{
fseek(fp, i, SEEK_SET); //逐字匹配
if(isalpha(str[i]))
{
str[i] = toupper(str[i]);
char * ch = str + i;
fwrite(ch, sizeof(char), 1, fp); //第一个必须是指针
}
}
fclose(fp);
printf("Done!");
return 0;
}
CH13 Code answer 4:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
FILE * fp;
int i;
char ch;
if(argc == 1)
{
fprintf(stderr, "Please enter file name");
exit(EXIT_FAILURE);
}
for (i = 1; i < argc; i++) //去掉执行文件的1
{
if ( (fp = fopen(argv[i], "r")) == NULL)
{
fprintf(stderr, "Can's open %s file", argv[i]);
exit(EXIT_FAILURE);
}
printf("This content of file %s is:\\n", argv[i]);
while( (ch = getc(fp)) != EOF)
putchar(ch);
printf("\\n");
fclose(fp);
}
printf("Done!");
return 0;
}
CH13 Code answer 5:
/* append.c -- appends files to a file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 4096
#define SLEN 81
void append(FILE *source, FILE *dest);
char * s_gets(char * st, int n);
int main(int argc, char * argv[])
{
FILE *fa, *fs; // fa for append file, fs for source file
int files = 0; // number of files appended
int ch;
int i;
if (argc == 1)
{
fprintf(stderr, "Please enter file name");
exit(EXIT_FAILURE);
}
if ((fa = fopen(argv[1], "a+")) == NULL) //创建目标文件
{
fprintf(stderr, "Can't open %s\\n", argv[1]);
exit(EXIT_FAILURE);
}
if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create output buffer\\n", stderr);
exit(EXIT_FAILURE);
}
for(i = 2; i < argc; i++)
{
if (strcmp(argv[1], argv[i]) == 0) //比较目标文件名和要附加的文件名
fputs("Can't append file to itself\\n",stderr);
else if ((fs = fopen(argv[i], "r")) == NULL)
fprintf(stderr, "Can't open %s\\n", argv[i]);
else
{
if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create input buffer\\n",stderr);
continue;
}
append(fs, fa);
if (ferror(fs) != 0)
fprintf(stderr,"Error in reading file %s.\\n",
argv[i]);
if (ferror(fa) != 0)
fprintf(stderr,"Error in writing file %s.\\n",
argv[1]);
fclose(fs);
files++;
printf("File %s appended.\\n", argv[i]);
}
}
printf("Done appending. %d files appended.\\n", files);
rewind(fa);
printf("%s contents:\\n", argv[1]);
while ((ch = getc(fa)) != EOF)
putchar(ch);
puts("\\nDone displaying.");
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE]; // allocate once
while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)
fwrite(temp, sizeof (char), bytes, dest);
}
CH13 Code answer 6:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
#define BUFSIZE 4096 //目前对这个缓冲区的认识是一次读取多个字节会方便点?
void Copy(FILE *, FILE *);
char * s_gets(char *, int);
int main(void)
{
FILE * faim;
FILE * fsrc;
char ch;
char src[SIZE];
char aim[SIZE];
puts("Enter name of destination file:");
s_gets(aim, SIZE);
if ( (faim = fopen(aim, "r")) == NULL)
{
fprintf(stderr, "Can's open %s file\\n", aim);
exit(EXIT_FAILURE);
}
if ( setvbuf(faim, NULL, _IOFBF, BUFSIZE) != 0 ) //setvbuf失败时返回0 刷新满时创建
{
fputs("Can's create output buffer\\n", stderr);
exit(EXIT_FAILURE);
}
puts("Enter name of copy file:");
s_gets(src, SIZE);
if ( strcmp(aim, src) == 0)
fputs("Can't append file to it self\\n", stderr);
else if ( (fsrc = fopen(src, "w+")) == NULL )
{
fprintf(stderr, "Can't open %s file\\n", src);
exit(EXIT_FAILURE);
}
else
{
if( setvbuf(fsrc, NULL, _IOFBF, BUFSIZE) != 0 )
{
fputs("Can't create input buffer\\n", stderr);
exit(EXIT_FAILURE);
}
Copy(faim, fsrc);
}
printf("Done copy.!\\n");
rewind(fsrc);
printf("%s content:\\n", src);
while ( (ch = getc(fsrc)) != EOF )
putchar(ch);
puts("\\nDone displaying!");
fclose(faim);
fclose(fsrc);
return 0;
}
void Copy(FILE * source, FILE * dest)
{
size_t bytes;
static char temp[BUFSIZE];
while ( (bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0 )
fwrite(temp, sizeof(char), bytes, dest);
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if(ret_val)
{
find = strchr(st, '\\n');
if (find)
*find = '\\0';
else
while (getchar() != '\\n')
continue;
}
return ret_val;
}
CH13 Code answer 7:
A
//想了半天想不到好的办法 有个方法应该是读取所有数据 然后计算大小 然后输出 但总感觉这个方法有点笨
//是想用递归解决 但递归是用空间换时间的方法 我先试试 递归成了! 感谢报错让我的代码更精简
//这种题目 最让我纠结的问题就是什么时候该停下 就像循环的初始 次数 终止三条一开始就该想好的地方
//于是这题的循环次数让我无法确定 又是双线程 递归感觉是比较好的一个方法
#include <stdio.h>
#include <stdlib.h>
#define SIZE 256 //一行最大的大小为256
char ** Printx(FILE *, FILE *, char * ret_val[2]);
char * fs_get(char *, int, FILE *);
int main(int argc, char * argv[])
{
FILE * fp1;
FILE * fp2;
char * ret_val[2];
if(argc != 3)
{
fprintf(stderr, "Please enter two files!");
exit(EXIT_FAILURE);
}
else
{
if ( (fp1 = fopen(argv[1], "r")) == NULL )
{
fprintf(stderr, "Can't open %s file!!", argv[1]);
exit(EXIT_FAILURE);
}
if ( (fp2 = fopen(argv[2], "r")) == NULL )
{
fprintf(stderr, "Can't open %s file!!", argv[2]);
exit(EXIT_FAILURE);
}
}
Printx(fp1, fp2, ret_val);
puts("Done!");
return 0;
}
char * fs_gets(char * st, int n, FILE * fp)
{
char * ret_vall;
char * find;
ret_vall = fgets(st, n, fp);
if(ret_vall)
{
find = strchr(st, '\\n');
if (find)
*find = '\\0';
}
return ret_vall;
}
//突然产生一个对题目的思考
//指的一次打印行我要不要处理换行符
char ** Printx(FILE * fp1, FILE * fp2, char * ret_val[2])
{
static char temp[SIZE];
if ((ret_val[0] = fs_gets(temp, SIZE, fp1)) != NULL)
{
fputs(temp, stdout);
printf("\\n");
}
if ((ret_val[1] = fs_gets(temp, SIZE, fp2)) != NULL)
{
fputs(temp, stdout);
printf("\\n");
}
if (ret_val[0] == NULL && ret_val[1] == NULL)
return ret_val; //当两个都是NULL停止递归
Printx(fp1, fp2, ret_val);
}
B
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256 //一行最大的大小为256
char ** Printx(FILE *, FILE *, char * ret_val[2]);
char * fs_get(char *, int, FILE *);
int main(int argc, char * argv[])
{
FILE * fp1;
FILE * fp2;
char * ret_val[2];
if(argc != 3)
{
fprintf(stderr, "Please enter two files!");
exit(EXIT_FAILURE);
}
else
{
if ( (fp1 = fopen(argv[1], "r")) == NULL )
{
fprintf(stderr, "Can't open %s file!!", argv[1]);
exit(EXIT_FAILURE);
}
if ( (fp2 = fopen(argv[2], "r")) == NULL )
{
fprintf(stderr, "Can't open %s file!!", argv[2]);
exit(EXIT_FAILURE);
}
}
Printx(fp1, fp2, ret_val);
puts("\\nDone!");
return 0;
}
char * fs_gets(char * st, int n, FILE * fp)
{
char * ret_vall;
char * find;
ret_vall = fgets(st, n, fp);
if(ret_vall)
{
find = strchr(st, '\\n');
if (find)
*find = '\\0';
}
return ret_vall;
}
//突然产生一个对题目的思考
//指的一次打印行我要不要处理换行符 但这题目b理想效果应该是一行输入两个文件各一行并称一行
char ** Printx(FILE * fp1, FILE * fp2, char * ret_val[2])
{
static char temp[SIZE];
if ((ret_val[0] = fs_gets(temp, SIZE, fp1)) != NULL)
fputs(temp, stdout);
if ((ret_val[1] = fs_gets(temp, SIZE, fp2)) != NULL)
fputs(temp, stdout);
if (ret_val[0] == NULL && ret_val[1] == NULL)
return ret_val; //当两个都是NULL停止递归
printf("\\n");
Printx(fp1, fp2, ret_val);
}
CH13 Code answer 8:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
int main(int argc, char * argv[])
{
FILE * fs;
int files = 0;
char ch;
int count = 0;
int i;
if(argc == 2)
{
puts("Enter a string,I will tell you how many characters appear.(empty line to quit)");
while ((ch = getchar()) != '\\n')
if (*argv[1] == ch) //记得他是个指针类型 要解引用
++count;
printf("Appear %d times", count);
}
else if(argc > 2)
{
for (i = 2; i < argc; i++, count = 0) //重置count
{
if ( (fs = fopen(argv[i], "r")) == NULL )
fprintf(stderr, "Can't open %s \\n", argv[i]);
else
{
while ((ch = getc(fs)) != EOF)
if (*argv[1] == ch)
++count;
if (ferror(fs) != 0)
fprintf(stderr, "Error in reading file %s.\\n", argv[i]);
fclose(fs);
files++;
printf("This '%c' appears %d times in the file %s.\\n", *argv[1], count, argv[i]);
}
}
printf("Done! %d files end of reading", files);
}
else
{
fprintf(stderr, "Enter the character or file or all!");
exit(EXIT_FAILURE);
}
return 0;
}
CH13 Code answer 9:
#include <stdio.h>
#include <stdlib.h>
#define MAX 41
int main(void)
{
FILE * fp;
char words[MAX];
int n = 1;
if ( (fp = fopen("wordy.txt", "a+")) == NULL)
{
fprintf(stderr, "Can't open wordy.txt file\\n");
exit(EXIT_FAILURE);
}
while ( fgets(words, MAX, fp) != NULL )
++n;
puts("Enter words to add to the file; press the #");
while( (fscanf(stdin, "%40s", words) == 1) && (words[0] != '#') )
fprintf(fp, "%d %s\\n", n++, words);
rewind(fp);
while ( fgets(words, MAX, fp) != NULL )
printf("%s", words);
puts("Done!");
if (fclose(fp) != 0)
fprintf(stderr, "Error closing file\\n");
return 0;
}
CH13 Code answer 10:
#include <stdio.h>
#include <stdlib.h>
#define MAX 81
int main(void)
{
FILE * fp;
char ch;
char name[MAX];
long n;
int ret;
puts("Enter your wish open name of file!");
scanf("%s", name);
if ( (fp = fopen(name, "r")) == NULL )
{
fprintf(stderr, "Can't open %s file", name);
exit(EXIT_FAILURE);
}
puts("Enter the localtion:(enter other to quit)");
while ( scanf("%ld", &n) == 1)
{
fseek(fp, n, SEEK_SET);
while( (ch = getc(fp)) != '\\n' )
putchar(ch);
puts("\\nEnter the next localtion:(enter other to quit)");
}
puts("Done!");
fclose(fp);
return 0;
}
CH13 Code answer 11:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
int main(int argc, char * argv[])
{
FILE * fp;
char temp[MAX];
char st[MAX];
if ( argc != 3 )
{
fprintf(stderr, "Enter string and file name!");
exit(EXIT_FAILURE);
}
else
{
if ( (fp = fopen(argv[2], "r")) == NULL )
{
fprintf(stderr, "Can't open %s file!", argv[2]);
exit(EXIT_FAILURE);
}
while( fgets(temp, MAX, fp) )
if ( strstr(temp, argv[1]) )
puts(temp);
}
puts("Done!");
fclose(fp);
return 0;
}
CH13 Code answer 12:
12 13 14源文件
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 5 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 1 9 8 5 4 5 2 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 0 4 5 2 0 0 0 0 0 0 0 0
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 4 5 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 1 8 5 0 0 0 4 5 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 4 5 2 0 0 0 0 0
5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 3 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
0 0 0 0 2 2 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
0 0 0 0 3 3 0 0 0 0 0 0 5 8 9 9 8 5 0 5 6 1 1 1 1 6 5 0 0 0
0 0 0 0 4 4 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
0 0 0 0 5 5 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
//题目说是文件中的内容读入一个int型数据 但这样一般都是fread二进制流?? 但我们创建的文件内容都是文本
//于是写个文本转数字的函数好了
#include <stdio.h>
#include <stdlib.h>
#define LMAX 256
#define NMAX 81
#define ROWS 20
#define COLS 30
void Copy(FILE *, FILE *);
void St2Int(FILE *, int src[ROWS][COLS]);
void AirShip(int src[ROWS][COLS], char aim[ROWS][COLS + 1]);
int main(void)
{
int src[ROWS][COLS] = {0};
char aim[ROWS][COLS + 1] = {0};
int i, j;
FILE * fp;
char name[NMAX];
puts("Enter file name:");
scanf("%s", name);
if ( (fp = fopen(name, "r+")) == NULL )
{
fprintf(stderr ,"Can't open %s file!", name);
exit(EXIT_FAILURE);
}
St2Int(fp, src);
// fwrite(src[x], sizeof(int), 3, fp); 这样是二进制文件所以只能字符转int的函数来一个
// rewind(fp);
// fread(src[x], sizeof(int), 3, fp);
// printf("%d %d %d", src[x][0], src[x][1], src[x][2]);
AirShip(src, aim);
puts("Done!");
fclose(fp);
return 0;
}
void St2Int(FILE * fp, int src[ROWS][COLS])
{
int i, j;
char ch;
for (i = 0; i < ROWS; i++)
{
for(j = 0; j < COLS; )
{
ch = getc(fp);
// printf("%d ", ch);
if (ch != ' ' && ch != '\\n')
{
src[i][j] = ch - 48;
// printf("%d ", src[i][j]);
++j;
}
}
// printf("\\n");
}
}
void AirShip(int src[ROWS][COLS], char aim[ROWS][COLS + 1])
{
int i, j;
for( i = 0; i < ROWS; i++)
{
for( j = 0; j < COLS; j++)
{
switch (src[i][j])
{
case 0:
aim[i][j] = ' ';
break;
case 1:
aim[i][j] = '.';
break;
case 2:
aim[i][j] = 39;
break;
case 3:
aim[i][j] = ':';
break;
case 4:
aim[i][j] = '~';
break;
case 5:
aim[i][j] = '*';
break;
case 6:
aim[i][j] = '=';
break;
case 8:
aim[i][j] = '%';
break;
case 9:
aim[i][j] = '#';
break;
}
}
}
for( i = 0; i < ROWS; i++)
{
for( j = 0; j < COLS; j++)
putchar(aim[i][j]);
printf("\\n");
}
}
CH13 Code answer 13:
//题目说是文件中的内容读入一个int型数据 但这样一般都是fread二进制流?? 但我们创建的文件内容都是文本
//于是写个文本转数字的函数好了
#include <stdio.h>
#include <stdlib.h>
#define LMAX 256
#define NMAX 81
#define ROWS 20
#define COLS 30
void Copy(FILE *, FILE *);
void St2Int(int rows, int cols, FILE *, int src[rows][cols]);
void AirShip(int rows, int cols, int src[rows][cols], char aim[rows][cols + 1]);
int main(void)
{
int src[ROWS][COLS] = {0};
char aim[ROWS][COLS + 1] = {0};
int i, j;
FILE * fp;
char name[NMAX];
puts("Enter file name:");
scanf("%s", name);
if ( (fp = fopen(name, "r+")) == NULL )
{
fprintf(stderr ,"Can't open %s file!", name);
exit(EXIT_FAILURE);
}
St2Int(ROWS, COLS, fp, src);
AirShip(ROWS, COLS, src, aim);
puts("Done!");
fclose(fp);
return 0;
}
void St2Int(int rows, int cols, FILE * fp, int src[rows][cols])
{
int i, j;
char ch;
for (i = 0; i < rows; i++)
{
for(j = 0; j < cols; )
{
ch = getc(fp);
// printf("%d ", ch);
if (ch != ' ' && ch != '\\n')
{
src[i][j] = ch - 48;
// printf("%d ", src[i][j]);
++j;
}
}
// printf("\\n");
}
}
void AirShip(int rows, int cols, int src[rows][cols], char aim[rows][cols + 1])
{
int i, j;
for( i = 0; i < rows; i++)
{
for( j = 0; j < cols; j++)
{
switch (src[i][j])
{
case 0:
aim[i][j] = ' ';
break;
case 1:
aim[i][j] = '.';
break;
case 2:
aim[i][j] = 39;
break;
case 3:
aim[i][j] = ':';
break;
case 4:
aim[i][j] = '~';
break;
case 5:
aim[i][j] = '*';
break;
case 6:
aim[i][j] = '=';
break;
case 8:
aim[i][j] = '%';
break;
case 9:
aim[i][j] = '#';
break;
}
}
}
for( i = 0; i < rows; i++)
{
for( j = 0; j < cols; j++)
putchar(aim[i][j]);
printf("\\n");
}
}
CH13 Code answer 14:
//题目说是文件中的内容读入一个int型数据 但这样一般都是fread二进制流?? 但我们创建的文件内容都是文本
//于是写个文本转数字的函数好了
#include <stdio.h>
#include <stdlib.h>
#define LMAX 256
#define NMAX 81
#define ROWS 20
#define COLS 30
void Copy(FILE *, FILE *);
void St2Int(int rows, int cols, FILE *, int src[rows][cols]);
void AirShip(int rows, int cols, int src[rows][cols], char aim[rows][cols + 1]);
void Tort(int rows, int cols, int src[rows][cols]);
int F_F(int *, int);
int main(void)
{
int src[ROWS][COLS] = {0};
char aim[ROWS][COLS + 1] = {0};
int i, j;
FILE * fp;
char name[NMAX];
// int ar[4] = {10, 10, 5, 5}; 测试F_F函数
// printf("%d", F_F(ar, 4));
puts("Enter file name:");
scanf("%s", name);
if ( (fp = fopen(name, "r+")) == NULL )
{
fprintf(stderr ,"Can't open %s file!", name);
exit(EXIT_FAILURE);
}
St2Int(ROWS, COLS, fp, src);
Tort(ROWS, COLS, src);
// for ( i = 0; i < ROWS; i++) 测试tort后的数据
// {
// for ( j = 0; j < COLS; j++)
// printf("%d ", src[i][j]);
// printf("\\n");
// }
AirShip(ROWS, COLS, src, aim);
puts("Done!");
fclose(fp);
return 0;
}
void St2Int(int rows, int cols, FILE * fp, int src[rows][cols])
{
int i, j;
char ch;
for (i = 0; i < rows; i++)
{
for(j = 0; j < cols; )
{
ch = getc(fp);
// printf("%d ", ch);
if (ch != ' ' && ch != '\\n')
{
src[i][j] = ch - 48;
// printf("%d ", src[i][j]);
++j;
}
}
// printf("\\n");
}
}
void AirShip(int rows, int cols, int src[rows][cols], char aim[rows][cols + 1])
{
int i, j;
for( i = 0; i < rows; i++)
{
for( j = 0; j < cols; j++)
{
switch (src[i][j])
{
case 0:
aim[i][j] = ' ';
break;
case 1:
aim[i][j] = '.';
break;
case 2:
aim[i][j] = 39;
break;
case 3:
aim[i][j] = ':';
break;
case 4:
aim[i][j] = '~';
break;
case 5:
aim[i][j] = '*';
break;
case 6:
aim[i][j] = '=';
break;
case 8:
aim[i][j] = '%';
break;
case 9:
aim[i][j] = '#';
break;
}
}
}
for( i = 0; i < rows; i++)
{
for( j = 0; j < cols; j++)
putchar(aim[i][j]);
printf("\\n");
}
}
void Tort(int rows, int cols, int src[rows][cols])
{
int ar[4] = {0};
int i, j, z;
int a;
int x, y;
for( i = 0; i < rows; i++)
{
for( j = 0, a = 0; j < cols; j++, a = 0)
{
//存储上下左右四个数 a并计数成功放入的个数
if ( (i - 1) > 0 )
ar[a++] = src[i - 1][j];
if ( (j + 1) < cols )
ar[a++] = src[i][j + 1];
if ( (i + 1) < rows)
ar[a++] = src[i + 1][j];
if ( (j - 1) > 0 )
ar[a++] = src[i][j - 1];
//测试周围的数是否都大于1
// printf("%d", a);
for(z = 0; z < a; z++)
{
// printf(":%d ", ar[z]);
x = src[i][j] - ar[z];
if ( x < 0 )
x = -x;
if ( x <= 1 ) //要与周围的数相差 都 大于1
break;
}
// printf("\\n");
if ( a == z ) //代表相差都大于1
src[i][j] = F_F(ar, a);
}
}
}
int F_F(int * ar, int n)
{
int i;
float x = 0; //千万不要忘了初始化0
int y = 0;
for ( i = 0; i < n; i++)
{
x += (float) ar[i];
y += ar[i];
}
// printf("%d:%d",x/4, y/4);
if ( (x / n - y / n) >= 0.5)
return y / n + 1;
else
return y / n;
}
以上是关于C Primer Plus(第六版)第十三章 编程练习答案的主要内容,如果未能解决你的问题,请参考以下文章