C语言 对篮球运动员姓名进行排序的功能



我需要编写一个程序来接收(来自用户)篮球队的球员数量,然后我需要以动态方式创建一个数组。程序将按字母顺序对数组进行排序,然后像这样打印。我写了这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define LENGTH 20
int main(void)
{
    int numplayers, i, j;
    char persname[LENGTH], tname[LENGTH], temp[LENGTH];
    printf("Please insert the amount of basketball players in the groupn");
    scanf("%d", &numplayers);
    char **players = (char **) malloc(numplayers * sizeof(char *));
    printf("Please insert the names of your %d basketball playersn", numplayers);
    for (i = 0; i < numplayers; i++) {
        gets(persname);
        strcpy(persname[i], tname[i]);
    }
    for (i = 0; i < numplayers-1; i++) {
        for (j = i+1; j < numplayers; j++) {
            if (strcmp(persname[i], persname[j])) {
                strcpy(temp[i], persname[i]);
                strcpy(persname[i], persname[j]);
                strcpy(persname[j], temp);
            }
        }
    }
    for (i = 0; i < numplayers; i++) {
        printf("%stt%sn", tname[i], persname[i]);
    }
    return 0;
}

但是当我运行代码时,在输入团队中的玩家数量后立即出现错误,Unhandled exception at 0x507340E3 (msvcr120d.dll) in Question4.exe: 0xC0000005: Access violation reading location 0xFFFFFFCC.我做错了什么。

输入

所有名称的循环不使用players。相反,它错误地使用pernametname。这一行:

strcpy(persname[i], tname[i]);

不应该编译,你以一种没有任何意义的方式混合类型。您应该输入一行,然后将新内存动态分配到players[i]并将输入复制到该行中。如果你有strdup(),那就是它的好处。

基本上输入循环应该是这样的:

for (i = 0; i < numplayers; i++)
{
    char line[1024];
    if(fgets(line, sizeof line, stdin) != NULL)
    {
       const size_t len = strlen(line);
       players[i] = malloc(len + 1);
       if(players[i] == NULL)
       {
         fprintf(stderr, "**Out of memory!n");
         exit(1);
       }
       memcpy(players[i], line, len + 1);
    }
    else
      fprintf(stderr, "**I/O error!n");
}

这使用fgets()比可怕的从未使用过的gets()怪物更安全。

此外,您不会为各个名称分配任何空间,而只是为字符串指针数组分配任何空间。

这一行:

char** players = (char**)malloc(numplayers*sizeof(char*));

可以简化为更清晰:

char** players = malloc(numplayers * sizeof *players);

无需重复类型名称,也无需强制转换 malloc() 的返回值。

我设法解决了它! :)

这是我更新的代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define LENGTH 20
#define LENGTH2 20
int main(void)
{
    int numplayers, i, j;
    char persname[LENGTH][LENGTH2], temp[LENGTH];
    printf("Please insert the amount of basketball players in the groupn");
    scanf("%d", &numplayers);
    char** players = (char**)malloc(numplayers*sizeof(char));
    printf("Please insert the names of your %d basketball playersn", numplayers);
    for (i = 0; i < numplayers+1; i++)
    {
        gets(persname[i]);
    }
    for (i = 1; i < numplayers+1; i++)
    {
        for (j = 1; j < numplayers+1; j++)
        {
            if (strcmp(persname[j - 1], persname[j]) > 0)
            {
                strcpy(temp, persname[j - 1]);
                strcpy(persname[j - 1], persname[j]);
                strcpy(persname[j], temp);
            }
        }
    }
    printf("nBasketball players names in order are : ");
    for (i = 0; i < numplayers+1; i++)
    {
        puts(persname[i]);
    }
    getch();
    free(players);
    system("PAUSE");
    return 0;
}

相关内容

  • 没有找到相关文章