gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:36:19 25 4
gpt4 key购买 nike

我正在尝试按字母顺序按姓氏对结构进行排序。我的大部分代码都可以正常工作,但我无法按字母顺序对其进行排序。我尝试使用交换器方法,其中您有一个临时支架,将临时与 [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;
}

关于structure - 在 C 中按姓氏按字母顺序排列结构化列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com