首页 > 解决方案 > 根据名称对结构项数组进行排序

问题描述

我希望有人能帮助我。我是编程新手,我的问题是,我试图根据哪个是在该结构项中输入的名称的第一个字母,按字母顺序对不同的结构项进行排序。我尝试了不同的东西,但我被困在这里。

我在这里粘贴了我写的代码。我评论了我帮助的部分,因为我需要程序是西班牙语,所以我的代码的某些部分是用这种语言编写的。我希望这对于理解我正在尝试做的事情不是问题。

PD。如果你们能给我一些建议来提高我的技能,那就太好了。提前致谢。

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

#define MAXNOMBRE 15
#define MAXAPELLIDO 15
#define MAXDIRECCION 30
#define MAXTEL 12
#define MAXALUMNOS 4


struct Alumno
{
    char nombre[MAXNOMBRE];
    char apellido[MAXAPELLIDO];
    char direccion[MAXDIRECCION];
    int edad;
    char telefono[MAXTEL];
} alumnos[MAXALUMNOS], alumnos_cpy[1]; 

int main()
{
    int contador=0, i=0, j=0, k;
    char seleccion;


    system("CLS");

    do
    {
        printf("BIENVENIDO AL REGISTRO\n");
        printf("a) Registrar alumno\n");
        printf("b) Mostrar alumnos\n");
        printf("c) Salir\n");
        scanf("%s", &seleccion);

        switch (seleccion)
        {
            case 'A': case 'a':
                if (contador < MAXALUMNOS)
                {   
                    printf("Lugares Disponibles: <%d>\n", MAXALUMNOS-contador);
                    printf("Numero de alumnos que desee registrar: ");
                    fflush(stdin);
                    scanf("%d", &j);

                    for (i = 0; i < j; i++)
                    {
                        printf("\n");

                        printf("Ingrese el nombre del alumno: ");
                        fflush(stdin);
                        gets(alumnos[contador].nombre);
                        printf("Ingrese el apellido del alumno: ");
                        fflush(stdin);
                        gets(alumnos[contador].apellido);
                        printf("Ingrese direccion (calle y numero): ");
                        fflush(stdin);
                        gets(alumnos[contador].direccion);
                        printf("Ingrese edad: ");
                        fflush(stdin);
                        scanf("%d", &alumnos[contador].edad);
                        printf("Ingrese telefono: ");
                        fflush(stdin);
                        gets(alumnos[contador].telefono);
                        contador++;

                        printf("\n");
                    }
                }
                else
                {
                    printf("La memoria ya esta llena!\n");
                }

                //ALPHABETICALLY SORTING
                //ALPHABETICALLY SORTING
                //ALPHABETICALLY SORTING
                for (i = 0; i <= contador-1; i++)
                {   
                    if (alumnos[i].nombre[0] > alumnos[i+1].nombre[0])
                    {

                        // memcpy(&alumnos_cpy[0], &alumnos[i], sizeof(alumnos[i]));
                        // memcpy(&alumnos[i], &alumnos[i+1], sizeof(alumnos[i+1]));
                        // memcpy(&alumnos[i+1], &alumnos_cpy[0], sizeof(alumnos_cpy[0]));
                        // //alumnos_cpy[0] = alumnos[i];
                        // //alumnos[i] = alumnos[i+1];
                        // //alumnos[i+1] = alumnos_cpy[0];
                    }
                }
                //ALPHABETICALLY SORTING
                //ALPHABETICALLY SORTING
                //ALPHABETICALLY SORTING


                printf("\nPresione Enter para continuar...");
                break;


            case 'B': case 'b':
                for (i = 0; i < contador; i++)
                {
                    printf("\n\nAlumno %d", i+1);
                    printf("\nNombre: %s %s", alumnos[i].nombre, alumnos[i].apellido);
                    printf("\nDireccion: %s", alumnos[i].direccion);
                    printf("\nEdad: %d", alumnos[i].edad);
                    printf("\nTelefono: %s", alumnos[i].telefono);
                }

                printf("\nPresione Enter para continuar...");
                break;

            case 'C': case 'c':
                printf("Presione Enter para salir...");
                break;

            default:
                printf("Seleccion Incorrecta - Intente de Nuevo\n");
                printf("Presione Enter para continuar...");
                break;

        }

        getch();
        system("CLS");

    } while (seleccion != 'C' && seleccion != 'c');


    printf("\n");
    getch();
    return 0;
}

标签: carraysstringsorting

解决方案


qsort您可以使用它来为您完成工作,而不是手动复制整个结构。您只需编写一个返回 -1、0 或 1 的辅助函数。该qsort函数将完成其余的逻辑。

// qsort helper function
int alumno_sort(void *p1, void *p2) {
    struct Alumno *a1 = p1, *a2 = p2;
    return strcasecmp(a1->nombre, a2->nombre);
}

// actually sorting
qsort(alumnos, contador, sizeof *alumnos, alumno_sort);

解决此问题的另一种方法是将结构放入例如链表而不是数组中。然后,您可以交换对其他项目的引用以对列表进行排序。


推荐阅读