Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
';压缩';C语言中字符数据的二维数组输入_C_Char_Compression_Multidimensional Array - Fatal编程技术网

';压缩';C语言中字符数据的二维数组输入

';压缩';C语言中字符数据的二维数组输入,c,char,compression,multidimensional-array,C,Char,Compression,Multidimensional Array,该程序试图读取2D数组(5x10)中的字符,并对其进行压缩。这样(aaacccfeee->a3c3f1e3)。我的输出有点问题,我不知道问题出在哪里 代码如下: #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define SIZE1 5 #define SIZE2 10 void compress(char data[SIZE1][SIZE2]); int main () { int row, col;

该程序试图读取2D数组(5x10)中的字符,并对其进行压缩。这样(aaacccfeee->a3c3f1e3)。我的输出有点问题,我不知道问题出在哪里

代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#define SIZE1 5 
#define SIZE2 10

void compress(char data[SIZE1][SIZE2]);

int main ()
{
    int row, col;
    char qn2[SIZE1][SIZE2];

    printf("\nEnter your data (5x10 of characters): \n");
    for (row = 0; row < SIZE1; ++row)
        for (col = 0; col < SIZE2; ++col)
            scanf("%c", &qn2[row][col]);
    fflush(stdin);
    compress(qn2);
    return 0;
}

void compress(char data[SIZE1][SIZE2])
{
    int row, col, count = 0;
    char tempChar = data[0][0];

    printf("\nThe compression output: \n");

    for (row = 0; row < SIZE1; row++)
    {
        for (col = 0; col < SIZE2; col++)
        {
            if (data[row][col] == tempChar)
            {
                count ++;
            }
            else
            {
                printf("%c%d", tempChar, count);
                tempChar = data[row][col];
                count = 1;
            }   
        } printf("\n");
    }

}
我得到的输出:

The compression output:
a3c3d1
e3f3g3h1
i3j3k3l1
m3n3o3p1
q3r3s3t1u3
试试这个:

for (row = 0; row < SIZE1; row++)
{
    for (col = 0; col < SIZE2; col++)
    {
        if (data[row][col] == tempChar)
        {
            count ++;
        }
        else
        {
            printf("%c", tempChar);
            printf("%d", count);
            tempChar = data[row][col];
            count = 1;
        }   
    }
}
if(count!=0) //need to print the last sequence of repeated characters.
{
     printf("%c", tempChar);
     printf("%d", count);
}
for(行=0;行
Duh。关于假警报的问题。如果希望在扫描期间使用换行符,则需要在
%c
前面加上前导空格。尝试
%c“
。(同样,sry,那个decl看起来很奇怪,直到我走近它)。但是,
fflush
仍然是非标准的。除了有助于消除空白的scanfThanks@WhozCraig部分之外,代码似乎还可以。但我需要这样显示:每一新行的结果必须显示在新行上。@untendejoy ok。因此,您的
printf
参数需要类似
%c%d”、tempChar、count
的内容,并在
for col
循环的
}
结束后滑动
printf(“\n”)
put(“”)
(转储您剩下的任何字符/计数,顺便说一句)我不确定您想要的输出是什么,但这似乎与你所追求的非常接近。不管怎样,还是玩玩吧。@WhozCraig对不起,我好像弄不懂。我放置了printf(“\n”);在for col循环之后,但我没有得到如上所示的预期输出(我编辑了问题以向您展示预期输出的示例)。@valter即使它是1,您也需要打印该值。那么为什么保留if语句?@valter在这个问题的上下文中并不需要它。但是,如果问题陈述后面有一些更改,那么添加检查总是好的。
for (row = 0; row < SIZE1; row++)
{
    for (col = 0; col < SIZE2; col++)
    {
        if (data[row][col] == tempChar)
        {
            count ++;
        }
        else
        {
            printf("%c", tempChar);
            printf("%d", count);
            tempChar = data[row][col];
            count = 1;
        }   
    }
}
if(count!=0) //need to print the last sequence of repeated characters.
{
     printf("%c", tempChar);
     printf("%d", count);
}