c语言员工管理系统 包含编号想,姓名,性别,电话,地址,学历,完成查询更新插入删除操作
时间: 2024-03-16 12:42:16 浏览: 60
C语言员工管理系统 通过动态链表的创建,插入,删除,更新,查询
好的,这是一个较为复杂的问题,我会尽力回答。根据您的需求,我可以为您提供一个简单的C语言员工管理系统的框架,包括以下功能:
1. 添加员工信息:从键盘输入员工编号,姓名,性别,电话,地址,学历等信息,将其保存在文件中。
2. 查询员工信息:从文件中读取员工信息,并根据用户输入的关键字(如编号、姓名、电话等)进行查询。
3. 更新员工信息:根据员工编号,修改员工的其他信息。
4. 删除员工信息:根据员工编号,从文件中删除员工信息。
下面是一个简单的C语言员工管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct employee {
int id;
char name[20];
char gender[10];
char phone[20];
char address[50];
char education[20];
};
void add_employee() {
FILE *fp;
struct employee emp;
fp = fopen("employee.dat", "ab");
printf("Enter employee id: ");
scanf("%d", &emp.id);
printf("Enter employee name: ");
scanf("%s", emp.name);
printf("Enter employee gender: ");
scanf("%s", emp.gender);
printf("Enter employee phone: ");
scanf("%s", emp.phone);
printf("Enter employee address: ");
scanf("%s", emp.address);
printf("Enter employee education: ");
scanf("%s", emp.education);
fwrite(&emp, sizeof(emp), 1, fp);
fclose(fp);
}
void search_employee() {
FILE *fp;
struct employee emp;
int id, found = 0;
char name[20], phone[20];
printf("Enter search keyword: ");
scanf("%s", name);
fp = fopen("employee.dat", "rb");
while (fread(&emp, sizeof(emp), 1, fp)) {
if (strcmp(emp.name, name) == 0 || strcmp(emp.phone, name) == 0 || emp.id == atoi(name)) {
printf("Employee details:\n");
printf("ID: %d\n", emp.id);
printf("Name: %s\n", emp.name);
printf("Gender: %s\n", emp.gender);
printf("Phone: %s\n", emp.phone);
printf("Address: %s\n", emp.address);
printf("Education: %s\n", emp.education);
found = 1;
}
}
if (!found) {
printf("Employee not found.\n");
}
fclose(fp);
}
void update_employee() {
FILE *fp;
struct employee emp;
int id, found = 0;
fp = fopen("employee.dat", "rb+");
printf("Enter employee id to update: ");
scanf("%d", &id);
while (fread(&emp, sizeof(emp), 1, fp)) {
if (emp.id == id) {
printf("Enter new details:\n");
printf("Enter employee name: ");
scanf("%s", emp.name);
printf("Enter employee gender: ");
scanf("%s", emp.gender);
printf("Enter employee phone: ");
scanf("%s", emp.phone);
printf("Enter employee address: ");
scanf("%s", emp.address);
printf("Enter employee education: ");
scanf("%s", emp.education);
fseek(fp, -sizeof(emp), SEEK_CUR);
fwrite(&emp, sizeof(emp), 1, fp);
found = 1;
break;
}
}
if (!found) {
printf("Employee not found.\n");
}
fclose(fp);
}
void delete_employee() {
FILE *fp, *temp;
struct employee emp;
int id, found = 0;
fp = fopen("employee.dat", "rb");
temp = fopen("temp.dat", "wb");
printf("Enter employee id to delete: ");
scanf("%d", &id);
while (fread(&emp, sizeof(emp), 1, fp)) {
if (emp.id != id) {
fwrite(&emp, sizeof(emp), 1, temp);
} else {
found = 1;
}
}
if (!found) {
printf("Employee not found.\n");
} else {
printf("Employee deleted successfully.\n");
}
fclose(fp);
fclose(temp);
remove("employee.dat");
rename("temp.dat", "employee.dat");
}
int main() {
int choice;
do {
printf("Employee Management System\n");
printf("1. Add employee\n");
printf("2. Search employee\n");
printf("3. Update employee\n");
printf("4. Delete employee\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_employee();
break;
case 2:
search_employee();
break;
case 3:
update_employee();
break;
case 4:
delete_employee();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 5);
return 0;
}
```
以上是一个简单的C语言员工管理系统,包括添加、查询、更新和删除员工信息的基本功能。您可以根据自己的需求进行修改和完善。
阅读全文