gpt4 book ai didi

c - 如何在 C 中对结构体执行选择排序?

转载 作者:行者123 更新时间:2023-11-30 20:27:30 25 4
gpt4 key购买 nike

我有一个处理学生日程的程序。部分、标题和教师存储在名为 ClassCollection[] 的 struct ClassInfo 数组中。其中一个选项是添加一个类。添加类(class)后,应根据标题对类(class)进行排序。尽管此函数使用 C++ 操作“cin”,但该程序应该主要用 C 编写。这就是我对 add() 函数的内容。信息的输入已经为我写好了。我只是应该对其进行排序。

void class_add() {
//check if we have room in the array
if(nextIndex < MAX_CLASSES)
{
... //Input code

int xMin,x,y;

for(y=0;y<nextIndex;y++){
xMin = y;
for(x=y+1;x<nextIndex;x++){
if(strcmp(ClassCollection[x].title,ClassCollection[xMin].title)<0)
xMin = x;
}
if(xMin!=y){
swap(ClassCollection[x],ClassCollection[xMin]);
}
}
} else {
printf("\nERROR: Your collection is full. Cannot add new entries.\n");
cin.ignore();
}
}

如果我注释掉我的代码,我可以毫无问题地将一个类添加到数组的末尾。然而,当我尝试对数组进行排序时,它所做的只是将我添加的类(class)分别更改为 0、“”、“”(分别代表部分、标题和教师)。我知道我的选择排序的基本过程应该是正确的,但我不太熟悉 C 在处理字符数组时令人讨厌的小奇怪之处。有人可以帮忙吗?如果我需要发布更多代码或解释任何变量/常量,请告诉我。

-编辑-

我替换了

if(xMin!=y){
swap(ClassCollection[x],ClassCollection[xMin]);
}

if(xMin!=y){
temp = ClassCollection[x];
ClassCollection[x]=ClassCollection[xMin];
ClassCollection[y] = temp;
}

其中 temp 定义为 ClassInfo,与 ClassCollection 相同。我一直在用数组中的 2 个类对此进行测试。当我用这个新方法添加第三个时,数组中的第一个类分别变为 0、“”、“”,分别表示部分、标题和教师。第二课和第三课成为我输入的新信息。对此有什么想法吗?

最佳答案

如果您正在编写 C 代码,则对 swap() 的调用必须传递指针。

您已确定 xMin 小于 y,但交换了 xxMin。交换 xMiny 效果会更好。

for (y = 0; y < nextIndex; y++)
{
xMin = y;
for (x = y + 1; x < nextIndex; x++)
{
if (strcmp(ClassCollection[x].title, ClassCollection[xMin].title) < 0)
xMin = x;
}
if(xMin != y)
swap(&ClassCollection[y], &ClassCollection[xMin]);
}

您应该将输入操作与排序代码分开(两个函数)。当您应该使用 C 进行编程时,您应该编写 C 代码。不要通过混合 C 和 C++ 来搞砸事情。

工作代码

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

enum { MAX_STRING_LENGTH = 64 };
enum { MAX_CLASSES = 10 };

typedef struct ClassInfo
{
char title[MAX_STRING_LENGTH];
char teacher[MAX_STRING_LENGTH];
int section;
} ClassInfo;

static ClassInfo ClassCollection[MAX_CLASSES];
static int nextIndex = 0;

static void dump_item(int i, ClassInfo const *c)
{
printf("%d: %-12s : %-12s : %3d\n", i, c->title, c->teacher, c->section);
}

static void dump_array(const char *tag, int n, ClassInfo *array)
{
printf("%s: %d\n", tag, n);
for (int i = 0; i < n; i++)
dump_item(i, &array[i]);
}

static void class_add(void)
{
if(nextIndex < MAX_CLASSES)
{
char *end;
char line[MAX_STRING_LENGTH];
printf("What is the title of the class? ");
if (fgets(ClassCollection[nextIndex].title, MAX_STRING_LENGTH, stdin) == 0)
exit(1);
if ((end = strchr(ClassCollection[nextIndex].title, '\n')) != 0)
*end = '\0';
printf("What is the name of the teacher? ");
if (fgets(ClassCollection[nextIndex].teacher, MAX_STRING_LENGTH, stdin) == 0)
exit(1);
if ((end = strchr(ClassCollection[nextIndex].teacher, '\n')) != 0)
*end = '\0';
printf("nWhat is the section number? ");
if (fgets(line, MAX_STRING_LENGTH, stdin) == 0)
exit(1);
if (sscanf(line, "%d", &ClassCollection[nextIndex].section) != 1)
exit(1);
nextIndex++;
}
else
printf("No space left\n");
}

static void swap(ClassInfo *c1, ClassInfo *c2)
{
ClassInfo t = *c1;
*c1 = *c2;
*c2 = t;
}

static void selection_sort(void)
{
int xMin, x, y;

for (y = 0; y < nextIndex; y++)
{
xMin = y;
for (x = y + 1; x < nextIndex; x++)
{
if (strcmp(ClassCollection[x].title, ClassCollection[xMin].title) < 0)
xMin = x;
}
if (xMin != y)
{
printf("Swap(%d,%d)\n", y, xMin);
swap(&ClassCollection[y], &ClassCollection[xMin]);
}
}
}

int main(void)
{
for (int i = 0; i < 3; i++)
class_add();
putchar('\n');
dump_array("Before swap", 3, ClassCollection);
swap(&ClassCollection[0], &ClassCollection[1]);
dump_array("After swap", 3, ClassCollection);
swap(&ClassCollection[0], &ClassCollection[1]);
dump_array("Before sort", 3, ClassCollection);
selection_sort();
dump_array("After sort", 3, ClassCollection);
}

输入数据

Physics
Dobson
101
Chemistry
Keeling
221
Mathematics
Toulson
312

程序的输出

忽略提示...

Before swap: 3
0: Physics : Dobson : 101
1: Chemistry : Keeling : 221
2: Mathematics : Toulson : 312
After swap: 3
0: Chemistry : Keeling : 221
1: Physics : Dobson : 101
2: Mathematics : Toulson : 312
Before sort: 3
0: Physics : Dobson : 101
1: Chemistry : Keeling : 221
2: Mathematics : Toulson : 312
Swap(0,1)
Swap(1,2)
After sort: 3
0: Chemistry : Keeling : 221
1: Mathematics : Toulson : 312
2: Physics : Dobson : 101

关于c - 如何在 C 中对结构体执行选择排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063181/

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