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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
*/

char convert(int n);
void damages(int arr[][30], int row, int col);

int main(void)
{
? ? FILE *fp;
? ? int i,j;
? ? int num[20][30];
? ? char str[20][31];
? ? char file_name[MAX];
? ? char ch;
? ??
? ? //scanf("%s",file_name);
? ? if ((fp = fopen("13.11-12.txt", "r")) == NULL)
? ? {
? ? ? ? fprintf(stdout,"Can't open \"wordy\" file.\n");
? ? ? ? exit(EXIT_FAILURE);
? ? }
? ? 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);
?? ?damages(num,20,30);
?? ?for(i=0;i<20;i++)
?? ?{?? ?
?? ??? ?str[i][30] = '\0';
?? ??? ?for(j=0;j<30;j++)
?? ??? ??? ?str[i][j] = convert(num[i][j]);?? ??? ?
?? ?}
?? ?for(i=0;i<20;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(' ');
?? ?}
}
void damages(int arr[][30], int row, int col)
{
?? ?int i,j;
?? ?int left,right,up,down;
?? ?int a,b,c,d;
?? ?
?? ?for(i=0;i<row;i++)
?? ?{
?? ??? ?for(j=0;j<col;j++)
?? ??? ?{
?? ??? ??? ?if(j>0)
?? ??? ??? ??? ?left=arr[i][j-1];
?? ??? ??? ?else
?? ??? ??? ??? ?left=arr[i][j];
?? ??? ??? ?if(j<col-1)
?? ??? ??? ??? ?right=arr[i][j+1];
?? ??? ??? ?else
?? ??? ??? ??? ?right=arr[i][j];
?? ??? ??? ?if(i>0)
?? ??? ??? ??? ?up=arr[i-1][j];
?? ??? ??? ?else
?? ??? ??? ??? ?up=arr[i][j];
?? ??? ??? ?if(i<row-1)
?? ??? ??? ??? ?down=arr[i+1][j];
?? ??? ??? ?else
?? ??? ??? ??? ?down=arr[i][j];
?? ??? ??? ?a = abs(arr[i][j]-left);
?? ??? ??? ?b = abs(arr[i][j]-right);
?? ??? ??? ?c = abs(arr[i][j]-up);
?? ??? ??? ?d = abs(arr[i][j]-down);
?? ??? ??? ?if(a>1&&b>1&&c>1&&d>1){
?? ??? ??? ??? ?arr[i][j] = (int)((left+right+up+down)/4+0.5);?? ?
?? ??? ??? ??? ?printf("a=%d,b=%d,c=%d,d=%d,left=%d,right=%d,up=%d,down=%dchange[%d][%d]=%d\n",a,b,c,d,left,right,up,down,i,j,arr[i][j]);}
?? ??? ?}
?? ?}
}
?

文章来源:https://blog.csdn.net/apple_50569014/article/details/135775077
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。