Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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语言中,如何在数组中获得整个ascii表?_C_Arrays_Ascii - Fatal编程技术网

频率分析在c语言中,如何在数组中获得整个ascii表?

频率分析在c语言中,如何在数组中获得整个ascii表?,c,arrays,ascii,C,Arrays,Ascii,我目前正在学校的一个项目中工作,我想为字符串创建一个频率分析,这样我们就可以对它们进行编码或解码。 我需要计算每个字符在字符串中出现的次数,strig是一个随机文件,可以是一本书中的一整页或其他任何内容,因此我们需要ascii表中的所有字符。 现在,我正在努力获得一个数组,里面充满了所有可能出现的字符 我已经决定使用一个结构数组的方向,该数组包含一个char和一个int,在我的头脑中,这两个字符都是有效的,因此char是键,int是特定char在字符串中出现的次数。 我需要将所有256个可能的字

我目前正在学校的一个项目中工作,我想为字符串创建一个频率分析,这样我们就可以对它们进行编码或解码。 我需要计算每个字符在字符串中出现的次数,strig是一个随机文件,可以是一本书中的一整页或其他任何内容,因此我们需要ascii表中的所有字符。 现在,我正在努力获得一个数组,里面充满了所有可能出现的字符

我已经决定使用一个结构数组的方向,该数组包含一个char和一个int,在我的头脑中,这两个字符都是有效的,因此char是键,int是特定char在字符串中出现的次数。 我需要将所有256个可能的字符放入数组中,但无法找到获取前32个字符的方法

  for (char c = NULL; c <= 'z' ; c++){
    ftabel[i].value = c;
    i++;
  }

for(char c=NULL;c这实际上几乎是微不足道的,因为
char
值本身就是您需要的全部索引:

#include <limits.h>

// static, so it's initialized to zero
unsigned long long charCounts[ UCHAR_MAX + 1 ];

// Note the use of **unsigned** char
// `unsigned char` ensures any `char` value will
// not cause an improper access to charCounts[]
void countChars( const unsigned char *str )
{
    while ( *str )
    {
        ( charCounts[ *str ] )++;
        str++;
    }
}

int main( int argc, char **argv )
{
    .
    .
    .

是一种7位编码方案,这意味着所有编码字符(可打印和不可打印)的值都在
0
127
(包括)的范围内。创建
128
元素数组应该没有那么难…;)还要注意
NULL
是一个空指针,而不是“空值”。如果要从零开始循环,请使用零(即
0
)。
'z'
不是ASCII表的最后一个元素,只查找字母或表的所有元素?请注意ASCII集中没有的字符,给定负值。@对于B,在本例中,256个不同字符和前256个不同整数之间没有差异。一个字符就是一个整数。e、 g.字符文字“A”与ASCII中的整数65相同。您可以从整数0迭代到255
for(inti=0;ci255;i++){ftabel[i].value=i;}
hats是一个更好的解决方案,但是像ESC和≤?<代码>≤
不是ASCII或Extended ASCIIThanks的帮助,我想我对实际练习有点不清楚,我将得到一个内嵌,从中我需要创建一个包含所有字符的频率表。然后我需要将“哈夫曼代码(?)”转换成二进制表示,并创建一个trie,这样我就可以对其进行编码或解码。
Success #stdin #stdout 0s 4560KB
Histogram
 (032)[0x20] : ====================================================== (54)
,(044)[0x2C] : === (3)
-(045)[0x2D] : == (2)
.(046)[0x2E] : ==== (4)
1(049)[0x31] : = (1)
4(052)[0x34] : = (1)
A(065)[0x41] : == (2)
B(066)[0x42] : == (2)
C(067)[0x43] : == (2)
J(074)[0x4A] : = (1)
L(076)[0x4C] : = (1)
N(078)[0x4E] : = (1)
R(082)[0x52] : = (1)
S(083)[0x53] : == (2)
T(084)[0x54] : == (2)
V(086)[0x56] : = (1)
a(097)[0x61] : ======================= (23)
b(098)[0x62] : ===== (5)
c(099)[0x63] : ========== (10)
d(100)[0x64] : ================ (16)
e(101)[0x65] : ================================ (32)
f(102)[0x66] : ===== (5)
g(103)[0x67] : ======== (8)
h(104)[0x68] : =============== (15)
i(105)[0x69] : ============================= (29)
k(107)[0x6B] : == (2)
l(108)[0x6C] : ================ (16)
m(109)[0x6D] : ==== (4)
n(110)[0x6E] : ========================= (25)
o(111)[0x6F] : ======================== (24)
p(112)[0x70] : ===== (5)
r(114)[0x72] : ============== (14)
s(115)[0x73] : ===================== (21)
t(116)[0x74] : =============== (15)
u(117)[0x75] : ====== (6)
v(118)[0x76] : = (1)
w(119)[0x77] : == (2)
y(121)[0x79] : ====== (6)
#include <limits.h>

// static, so it's initialized to zero
unsigned long long charCounts[ UCHAR_MAX + 1 ];

// Note the use of **unsigned** char
// `unsigned char` ensures any `char` value will
// not cause an improper access to charCounts[]
void countChars( const unsigned char *str )
{
    while ( *str )
    {
        ( charCounts[ *str ] )++;
        str++;
    }
}

int main( int argc, char **argv )
{
    .
    .
    .
#include <ctype.h>

void printCounts()
{
    // unsigned char ii here would infinite loop...
    // (yes, pedantically even `unsigned int` might)
    for ( unsigned int ii = 0; ii <= UCHAR_MAX; ii++ )
    {
        // get a printable char value
        unsigned char pp = ii;
        if ( !isprint( ii ) )
        {
            pp = '*';
        }

        printf( "'%c' (%02X): %llu\n", pp, ii, charCounts[ ii ] );
    }
}

    .
    .
    .