温馨提示×

C语言怎么实现简单职工信息管理系统

小亿
94
2023-07-28 13:12:56
栏目: 编程语言

要实现一个简单的职工信息管理系统,可以使用C语言结合文件操作来实现。以下是一个简单的示例:

#include <stdio.h>
#include <stdlib.h>
#define MAX_EMPLOYEES 100
struct Employee {
char name[50];
int age;
float salary;
};
void addEmployee(struct Employee employees[], int* count);
void displayEmployees(struct Employee employees[], int count);
int main() {
struct Employee employees[MAX_EMPLOYEES];
int count = 0;
int choice;
do {
printf("1. Add employee\n");
printf("2. Display employees\n");
printf("3. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addEmployee(employees, &count);
break;
case 2:
displayEmployees(employees, count);
break;
case 3:
printf("Exiting program\n");
break;
default:
printf("Invalid choice\n");
break;
}
} while(choice != 3);
return 0;
}
void addEmployee(struct Employee employees[], int* count) {
if (*count >= MAX_EMPLOYEES) {
printf("Maximum number of employees reached\n");
return;
}
printf("Enter employee name: ");
scanf("%s", employees[*count].name);
printf("Enter employee age: ");
scanf("%d", &employees[*count].age);
printf("Enter employee salary: ");
scanf("%f", &employees[*count].salary);
(*count)++;
}
void displayEmployees(struct Employee employees[], int count) {
if (count == 0) {
printf("No employees to display\n");
return;
}
printf("Employee information:\n");
for (int i = 0; i < count; i++) {
printf("Name: %s\n", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("Salary: %.2f\n", employees[i].salary);
printf("--------------------\n");
}
}

这个示例代码中,定义了一个结构体 Employee 表示职工的信息,包含了姓名、年龄和工资。使用 addEmployee 函数可以添加职工,使用 displayEmployees 函数可以显示所有职工的信息。

程序通过一个循环菜单实现交互,用户可以选择添加职工、显示职工信息或退出程序。

在实际使用中,可以将职工信息保存到文件中,比如使用 fopen 函数打开一个文件,使用 fprintf 函数将职工信息写入文件,使用 fscanf 函数从文件读取职工信息。

0