题目描述
某企业有 N 个( N >=5)员工,请用结构体数组存储并管理企业职工信息,职工信息主要包括:工号、姓名、年龄、工资收入等。教职工信息结构类型定义如下:
struct worker (
char ID [10];
char Name [10];
float Salary ;);
(1)请定义一个结构体数组,并把职工信息存入结构体数组中:
(2)使用插入排序法以 Salary 为关键字并以降序的方式对以上员工信息进行排序
(3)求出平均工资并输出高于平均工资的员工信息:
(4)使用希尔排序算法以 ID 为关键字并以升序的方式对员工信息进行排序:
(5)请用二分查找法以 ID 为关键词进行查找,若找到输出其工号,姓名,工资,否则输出不是公司员工:
(6)修改员工信息,以 ID 为关键词进行查找,找到后修改其工资,并显示修改后的信息。
案例代码
#include <stdio.h>
#include <string.h>
struct worker {
char ID[10];
char Name[10];
float Salary;
};
void insertSort(struct worker arr[], int n) {
int i, j;
struct worker key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j].Salary < key.Salary) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
float calculateAverageSalary(struct worker arr[], int n) {
float totalSalary = 0;
for (int i = 0; i < n; i++) {
totalSalary += arr[i].Salary;
}
return totalSalary / n;
}
void printAboveAverageSalary(struct worker arr[], int n, float averageSalary) {
printf("Employees with salary above average:\n");
for (int i = 0; i < n; i++) {
if (arr[i].Salary > averageSalary) {
printf("ID: %s, Name: %s, Salary: %.2f\n", arr[i].ID, arr[i].Name, arr[i].Salary);
}
}
}
void shellSort(struct worker arr[], int n) {
int gap, i, j;
struct worker temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i; j >= gap && strcmp(arr[j - gap].ID, temp.ID) > 0; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
int binarySearch(struct worker arr[], int low, int high, char* key) {
if (high >= low) {
int mid = low + (high - low) / 2;
if (strcmp(arr[mid].ID, key) == 0) {
return mid;
}
if (strcmp(arr[mid].ID, key) > 0) {
return binarySearch(arr, low, mid - 1, key);
}
return binarySearch(arr, mid + 1, high, key);
}
return -1;
}
void modifySalary(struct worker arr[], int n, char* key, float newSalary) {
int index = binarySearch(arr, 0, n - 1, key);
if (index != -1) {
arr[index].Salary = newSalary;
printf("Employee information after modification: \n");
printf("ID: %s, Name: %s, Salary: %.2f\n", arr[index].ID, arr[index].Name, arr[index].Salary);
} else {
printf("Employee not found in the company.\n");
}
}
int main() {
int n = 5; // 假设有5个员工
struct worker employees[n];
// 将员工信息存入结构体数组
strcpy(employees[0].ID, "001");
strcpy(employees[0].Name, "Alice");
employees[0].Salary = 5000;
strcpy(employees[1].ID, "002");
strcpy(employees[1].Name, "Bob");
employees[1].Salary = 6000;
strcpy(employees[2].ID, "003");
strcpy(employees[2].Name, "Charlie");
employees[2].Salary = 4800;
strcpy(employees[3].ID, "004");
strcpy(employees[3].Name, "David");
employees[3].Salary = 5500;
strcpy(employees[4].ID, "005");
strcpy(employees[4].Name, "Eve");
employees[4].Salary = 5200;
// 使用插入排序法以 Salary 为关键字并以降序方式对员工信息进行排序
insertSort(employees, n);
printf("Employees sorted by descending salary:\n");
for (int i = 0; i < n; i++) {
printf("ID: %s, Name: %s, Salary: %.2f\n", employees[i].ID, employees[i].Name, employees[i].Salary);
}
// 求出平均工资并输出高于平均工资的员工信息
float averageSalary = calculateAverageSalary(employees, n);
printf("Average salary: %.2f\n", averageSalary);
printAboveAverageSalary(employees, n, averageSalary);
// 使用希尔排序算法以 ID 为关键字并以升序方式对员工信息进行排序
shellSort(employees, n);
printf("Employees sorted by ascending ID:\n");
for (int i = 0; i < n; i++) {
printf("ID: %s, Name: %s, Salary: %.2f\n", employees[i].ID, employees[i].Name, employees[i].Salary);
}
// 用二分查找法以 ID 为关键词进行查找
char key[] = "003";
int searchResult = binarySearch(employees, 0, n - 1, key);
if (searchResult != -1) {
printf("Employee found at index %d - ID: %s, Name: %s, Salary: %.2f\n", searchResult, employees[searchResult].ID, employees[searchResult].Name, employees[searchResult].Salary);
} else {
printf("Employee not found in the company.\n");
}
// 修改员工信息
char modifyKey[] = "004";
float newSalary = 5800;
modifySalary(employees, n, modifyKey, newSalary);
return 0;
}
以上是一份完整的 C 语言程序,包括了结构体定义、排序函数、平均工资计算、二分查找、修改工资等功能。你可以根据自己的需求对其中的员工信息和操作进行调整。希望对你有所帮助!
© 版权声明
本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!
THE END