按字母顺序对字符串列表进行排序(C) [英] Sorting a list of Strings in Alphabetical order (C)

查看:185
本文介绍了按字母顺序对字符串列表进行排序(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的问题.老师必须随机选择一个学生(从她所拥有的学生中),以获得最终分数的特殊奖励,为此,她将N片从1到N的纸放入袋中,并随机选择一个数字K ;获奖学生是学生名单中的第K个学生.问题在于老师不知道哪个号码对应哪个学生,因为她丢失了包含该信息的论文.她所知道的是:所有学生的姓名,以及他们的编号,从1到N,都是按照字母顺序分配的.

Ok, here is my problem. A teacher has to randomly select a student (from the students she has) to earn a special bonus in the final score and in order to do that she puts N pieces of paper numbered from 1 to N in a bag and randomly select a number K; the award-winning student was the K-th student in the student list. The problem is that the teacher does not know which number corresponds to which student because she lost the paper that contained this information. What she knows: the names of all students, and that, their numbers, from 1 to N, are assigned according to the alphabetical order.

因此,我需要获得一组输入的名称,按字母顺序对它们进行排序,然后提供获得特别奖金的学生的姓名,但我在这样做时遇到了麻烦.我编写的程序对除第一个名字以外的所有名字进行排序.

So I need to get the set of names that is given as input, sort them alphabetically and then provide the name of the student who won the special bonus, but I'm having trouble doing so. The program I wrote orders all the names except the first.

此外,当我使用Code :: Blocks运行项目时,会出现以下警告:

In addition, the following warnings appear when I run the project with Code::Blocks:

  • (第16行)ISO C90禁止数组变量长度为's'[-Wvla]
  • (第13行)ISO C90禁止使用混合声明和代码[-Wpedantic]

请告诉我我在这里做错了什么,是否有一种更好的方法来对名称进行排序而不用指定数量的名称.

Please, tell me what am I doing wrong here and if there's a better way to sort the names without having the specified amount of names.

注意:当N和K等于零时,程序应停止读取输入.

Note: the program should stop reading the input when N and K are equal to zero.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int n, k, i, j=0, aux, numMenorNome;
    char str[]="zzzzzzzzzzzzzzzzzzzz", str2[]="zwyxzzzzzzzzzzzzzzzz";

    do
    {
       scanf("%d%d", &n, &k);
       struct student
       {
           char nome[21]; /*name*/
           char nomes_ordenados[21]; /*array to put the names already sorted*/
       } s[n];

       for (i=0; i<n; i++)
       {
           scanf(" %s", s[i].nome);
       }

       for (i=0; i<n; i++)
       {
           aux = strcmp(str, s[i].nome); /*compares the string that would be the last in the alphabetical order ("zzzzzzzzzzzzzzzzzzzz") with the given names*/
           if(aux>0)
           {
               strcpy(str, s[i].nome); /*it gives me the name that comes first in alphabetical order */
               numMenorNome = i;  /* identification number of the name that was obtained */
           }
           if (i==(n-1))
           {
               strcpy(s[j].nomes_ordenados,str); 
               printf("%s\n", s[j].nomes_ordenados);
               strcpy(str, "zzzzzzzzzzzzzzzzzzzz"); 
               strcpy(s[numMenorNome].nome, str2); 
               j++;
               i=0; /* restarts the loop in order to obtain the second name in alphabetical order, the third name, the fourth name and so on */
               if(j==n)
                    break;
           }
       }
       printf("%s\n\n", s[k-1].nomes_ordenados);

    } while (n!=0&&k!=0);
    return 0;
}

推荐答案

对字符串数组进行排序非常简单.只需使用qsort和现有的比较功能(即strcmp)

Sorting an array of strings are real simple. Just use qsort and the existing compare function (i.e. strcmp)

示例:

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

#define NAMES 5
#define NAME_LEN 10

void print_names(char names[][NAME_LEN])
{
    int i;
    for(i=0; i<NAMES; ++i)
    {
        printf("%s\n", names[i]);
    }
}

int main(void) {
    char names[NAMES][NAME_LEN] = { "xxx", "uuu", "ccc", "aaa", "bbb" };

    print_names(names);
    printf("---------------------------------\n");

    qsort(names, NAMES, NAME_LEN, strcmp);

    print_names(names);

    return 0;
}

这篇关于按字母顺序对字符串列表进行排序(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆