繁体   English   中英

我需要制作一个程序来打印单词中的字符,以了解它的使用频率

[英]I need to make a program that will print characters in a word on how frequent it is used

我需要制作一个程序,在一个单词中打印字符,以了解它的使用频率。 唯一字符将按递增顺序打印(忽略空格),如果有联系,则 ascii 值较低的字符将首先打印。

例如,如果输入是 hello world,字母“h”、“e”、“w”、“r”和“d”仅使用一次,字符“o”使用两次,字符“l”使用三次。 由于 h,e,w,r,d 是平局,我们应该将其排序为 d,e,h,r,w。 然后 next 将是 o,因为它被使用了两次,然后 last 是 l。 因此,如果输入是 hello world,则 output 必须是 dehrwol。 在我当前的程序中,问题是当有关系时,它不会按字母顺序排序,所以 output 是 hewrdol 而不是 dehrwol。

这是我写的代码

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int times[256];

int cmpLetters(const void* a, const void* b)

{

return (times[*(char*)a] > times[*(char*)b]) - (times[*(char*)a] < times[*(char*)b]);

}

int main()

{

char letters[256];

int i, j, k, lnum, t;

char s[1000];


fgets(s, sizeof(s), stdin);

// Init occurrences as 0

memset(times, 0, sizeof(times));

for (i = lnum = 0; s[i] != '\0'; i++)

if (times[s[i]]++ == 0)

letters[lnum++] = s[i];

// Sort letters by number of occurrences

qsort(letters, lnum, sizeof(char), cmpLetters);

char* new = malloc(sizeof(char) * (i + 1));

for (j = k = 0; j < lnum; j++)

for (i = 0; i < times[letters[j]]; i++)

new[k++] = letters[j];

// new[k] = '\0';

for (i = 0; i<lnum; i++)

{

if(letters[i] != '\n' && letters[i] !=' ')

printf("%c",letters[i]);

}

printf("\n\n");



return 0;

}

在这个for循环中

for (i = lnum = 0; s[i] != '\0'; i++)

if (times[s[i]]++ == 0)

letters[lnum++] = s[i];

你不是在检查s[i]是否代表一个字母。

比较 function

int cmpLetters(const void* a, const void* b)

{

return (times[*(char*)a] > times[*(char*)b]) - (times[*(char*)a] < times[*(char*)b]);

}

只比较字符而不比较它们的频率。

这段代码片段

char* new = malloc(sizeof(char) * (i + 1));

for (j = k = 0; j < lnum; j++)

for (i = 0; i < times[letters[j]]; i++)

new[k++] = letters[j];

没有意义,因为数组 new 没有在程序中进一步使用。 它只会产生 memory 泄漏。

如果引入一个包含两个存储字母及其频率的数据成员的结构,程序会更简单。

这是一个演示程序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct Pair
{
    char c;
    size_t n;
};

int cmp( const void *a, const void *b )
{
    const struct Pair *p1 = a;
    const struct Pair *p2 = b;

    int result = ( p2->n < p1->n ) - ( p1->n < p2->n );

    if (result == 0)
    {
        result = ( p2->c < p1->c ) - ( p1->c < p2->c );
    }

    return result;
}

int main( void )
{
    enum { N = 1000} ;
    char s[N];

    fgets( s, sizeof( s ), stdin );

    size_t n = 0;

    for (size_t i = 0; s[i] != '\0'; ++i)
    {
        if (isalpha( ( unsigned char )s[i] ))
        {
            size_t j = 0;

            while (j != i && s[j] != s[i]) ++j;

            n += j == i;
        }
    }

    if (n != 0)
    {
        struct Pair pairs[n];
        memset( pairs, 0, n * sizeof( struct Pair ) );

        for (size_t i = 0, m = 0; s[i] != '\0'; i++)
        {
            if (isalpha( ( unsigned char )s[i] ))
            {
                size_t j = 0;

                while (j != m && pairs[j].c  != s[i]) ++j;

                if (j == m)
                {
                    pairs[m].c = s[i];
                    ++pairs[m].n;
                    ++m;
                }
                else
                {
                    ++pairs[j].n;
                }
            }
        }

        qsort( pairs, n, sizeof( *pairs ), cmp );

        for (size_t i = 0; i < n; i++)
        {
            putchar( pairs[i].c );
        }
        putchar( '\n' );
    }
}

程序 output 可能看起来像

hello world
dehrwol

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM