C语言 对名称列表中的元素进行计数时出现问题

xxslljrj  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(127)

我做了一个程序,你输入几个字符(最多10个)。它使你一个列表,计算姓氏的平均长度,告诉多少不同的名字。但问题是,当我输入最后一个数字(10)-它排序我它不正确(像1,10,2,3,4,5,6,7,8,9)。下面我将提出我的代码。

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

struct people {
        char num[2];
        char surname[20];
        char name[10];
    } peoples[10], c;
    
int main()
{
    int i, j, k = 0, l = 0, m = 0, n = 0;
    float  s = 0;
    char str[100];
    system("chcp 1251 > nul");
    
    for (i = 0, j = 0; i < 10; i++, j++)
    {
        printf("Enter number, surname, name %d of your human: ", i + 1);
        gets(str);
        
        while (str[n] != '\n')
        {
            
            if (str[n] != ' ')
            {
                peoples[j].num[k] = str[n];
            }
            else
                break;
                
            n++;
            k++;
        }
        n++;
        k = 0;
        while (str[n] != '\n')
        {
            
            if (str[n] != ' ')
            {
                peoples[j].surname[k] = str[n];
            }
            else
                break;
                
            n++;
            k++;
        }
        n++;
        k = 0;
        while (str[n] != '\n')
        {
            
            if (str[n] != '\0')
            {
                peoples[j].name[k] = str[n];
            }
            else
                break;
                
            n++;
            k++;
        }
        n = 0;
        k = 0;
    }
    
    for (i = 0; i < 10; i++)
    {
        for (j = i + 1; j < 10; j++)
        {
            if (!strcmp(peoples[i].name, peoples[j].name))
                m = 1;
        }
        if (m == 0)
            l++;
        
        m = 0;
        
        s = s + strlen(peoples[i].surname);
    }
    
    for (i = 0; i < 9; i++)
    
        for (j = 0; j < 9; j++)
        
            if (strcmp(peoples[j].num, peoples[j+1].num) > 0)
            {
                c = peoples[j+1];
                peoples[j+1] = peoples[j];
                peoples[j] = c;
            }
        
    for (i = 0; i < 10; i++)
    {
        printf("%s ", peoples[i].num);
        printf("%s ", peoples[i].name);
        printf("%s ", peoples[i].surname);
        printf("\n");
    }
    
    printf("\nYou have %d different names\n", l);
    printf("Avarege lenght of surname is = %f\n", s / 10);
}
deyfvvtc

deyfvvtc1#

如果你想给予数字输入,那么 * 使用 * 实际的数字数据。
num字段更改为int,而不是单字符字符串:

struct people {
    int num;
    char surname[20];
    char name[10];
};

使用fgets读取以下行:

fgets(str, sizeof str, stdin);

[错误检查作为练习留给读者]
然后使用例如sscanf解析字符串:

sscanf(str, "%d %s %s", &peoples[j].num, &peoples[j].name, &peoples[j].name);

[错误检查作为练习留给读者]
最后,不要自己进行排序,而是使用标准的qsort函数:

qsort(peoples, 10, sizeof(struct people), &compare_people_num);

比较函数如下所示:

int compare_people_num(const void *a, const void *b)
{
    const struct people *p1 = a;
    const struct people *p2 = b:

    return p1->num - p2->num;  // Change order to reverse sort
}

使用sscanfqsort将使您的代码 * 更加 * 简单和易于理解。

相关问题