(3)职工信息管理职工信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等(职工号不重复),职工信息管理,可具有以下功能:职工信息录入、输出、查询、删除、修改、数据保存、文件打开等.C语言实现代码
时间: 2023-07-03 10:06:40 浏览: 127
以下是一个简单的职工信息管理系统的 C 语言实现代码,包括了职工信息的录入、输出、查询、删除、修改、数据保存、文件打开等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 50 // 定义字符串最大长度
// 声明结构体类型
typedef struct {
int id; // 职工号
char name[MAX_LEN]; // 姓名
char gender[MAX_LEN]; // 性别
char birthdate[MAX_LEN]; // 出生年月
char education[MAX_LEN]; // 学历
char position[MAX_LEN]; // 职务
float salary; // 工资
char address[MAX_LEN]; // 住址
char phone[MAX_LEN]; // 电话
} Employee;
// 声明函数
void menu();
void input(Employee *emp);
void output(Employee *emp);
void query(Employee *emp, int n);
void delete(Employee *emp, int n);
void modify(Employee *emp, int n);
void save(Employee *emp, int n);
void open(Employee *emp, int *n);
int main()
{
Employee emp[100]; // 最多存储 100 个职工信息
int n = 0; // 当前职工信息数量
int choice;
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
input(&emp[n]);
n++;
break;
case 2:
output(emp);
break;
case 3:
query(emp, n);
break;
case 4:
delete(emp, n);
n--;
break;
case 5:
modify(emp, n);
break;
case 6:
save(emp, n);
break;
case 7:
open(emp, &n);
break;
case 8:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 8);
return 0;
}
// 显示菜单
void menu()
{
printf("\n");
printf("========== Employee Management System ==========\n");
printf("1. Input employee information\n");
printf("2. Output employee information\n");
printf("3. Query employee information\n");
printf("4. Delete employee information\n");
printf("5. Modify employee information\n");
printf("6. Save employee information to file\n");
printf("7. Open employee information from file\n");
printf("8. Exit\n");
printf("Please enter your choice: ");
}
// 录入职工信息
void input(Employee *emp)
{
printf("Please enter employee information:\n");
printf("ID: ");
scanf("%d", &(emp->id));
printf("Name: ");
scanf("%s", emp->name);
printf("Gender: ");
scanf("%s", emp->gender);
printf("Birthdate: ");
scanf("%s", emp->birthdate);
printf("Education: ");
scanf("%s", emp->education);
printf("Position: ");
scanf("%s", emp->position);
printf("Salary: ");
scanf("%f", &(emp->salary));
printf("Address: ");
scanf("%s", emp->address);
printf("Phone: ");
scanf("%s", emp->phone);
}
// 输出职工信息
void output(Employee *emp)
{
printf("Employee information:\n");
printf("ID\tName\tGender\tBirthdate\tEducation\tPosition\tSalary\tAddress\tPhone\n");
for (int i = 0; i < MAX_LEN; i++) {
printf("-");
}
printf("\n");
for (int i = 0; i < MAX_LEN; i++) {
if (emp[i].id == 0) {
break;
}
printf("%d\t%s\t%s\t%s\t%s\t%s\t%.2f\t%s\t%s\n", emp[i].id, emp[i].name, emp[i].gender, emp[i].birthdate, emp[i].education, emp[i].position, emp[i].salary, emp[i].address, emp[i].phone);
}
}
// 查询职工信息
void query(Employee *emp, int n)
{
int id;
printf("Please enter the ID of the employee you want to query: ");
scanf("%d", &id);
for (int i = 0; i < n; i++) {
if (emp[i].id == id) {
printf("Employee information:\n");
printf("ID\tName\tGender\tBirthdate\tEducation\tPosition\tSalary\tAddress\tPhone\n");
for (int j = 0; j < MAX_LEN; j++) {
printf("-");
}
printf("\n");
printf("%d\t%s\t%s\t%s\t%s\t%s\t%.2f\t%s\t%s\n", emp[i].id, emp[i].name, emp[i].gender, emp[i].birthdate, emp[i].education, emp[i].position, emp[i].salary, emp[i].address, emp[i].phone);
return;
}
}
printf("Employee not found!\n");
}
// 删除职工信息
void delete(Employee *emp, int n)
{
int id;
printf("Please enter the ID of the employee you want to delete: ");
scanf("%d", &id);
for (int i = 0; i < n; i++) {
if (emp[i].id == id) {
for (int j = i; j < n-1; j++) {
emp[j] = emp[j+1];
}
emp[n-1].id = 0;
printf("Employee information deleted successfully!\n");
return;
}
}
printf("Employee not found!\n");
}
// 修改职工信息
void modify(Employee *emp, int n)
{
int id;
printf("Please enter the ID of the employee you want to modify: ");
scanf("%d", &id);
for (int i = 0; i < n; i++) {
if (emp[i].id == id) {
printf("Please enter the new information for the employee:\n");
printf("Name: ");
scanf("%s", emp[i].name);
printf("Gender: ");
scanf("%s", emp[i].gender);
printf("Birthdate: ");
scanf("%s", emp[i].birthdate);
printf("Education: ");
scanf("%s", emp[i].education);
printf("Position: ");
scanf("%s", emp[i].position);
printf("Salary: ");
scanf("%f", &(emp[i].salary));
printf("Address: ");
scanf("%s", emp[i].address);
printf("Phone: ");
scanf("%s", emp[i].phone);
printf("Employee information modified successfully!\n");
return;
}
}
printf("Employee not found!\n");
}
// 保存职工信息到文件
void save(Employee *emp, int n)
{
FILE *fp;
fp = fopen("employee.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
for (int i = 0; i < n; i++) {
fprintf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%.2f\t%s\t%s\n", emp[i].id, emp[i].name, emp[i].gender, emp[i].birthdate, emp[i].education, emp[i].position, emp[i].salary, emp[i].address, emp[i].phone);
}
fclose(fp);
printf("Employee information saved to file!\n");
}
// 从文件中打开职工信息
void open(Employee *emp, int *n)
{
FILE *fp;
fp = fopen("employee.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
*n = 0;
while (fscanf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%s\t%s\n", &(emp[*n].id), emp[*n].name, emp[*n].gender, emp[*n].birthdate, emp[*n].education, emp[*n].position, &(emp[*n].salary), emp[*n].address, emp[*n].phone) != EOF) {
(*n)++;
}
fclose(fp);
printf("Employee information opened from file!\n");
}
```
上述代码实现了一个简单的职工信息管理系统,包括了职工信息的录入、输出、查询、删除、修改、数据保存、文件打开等功能。
阅读全文