C Prime Plus(第五版)第13张编程练习题12解答

 

/*12.创建一个包含20行,每行30个整数的文本文件。整数在O9之间,用空格分开。该文件是一个图片的数字表示,从09的值代表逐渐增加的灰度。编写一个程序,将文件的内容读入到一个20*30int数组中。一种将这种数字表示转化成图片的粗略方法就是让程序使用数组中的数值来初始化一个20*31的字符阵列。0对应空格字符,l对应句号字符,依此类推,较大的值对应占用空间较多的字符。比如,可以使用#代表9。每行的最后一个字符(第31个)为空字符,这样数组将包含20个字符串。然后程序显示结果图片(即打印这些字符串),并将结果存入一个文本文件中。例如,如果开始的数据为:

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

 

对于一种特定的输出字符的选择,输出是这样的:

 C Prime Plus(第五版)第13张编程练习题12解答


#include<stdio.h>
#include<stdlib.h>
#define LEN 81
//long count=0L;
const char trans[11]={' ','.','\'',':','~','*','=','&','%','#'};
void main()
{
	FILE *fp;
	int data[20][30];
	int data1[20][31]={0};
	char file[LEN];
	int last;
	int count;
	int i;
	int j;
	puts("Enter the name of file to be anlysised:");
	//gets(file);//本程序直接打开目标文件
	while((fp=fopen("D:\\学习\\Cproject\\p+13-12\\11.txt","r"))==NULL)//打开固定位置的文件
	{
		fprintf(stderr,"Can't open the file.\n");
		exit(1);
	}


	//将最终数组data1的最后一列设置为0满足题目要求
	for(i=0;i<20;i++)
		data1[i][30]=0;


	//确定文件大小
	fseek(fp,0L,SEEK_END);
	last=ftell(fp);
	printf("%d\n",last);



	//测试程序片段
	//for(i=0;i<11;i++)
		//putchar(trans[i]);
	//count=0L;
	//while(scanf("%d",&i)==1){
	//fseek(fp,i,SEEK_SET);
	//data3=getc(fp);
	//printf("%d\n",data3);
	//}




	//将文本文档11.txt中的数据提取出来存入20*30的数组中
	for(i=0;i<20;i++){
		//fseek(fp,1L,SEEK_CUR);
		//data[i][0]=getc(fp);
		for(count=i*30,j=0;j<30;j++,count++){
			//for(;count<last;count++)
			fseek(fp,2*count+i,SEEK_SET);
			data[i][j]=getc(fp);
		}
		//last=ftell(fp);
		//printf("%d\n",last);
	}




	//打印出预处理的数组
	for(i=0;i<20;i++)
	{
		for(j=0;j<31;j++)
			putchar(data[i][j]);
		putchar('\n');
	}


	//根据要求对产生的数组进行内容转换
	for(i=0;i<20;i++)
		for(j=0;j<30;j++)
			data1[i][j]=trans[data[i][j]-48];


	//最终输出处理得到的图像
	for(i=0;i<20;i++)
	{
		for(j=0;j<31;j++)
			putchar(data1[i][j]);
		putchar('\n');
	}
	system("Pause");
}  

你可能感兴趣的:(c,Prime,Plus,第13章编程练习12)