我正在尝试创建一个员工列表,每次添加新员工时都会根据年龄对列表进行排序我遇到的问题是只有一个雇员的名单是“排序”的如果我加上另一个雇员,所有年龄突然变成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_sort
循环中的for
中,将其更改为
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注意每个printf中的
list[I]
而不是list[i+1]
编辑
您可以在
qsort
头文件的函数中使用stdlib.h
biult,而不是自己对其进行排序。实现:你需要定义一个比较器函数,因为你没有一个传统的数组来排序
定义一个函数
int comparator(const struct employee *p, const struct employee *q)
{
int l = p->age;
int r = q->age;
return (l - r);
}
然后按如下方式对列表调用进行排序
qsort(list,i,sizeof(list[0]),comparator);
关于c - 在C中对递增列表进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47686155/