职工信息管理系统(C语言实现)
时间: 2023-11-18 19:04:11 浏览: 51
职工信息管理系统是一种常见的信息管理系统,它可以用来管理公司或组织中的职工信息,包括基本信息、薪资信息、福利信息等。下面是一个简单的职工信息管理系统的 C 语言实现。
首先,需要定义职工信息结构体:
```c
#define NAME_LEN 20
#define DEPART_LEN 20
typedef struct {
int id;
char name[NAME_LEN];
int age;
char depart[DEPART_LEN];
double salary;
} Worker;
```
接着,可以定义一些函数来实现职工信息的增删改查等操作。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORKERS 100
Worker workers[MAX_WORKERS];
int num_workers = 0;
void add_worker() {
if (num_workers >= MAX_WORKERS) {
printf("The number of workers has reached the maximum.\n");
return;
}
printf("Please input the information of the worker:\n");
Worker worker;
printf("ID: ");
scanf("%d", &worker.id);
printf("Name: ");
scanf("%s", worker.name);
printf("Age: ");
scanf("%d", &worker.age);
printf("Department: ");
scanf("%s", worker.depart);
printf("Salary: ");
scanf("%lf", &worker.salary);
workers[num_workers++] = worker;
printf("Add worker successfully.\n");
}
void search_worker() {
printf("Please input the name of the worker you want to search:\n");
char name[NAME_LEN];
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_workers; i++) {
if (strcmp(workers[i].name, name) == 0) {
found = 1;
printf("ID: %d\tName: %s\tAge: %d\tDepartment: %s\tSalary: %.2f\n",
workers[i].id, workers[i].name, workers[i].age,
workers[i].depart, workers[i].salary);
}
}
if (!found) {
printf("Worker not found.\n");
}
}
void delete_worker() {
printf("Please input the ID of the worker you want to delete:\n");
int id;
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_workers; i++) {
if (workers[i].id == id) {
found = 1;
for (int j = i; j < num_workers - 1; j++) {
workers[j] = workers[j + 1];
}
num_workers--;
printf("Delete worker successfully.\n");
break;
}
}
if (!found) {
printf("Worker not found.\n");
}
}
void modify_worker() {
printf("Please input the ID of the worker you want to modify:\n");
int id;
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_workers; i++) {
if (workers[i].id == id) {
found = 1;
printf("Please input the new information of the worker:\n");
printf("ID: ");
scanf("%d", &workers[i].id);
printf("Name: ");
scanf("%s", workers[i].name);
printf("Age: ");
scanf("%d", &workers[i].age);
printf("Department: ");
scanf("%s", workers[i].depart);
printf("Salary: ");
scanf("%lf", &workers[i].salary);
printf("Modify worker successfully.\n");
break;
}
}
if (!found) {
printf("Worker not found.\n");
}
}
void print_all_workers() {
printf("All workers:\n");
for (int i = 0; i < num_workers; i++) {
printf("ID: %d\tName: %s\tAge: %d\tDepartment: %s\tSalary: %.2f\n",
workers[i].id, workers[i].name, workers[i].age,
workers[i].depart, workers[i].salary);
}
}
void print_menu() {
printf("Welcome to the worker information management system.\n");
printf("1. Add worker\n");
printf("2. Search worker\n");
printf("3. Delete worker\n");
printf("4. Modify worker\n");
printf("5. Print all workers\n");
printf("6. Exit\n");
}
int main() {
int choice = 0;
while (choice != 6) {
print_menu();
printf("Please input your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_worker();
break;
case 2:
search_worker();
break;
case 3:
delete_worker();
break;
case 4:
modify_worker();
break;
case 5:
print_all_workers();
break;
case 6:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
这个程序中,我们定义了一个 `Worker` 结构体来存储职工信息,使用一个全局数组 `workers` 来存储所有的职工信息。然后,我们定义了一些函数来实现职工信息的增删改查等操作。在 `main()` 函数中,我们通过一个循环来不断显示菜单,并根据用户的选择调用相应的函数。