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 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
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 41
char convert(int n);
int get_row(FILE *fp);
int get_col(FILE *fp);
int main(void)
{
char file_name[MAX];
char ch;
FILE *fp;
int i,j;
int row,col;
scanf("%s",file_name);
if ((fp = fopen(file_name, "r")) == NULL)
{
fprintf(stdout,"Can't open file.\n");
exit(EXIT_FAILURE);
}
row = get_row(fp);
col = get_col(fp);
printf("row=%d,col=%d\n",row,col);
int num[row][col];
char str[row][col+1];
i=0;
j=0;
while((ch = getc(fp))!=EOF)
{
if(ch != '\n'&&ch != ' ')
{
num[i][j] = ch - '0';
j++;
}
else if(ch=='\n')
{
i++;
j=0;
}
}
fclose(fp);
for(i=0;i<row;i++)
{
str[i][col] = '\0';
for(j=0;j<col;j++)
str[i][j] = convert(num[i][j]);
}
for(i=0;i<row;i++)
printf("%s\n",str[i]);
return 0;
}
char convert(int n)
{
switch(n)
{
case 0:return(' ');
case 1:return('.');
case 2:return('\'');
case 3:return(':');
case 4:return('~');
case 5:return('*');
case 6:return('=');
case 7:return('+');
case 8:return('%');
case 9:return('#');
default:return(' ');
}
}
int get_row(FILE *fp)
{
int ch,count=0;
while((ch = getc(fp))!=EOF)
{
if(ch=='\n')
count++;
}
fseek( fp, 0, SEEK_SET );
return count;
}
int get_col(FILE *fp)
{
int ch,count=0;
while((ch = getc(fp))!=EOF)
{
if(ch != ' '&&ch != '\n')
count++;
else if(ch =='\n')
break;
}
fseek( fp, 0, SEEK_SET );
return count;
}