C语言设计一个小型公司人员管理程序,程序能够实现对人员信息的输入、输出、插入、删除、修改、查找和排序等基本功能,使用文件作为信息的外部存储形式
时间: 2023-07-10 15:13:03 浏览: 87
好的,我可以为您提供一种实现方式,具体如下:
1. 定义人员信息的结构体,包含姓名、性别、年龄、职位等字段。
```c
typedef struct {
char name[20];
char gender[5];
int age;
char position[20];
} Employee;
```
2. 定义文件存储路径和文件名。
```c
#define FILE_PATH "./employee.dat"
```
3. 实现人员信息的输入和输出功能。
```c
void inputEmployee(Employee *emp) {
printf("请输入员工姓名:");
scanf("%s", emp->name);
printf("请输入员工性别:");
scanf("%s", emp->gender);
printf("请输入员工年龄:");
scanf("%d", &emp->age);
printf("请输入员工职位:");
scanf("%s", emp->position);
}
void outputEmployee(Employee emp) {
printf("姓名:%s\t性别:%s\t年龄:%d\t职位:%s\n", emp.name, emp.gender, emp.age, emp.position);
}
```
4. 实现插入、删除、修改、查找和排序等基本功能。
```c
void insertEmployee() {
Employee emp;
FILE *fp;
fp = fopen(FILE_PATH, "ab");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
inputEmployee(&emp);
fwrite(&emp, sizeof(Employee), 1, fp);
fclose(fp);
}
void deleteEmployee() {
char name[20];
FILE *fp, *temp;
Employee emp;
fp = fopen(FILE_PATH, "rb");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
temp = fopen("temp.dat", "wb");
if (temp == NULL) {
printf("文件打开失败\n");
return;
}
printf("请输入要删除的员工姓名:");
scanf("%s", name);
while (fread(&emp, sizeof(Employee), 1, fp) == 1) {
if (strcmp(emp.name, name) != 0) {
fwrite(&emp, sizeof(Employee), 1, temp);
}
}
fclose(fp);
fclose(temp);
remove(FILE_PATH);
rename("temp.dat", FILE_PATH);
}
void modifyEmployee() {
char name[20];
FILE *fp;
Employee emp;
int found = 0;
fp = fopen(FILE_PATH, "rb+");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
printf("请输入要修改的员工姓名:");
scanf("%s", name);
while (fread(&emp, sizeof(Employee), 1, fp) == 1) {
if (strcmp(emp.name, name) == 0) {
inputEmployee(&emp);
fseek(fp, -sizeof(Employee), SEEK_CUR);
fwrite(&emp, sizeof(Employee), 1, fp);
found = 1;
break;
}
}
if (!found) {
printf("未找到该员工\n");
}
fclose(fp);
}
void searchEmployee() {
char name[20];
FILE *fp;
Employee emp;
int found = 0;
fp = fopen(FILE_PATH, "rb");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
printf("请输入要查找的员工姓名:");
scanf("%s", name);
while (fread(&emp, sizeof(Employee), 1, fp) == 1) {
if (strcmp(emp.name, name) == 0) {
outputEmployee(emp);
found = 1;
break;
}
}
if (!found) {
printf("未找到该员工\n");
}
fclose(fp);
}
void sortEmployee() {
FILE *fp;
Employee empArray[100], temp;
int i, j, count;
fp = fopen(FILE_PATH, "rb");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
count = 0;
while (fread(&empArray[count], sizeof(Employee), 1, fp) == 1) {
count++;
}
for (i = 0; i < count - 1; i++) {
for (j = i + 1; j < count; j++) {
if (strcmp(empArray[i].name, empArray[j].name) > 0) {
temp = empArray[i];
empArray[i] = empArray[j];
empArray[j] = temp;
}
}
}
for (i = 0; i < count; i++) {
outputEmployee(empArray[i]);
}
fclose(fp);
}
```
5. 实现主函数,根据用户选择调用相应的功能函数。
```c
int main() {
int choice;
while (1) {
printf("1. 输入员工信息\n");
printf("2. 删除员工信息\n");
printf("3. 修改员工信息\n");
printf("4. 查找员工信息\n");
printf("5. 排序员工信息\n");
printf("6. 输出所有员工信息\n");
printf("0. 退出程序\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
insertEmployee();
break;
case 2:
deleteEmployee();
break;
case 3:
modifyEmployee();
break;
case 4:
searchEmployee();
break;
case 5:
sortEmployee();
break;
case 6:
outputAllEmployee();
break;
case 0:
return 0;
default:
printf("输入有误,请重新输入\n");
break;
}
}
}
```
6. 实现输出所有员工信息的函数。
```c
void outputAllEmployee() {
FILE *fp;
Employee emp;
fp = fopen(FILE_PATH, "rb");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
while (fread(&emp, sizeof(Employee), 1, fp) == 1) {
outputEmployee(emp);
}
fclose(fp);
}
```
以上就是一个简单的小型公司人员管理程序的实现,您可以根据需要进行修改和完善。
阅读全文