在 C 中按姓氏按字母顺序排列结构化列表

Alphabetizing structured lists by last name in C

我正在尝试按姓氏字母顺序对结构进行排序。我的大部分代码都可以正常工作,但我无法让它按字母顺序排序。我尝试使用交换器方法,您有一个临时持有人,将临时与 [s] 交换,并将 [s] 与 [s+1] 交换,但我无法正确交换。我试图创建一个函数并将结构转移到函数中,但我也无法使其正常运行。我是 C 的新手,我的语法不是很好。我在对充满数字的数组进行排序方面表现不错,但我一直无法找到任何排序结构的示例。任何帮助将不胜感激。到目前为止,这是我的代码:

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

struct student{
char f_name[20];
char l_name[20];
char temp[20];
int id_num;
float test1;
float test2;
float test3;
}s[10];

int main(void)
{
char grade;
float average;
int i, j, b;

for(i=0;i<10;i++)
{
printf("Enter first name(ZZ to quit): ");
scanf("%s", &s[i].f_name);
if(strcmp(s[i].f_name, "ZZ")==0)
    break;
else
{
printf("Enter students last name: ");
scanf("%s", &s[i].l_name);
printf("Enter Student ID: ");
scanf("%d", s[i].id_num);
printf("Enter 3 test scores: ");
scanf("%f %f %f", &s[i].test1, &s[i].test2, &s[i].test3);
    b++;
    }
}

i--;
printf("Student\t\tStudent Id\tFinal Grade\n");
for(i;i>=0;i--)
{
average = (s[i].test1 + s[i].test2 + s[i].test3) / 3;
if(average >= 90)
   grade = 'A';
else if(average >=80 || < 90)
   grade = 'B';
else if(average >= 70 || < 80)
   grade = 'C';
else if(average >= 60 || < 70)
   grade = 'D';
else
   grade = 'F';

printf("%s, %s\t%d\t\t%.2f \t%c\n", s[i].l_name, s[i].f_name, s[i].id_num, average, grade);
}
return 0;
}

由于您是 C 的新手,我将排序代码插入到您自己的代码中并进行了一些小的但必要的更正。

您的代码不太长,所以我在下面粘贴了完整的程序,并附有解释添加代码的注释。

不是最佳或高效的解决方案,但我想让您自己的代码正常工作,无需对其进行太多改动,以便您可以理解该解决方案,并且无需介绍许多您可能不理解的新概念。

随着你了解更多,如果你转向 C++,你会发现有更优雅和有效的方法来解决这个问题(尤其是 C++ 和它的排序算法)。

我也是按照名字排序的,如果姓氏相同,并且包含注释掉的硬代码,这样你可以更快地测试它,希望你继续自己重构和改进你的代码.

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

struct student{
    char f_name[20];
    char l_name[20];
    char temp[20];
    int id_num;
    float test1;
    float test2;
    float test3;
}s[10];

int main(void)
{
    char grade;
    float average;
    int i, j = 0, b = 0; //CORRECTION

    ////hard code for testing. UNCOMMENT, THEN COMMENT OUT INPUT 'FOR LOOP' FOR HARD CODE TESTING
    //strcpy(s[4].f_name, "Rob");
    //strcpy(s[4].l_name, "Zenf");
    //s[4].id_num = 5;
    //s[4].test1 = 60, s[4].test2 = 65, s[4].test3 = 70;

    //strcpy(s[3].f_name, "Red");
    //strcpy(s[3].l_name, "Zenf");
    //s[3].id_num = 4;
    //s[3].test1 = 80, s[3].test2 = 70, s[3].test3 = 75;

    //strcpy(s[2].f_name, "Adam");
    //strcpy(s[2].l_name, "Ant");
    //s[2].id_num = 3;
    //s[2].test1 = 100, s[2].test2 = 100, s[2].test3 = 100;

    //strcpy(s[1].f_name, "Zac"); 
    //strcpy(s[1].l_name, "Miller");
    //s[1].id_num = 2;
    //s[1].test1 = 60, s[1].test2 = 50, s[1].test3 = 40;

    //strcpy(s[0].f_name, "Ed");
    //strcpy(s[0].l_name, "George");
    //s[0].id_num = 1;
    //s[0].test1 = 90, s[0].test2 = 80, s[0].test3 = 70;


    //i = 5; // 1 more than index number of last hard-coded student

    ////end hard code

    //COMMENT OUT THIS 'FOR LOOP' FOR HARD CODE TESTING
    for (i = 0; i<10; i++)
    {
        printf("Enter first name(ZZ to quit): ");
        scanf("%s", &s[i].f_name);
        if (strcmp(s[i].f_name, "ZZ") == 0)
            break;
        else
        {
            printf("Enter students last name: ");
            scanf("%s", &s[i].l_name);
            printf("Enter Student ID: ");
            scanf("%d", &s[i].id_num); //CORRECTION
            printf("Enter 3 test scores: ");
            scanf("%f %f %f", &s[i].test1, &s[i].test2, &s[i].test3);
            b++;
        }
    }

    i--;

    //BEGINNING OF INSERTED SORT CODE. SHOULD BE REFACTORED INTO A FUNCTION

    int least; 

    //Nested for loops. For every Student (except the first; this
    //algorithm sorts in reverse alphabetical order, because 
    //your output 'for loop' prints in descending order), will 
    //compare with every remaining student name in second (nested) 
    //for loop, and swap with student name that comes first 
    //alphabetically in the second for loop, if any.
    for (int counter = i; counter > 0; --counter) {

        least = counter;

        for (int k = counter - 1; k > -1; --k) {

            // Compare last names. If same, compare first name
            if (   (strcmp(s[k].l_name, s[least].l_name) < 0)   ||
                ( (strcmp(s[k].l_name, s[least].l_name) == 0) && 
                (strcmp(s[k].f_name, s[least].f_name) < 0) )   )

                least = k;
        }

        // Very manual swap of values. 
        // Swaps take this format: temp = a; a = b; b = temp;
        char temp_f_name[20]; strcpy(temp_f_name, s[least].f_name);
        char temp_l_name[20]; strcpy(temp_l_name, s[least].l_name);
        char temp_temp[20]; strcpy(temp_temp, s[least].temp);
        int temp_id_num = s[least].id_num;
        float temp_test1 = s[least].test1;
        float temp_test2 = s[least].test2;
        float temp_test3 = s[least].test3;

        strcpy(s[least].f_name, s[counter].f_name);
        strcpy(s[least].l_name, s[counter].l_name);
        strcpy(s[least].temp, s[counter].temp);
        s[least].id_num = s[counter].id_num;
        s[least].test1 = s[counter].test1;
        s[least].test2 = s[counter].test2;
        s[least].test3 = s[counter].test3;

        strcpy(s[counter].f_name, temp_f_name);
        strcpy(s[counter].l_name, temp_l_name);
        strcpy(s[counter].temp, temp_temp);
        s[counter].id_num = temp_id_num;
        s[counter].test1 = temp_test1;
        s[counter].test2 = temp_test2;
        s[counter].test3 = temp_test3;

    } //END OF INSERTED SORT CODE


    printf("Student\t\tStudent Id\tFinal Grade\n");
    for (i; i >= 0; i--)
    {
        average = (s[i].test1 + s[i].test2 + s[i].test3) / 3;
        if (average >= 90)
            grade = 'A';
        else if (average >= 80 && average < 90) //CORRECTION. Use && not ||. Needed "average" again
            grade = 'B';
        else if (average >= 70 && average < 80) //CORRECTION
            grade = 'C';
        else if (average >= 60 && average < 70) //CORRECTION
            grade = 'D';
        else
            grade = 'F';

        printf("%s, %s\t%d\t\t%.2f \t%c\n", s[i].l_name, s[i].f_name, s[i].id_num, average, grade);
    }
    return 0;
}