c语言代码求助

如题所述

第1个回答  2023-06-07
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
// 定义职工信息类
class Employee {
public:
string name;
char gender;
string hometown;
int empId;
double salary;
};
// 比较函数,用于排序
bool compareEmp(const Employee &a, const Employee &b) {
return a.empId < b.empId;
}
// 查找函数
void findEmp(const Employee empList[], int size, int id) {
bool found = false;
for (int i = 0; i < size; i++) {
if (empList[i].empId == id) {
cout << "Name: " << empList[i].name << endl;
cout << "Salary: " << empList[i].salary << endl;
found = true;
break;
}
}
if (!found) {
cout << "Employee not found." << endl;
}
}
int main() {
// 初始化员工信息
Employee empList[10] = {
{"David", 'M', "Shanghai", 1001, 5000.0},
{"Alice", 'F', "Beijing", 1002, 6000.0},
{"Bob", 'M', "Shenzhen", 1003, 7000.0},
{"Cathy", 'F', "Guangzhou", 1004, 8000.0},
{"Frank", 'M', "Hangzhou", 1005, 9000.0},
{"Eva", 'F', "Chengdu", 1006, 10000.0},
{"George", 'M', "Wuhan", 1007, 11000.0},
{"Helen", 'F', "Nanjing", 1008, 12000.0},
{"Iris", 'F', "Tianjin", 1009, 13000.0},
{"Jack", 'M', "Xi'an", 1010, 14000.0}
};
// 按职工号排序
sort(empList, empList + 10, compareEmp);
// 输出排序后的列表
cout << "Employee List:" << endl;
for (int i = 0; i < 10; i++) {
cout << empList[i].empId << "\t" << empList[i].name << "\t" << empList[i].salary << endl;
}
// 查找职工信息
int idToFind;
cout << "Enter an employee ID to find: ";
cin >> idToFind;
findEmp(empList, 10, idToFind);
// 计算平均薪水
double totalSalary = 0.0;
for (int i = 0; i < 10; i++) {
totalSalary += empList[i].salary;
}
double avgSalary = totalSalary / 10;
cout << "Average Salary: " << avgSalary << endl;
// 计算方差
double variance = 0.0;
for (int i = 0; i < 10; i++) {
variance += pow(empList[i].salary - avgSalary, 2);
}
variance /= 10;
cout << "Variance: " << variance << endl;
return 0;
}
第2个回答  2023-05-25
以下是一个简单的职工工资和信息统计管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_EMPLOYEE 100
struct Employee {
char name[20];
int id;
float salary;
};
void addEmployee(struct Employee employees[], int *count) {
if (*count >= MAX_EMPLOYEE) {
printf("The maximum number of employees has been reached.\n");
return;
}
struct Employee employee;
printf("Enter the name of the employee: ");
scanf("%s", employee.name);
printf("Enter the ID of the employee: ");
scanf("%d", &employee.id);
printf("Enter the salary of the employee: ");
scanf("%f", &employee.salary);
employees[*count] = employee;
*count += 1;
printf("Employee added successfully.\n");
}
void printEmployees(struct Employee employees[], int count) {
printf("Name\tID\tSalary\n");
for (int i = 0; i < count; i++) {
printf("%s\t%d\t%.2f\n", employees[i].name, employees[i].id, employees[i].salary);
}
}
void sortEmployees(struct Employee employees[], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (employees[i].id > employees[j].id) {
struct Employee temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
printf("Employees sorted successfully.\n");
}
void searchEmployee(struct Employee employees[], int count) {
int id;
printf("Enter the ID of the employee to search: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (employees[i].id == id) {
printf("Name: %s\n", employees[i].name);
printf("Salary: %.2f\n", employees[i].salary);
return;
}
}
printf("Employee with ID %d not found.\n", id);
}
void calculateMean(struct Employee employees[], int count) {
float total = 0;
for (int i = 0; i < count; i++) {
total += employees[i].salary;
}
float mean = total / count;
printf("The average salary of the employees is: %.2f\n", mean);
}
void calculateVariance(struct Employee employees[], int count) {
float total = 0;
for (int i = 0; i < count; i++) {
total += pow((employees[i].salary - calculateMean(employees, count)), 2);
}
float variance = total / count;
printf("The variance of the salaries of the employees is: %.2f\n", variance);
}
int main() {
struct Employee employees[MAX_EMPLOYEE];
int count = 0;
while (1) {
printf("\nMenu:\n");
printf("1. Add employee\n");
printf("2. Print employees\n");
printf("3. Sort employees\n");
printf("4. Search employee\n");
printf("5. Calculate average salary\n");
printf("6. Calculate variance of salaries\n");
printf("7. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee(employees, &count);
break;
case 2:
printEmployees(employees, count);
break;
case 3:
sortEmployees(employees, count);
break;
case 4:
searchEmployee(employees, count);
break;
case 5:
calculateMean(employees, count);
break;
case 6:
calculateVariance(employees, count);
break;
case 7:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
该程序使用结构体存储每个职工的姓名、职工号和工资。主菜单提供了以下选项:
1. 添加职工
2. 打印职工列表
3. 按职工号对职工列表进行排序
4. 查找职工
5. 统计职工的平均工资
6. 计算职工工资的方差
7. 退出程序
程序通过循环和switch语句实现菜单选择和相应操作。