如果您正在编写 C 代码,则调用swap()
必须传递指针。
您已经确定xMin
小于y
但您交换x
和xMin
. 你会更好地交换xMin
和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[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