c语言程序设计职员信息管理,C语言程序设计,编写一个职工信息管理系统
时间: 2023-09-24 17:11:41 浏览: 37
好的,我可以为您提供一些思路和代码示例。首先,职工信息管理系统需要记录每个职工的姓名、工号、年龄、性别、职位、工资等信息。以下是一个简单的示例程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100
typedef struct {
char name[20];
int id;
int age;
char gender[10];
char position[20];
double salary;
} Employee;
int num_employees = 0;
Employee employees[MAX_EMPLOYEES];
void add_employee() {
Employee employee;
printf("Enter name: ");
scanf("%s", employee.name);
printf("Enter ID: ");
scanf("%d", &employee.id);
printf("Enter age: ");
scanf("%d", &employee.age);
printf("Enter gender: ");
scanf("%s", employee.gender);
printf("Enter position: ");
scanf("%s", employee.position);
printf("Enter salary: ");
scanf("%lf", &employee.salary);
employees[num_employees++] = employee;
printf("Employee added successfully.\n");
}
void print_employee(Employee employee) {
printf("Name: %s\n", employee.name);
printf("ID: %d\n", employee.id);
printf("Age: %d\n", employee.age);
printf("Gender: %s\n", employee.gender);
printf("Position: %s\n", employee.position);
printf("Salary: %.2lf\n", employee.salary);
}
void view_employees() {
if (num_employees == 0) {
printf("No employees found.\n");
return;
}
printf("List of employees:\n");
for (int i = 0; i < num_employees; i++) {
printf("Employee #%d:\n", i + 1);
print_employee(employees[i]);
}
}
void delete_employee() {
int id;
printf("Enter ID of employee to delete: ");
scanf("%d", &id);
for (int i = 0; i < num_employees; i++) {
if (employees[i].id == id) {
for (int j = i; j < num_employees - 1; j++) {
employees[j] = employees[j + 1];
}
num_employees--;
printf("Employee deleted successfully.\n");
return;
}
}
printf("Employee not found.\n");
}
void save_employees() {
FILE *fp = fopen("employees.dat", "wb");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
fwrite(&num_employees, sizeof(int), 1, fp);
fwrite(employees, sizeof(Employee), num_employees, fp);
fclose(fp);
printf("Employees saved to file.\n");
}
void load_employees() {
FILE *fp = fopen("employees.dat", "rb");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
fread(&num_employees, sizeof(int), 1, fp);
fread(employees, sizeof(Employee), num_employees, fp);
fclose(fp);
printf("Employees loaded from file.\n");
}
int main() {
int choice;
while (1) {
printf("Employee Management Menu:\n");
printf("1. Add Employee\n");
printf("2. View Employees\n");
printf("3. Delete Employee\n");
printf("4. Save Employees\n");
printf("5. Load Employees\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_employee();
break;
case 2:
view_employees();
break;
case 3:
delete_employee();
break;
case 4:
save_employees();
break;
case 5:
load_employees();
break;
case 6:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
}
}
```
该程序使用了一个结构体数组来记录职工信息,并提供了以下功能:
1. 添加职工信息。
2. 查看所有职工信息。
3. 删除指定职工。
4. 将职工信息保存到文件。
5. 从文件中加载职工信息。
6. 退出程序。
您可以根据需要自行修改代码。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)