定义一个包含5个教师成员的结构体数组,教师信息包括职工号、姓名和工资。要求从键盘输入所有教师的信息,并设计一个sort函数实现结构体数组按教师工资由少到多排序。要有input函数和output函数。头文件用<stdio.h>和<string.h>
时间: 2023-06-19 07:08:05 浏览: 82
```c
#include <stdio.h>
#include <string.h>
#define MAX_TEACHERS 5
struct Teacher {
int id;
char name[20];
float salary;
};
void input(struct Teacher *t);
void output(struct Teacher *t);
void sort(struct Teacher *t);
int main() {
struct Teacher teachers[MAX_TEACHERS];
for (int i = 0; i < MAX_TEACHERS; i++) {
printf("请输入第 %d 个教师的信息:\n", i + 1);
input(&teachers[i]);
}
sort(teachers);
printf("按教师工资排序后的结果为:\n");
for (int i = 0; i < MAX_TEACHERS; i++) {
output(&teachers[i]);
}
return 0;
}
void input(struct Teacher *t) {
printf("请输入教师职工号:");
scanf("%d", &t->id);
printf("请输入教师姓名:");
scanf("%s", t->name);
printf("请输入教师工资:");
scanf("%f", &t->salary);
}
void output(struct Teacher *t) {
printf("职工号:%d,姓名:%s,工资:%.2f\n", t->id, t->name, t->salary);
}
void sort(struct Teacher *t) {
for (int i = 0; i < MAX_TEACHERS - 1; i++) {
for (int j = i + 1; j < MAX_TEACHERS; j++) {
if (t[i].salary > t[j].salary) {
struct Teacher temp = t[i];
t[i] = t[j];
t[j] = temp;
}
}
}
}
```
阅读全文