C语言用结构体数组输入工号,姓名,工资,并且降序排序输出
时间: 2023-05-28 16:06:04 浏览: 155
以下是C语言代码:
```c
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[20];
float salary;
};
int main() {
int n;
printf("Enter the number of employees: ");
scanf("%d", &n);
struct Employee employees[n];
for (int i = 0; i < n; i++) {
printf("Enter employee %d details:\n", i+1);
printf("ID: ");
scanf("%d", &employees[i].id);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Salary: ");
scanf("%f", &employees[i].salary);
}
// sort employees in descending order of salary using bubble sort
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (employees[j].salary < employees[j+1].salary) {
struct Employee temp = employees[j];
employees[j] = employees[j+1];
employees[j+1] = temp;
}
}
}
printf("\nEmployees sorted in descending order of salary:\n");
printf("ID\tName\tSalary\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%.2f\n", employees[i].id, employees[i].name, employees[i].salary);
}
return 0;
}
```
该程序首先提示用户输入员工数量,然后使用结构体数组存储每个员工的工号、姓名和工资。然后,它使用冒泡排序将员工按照工资的降序排序。最后,程序输出排序后的员工列表。
阅读全文