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 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]);}
        }
    }
}
 

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值