在C中对递增列表进行排序

丹·奥康奈尔

我试图创建员工列表,并在每次添加新员工时根据年龄对列表进行排序。我遇到的问题是仅对1名员工进行了“排序”。如果我突然添加另一个雇员,所有年龄都变为0。这是我的代码:

#include <stdio.h>
#include <string.h>
#include "employee.h"

int i = 0;
unsigned int employee_get_num (struct employee* list)
{
    unsigned int i;

    for (i=0; list[i].name[0]; i++);

    return i;
}

void employee_sort (struct employee* list)
{
    int n = i;
    int I, j;
    int tmp;

    printf("There are %d employees\n", n);
    for(I=0; I<n; I++)
        printf("Age: %d\n", list[i-1].age);

    for(I=0; I<(n-1); I++)
        for(j=0; j<n-I-1; j++)
            if(list[j].age > list[j+1].age){
                tmp = list[j].age;
                list[j].age = list[j+1].age;
                list[j+1].age = tmp;
            }
    printf("Sorted list:\n");
    for(I=0; I<n; I++)
        printf("%d\n", list[i-1].age);  // only printing zeros for some reason
}


void employee_add (struct employee* list)
{
    i = i+1;    // i is global, keeps track of employees
    char first[128];
    char last[128];
    char space[] = " ";

    printf ("First Name: ");
    scanf ("%s", first);
    printf("Last Name: ");
    scanf ("%s", last);
    // Concatenate first and last name into one string
    strcpy(list[i-1].name, first);
    strcat(list[i-1].name, space);
    strcat(list[i-1].name, last);

    printf (" Age: ");
    scanf("%u", &(list->age));
    printf ("Wage: ");
    scanf("%u", &(list->wage));

    employee_sort(&list[i-1]);
}

我认为我可能没有正确地增加。

头文件:

#ifndef _employee_h_
#define _employee_h_

struct employee {
    char name[128];
    unsigned int age;
    unsigned int wage;
};

unsigned int employee_get_num (struct employee* list);
void employee_print (struct employee* e);
void employee_print_all (struct employee* list);
void employee_sort (struct employee* array);
void employee_add (struct employee* list);
void employee_delete (struct employee* list);

#endif

main()(在menu.c中)

int main (unsigned int argc, char** argv)
{
    struct employee list[MAX];

    unsigned int running = 1;

    /* Set all bits in the employee array to zero */
    memset (list, 0, MAX*sizeof(struct employee));

    while (running) {
        switch (print_menu()) {
            case OPTION_ADD:
                employee_add(list);
                break;
            case OPTION_DEL:
                employee_delete(list);
                break;
            case OPTION_LIST:
                employee_print_all(list);
                break;
            case OPTION_QUIT:
                running = 0;
                break;
        };

    }

    return 0;
}

我省略了menu.c的其余部分,因为它仅打印菜单,您将在下面看到。

输出应如下所示:

[1] Add New Employee
[2] Delete an Employee
[3] List All by Age (Acending)
[4] Quit
------------------------
Selection: 1
First Name: Bob
Last Name: Smith
 Age: 40
Wage: 60000
There are 1 employees
 Age: 40
Sorted list:
40

[1] Add New Employee
[2] Delete an Employee
[3] List All by Age (Acending)
[4] Quit
------------------------
Selection: 1
First Name: John
Last Name: Connor
 Age: 35
Wage: 62000
There are 2 employees
 Age: 40
 Age: 35
Sorted list:
35
40

我添加了一些额外的打印语句,只是为了表明它应该在做什么。

我也担心这只会影响年龄,而不会影响其余信息

狙击手

首先,employee_add在调用employee_sort时要传递最后一个struct元素。通过第一位员工。将最后一行更改employee_add

employee_sort(list);

下一个问题

如果我突然添加其他员工,所有年龄段都变为0

不,不是。排序之前employee_sortfor循环中,将其更改为

printf("There are %d employees\n", n);
    for(I=0; I<n; I++)
        printf("Age: %d\n", list[I].age);

下一个

在考虑年龄和工资投入后,将其更改为

scanf("%u", &(list[i-1].age));
scanf("%u", &(list[i-1].wage));

我也担心这只会影响年龄,而不会影响其余信息

是的,您只排序年龄,您应该根据年龄对整个结构进行排序。

for(I=0; I<(n-1); I++)
        for(j=0; j<n-I-1; j++)
            if(list[j].age > list[j+1].age){
                tmp = list[j];
                list[j] = list[j+1];
                list[j+1] = tmp;
            }

和打印排序表的最终问题

 printf("Sorted list:\n");
    for(I=0; I<n; I++)
        printf("%d\n", list[I].age); 

PS注意list[I]每个printf中的而不是list[i+1]

编辑
您可以qsortstdlib.h头文件中使用biult in函数,而不用自己对其进行排序

实现:您将需要定义一个比较器函数,因为您没有要排序的传统数组

只需定义一个函数

int comparator(const struct employee *p, const struct employee *q)
{
    int l = p->age;
    int r = q->age;
    return (l - r);
}

然后,你对列表进行排序呼叫qsort这样

qsort(list,i,sizeof(list[0]),comparator);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章