C语言实现统计员工信息
时间: 2024-01-24 09:17:31 浏览: 71
C语言实现员工信息管理系统-附详细注释.zip
5星 · 资源好评率100%
以下是一个简单的C语言程序,可以实现统计员工信息:
```c
#include <stdio.h>
struct employee {
char name[20];
int age;
char gender;
char department[20];
float salary;
};
int main() {
int n, i;
float total_salary = 0, avg_salary;
printf("Enter the number of employees: ");
scanf("%d", &n);
struct employee emp[n];
for (i = 0; i < n; i++) {
printf("\nEnter employee %d details:\n", i+1);
printf("Name: ");
scanf("%s", emp[i].name);
printf("Age: ");
scanf("%d", &emp[i].age);
printf("Gender (M/F): ");
scanf(" %c", &emp[i].gender);
printf("Department: ");
scanf("%s", emp[i].department);
printf("Salary: ");
scanf("%f", &emp[i].salary);
total_salary += emp[i].salary;
}
avg_salary = total_salary / n;
printf("\nEmployee details:\n");
for (i = 0; i < n; i++) {
printf("\nEmployee %d:\n", i+1);
printf("Name: %s\n", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("Gender: %c\n", emp[i].gender);
printf("Department: %s\n", emp[i].department);
printf("Salary: %.2f\n", emp[i].salary);
}
printf("\nTotal salary: %.2f\n", total_salary);
printf("Average salary: %.2f\n", avg_salary);
return 0;
}
```
这个程序使用了一个结构体来存储每个员工的信息。首先让用户输入员工的数量,然后创建一个包含该数量的结构体数组。接下来,通过循环遍历每个结构体,让用户输入每个员工的详细信息,同时计算所有员工的薪水总和。最后,输出每个员工的信息以及总薪水和平均薪水。
阅读全文