用c语言写职工信息管理系统设计 总体需求 职工信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等(职工号不重 复)。所有信息用文件保存。 功能要求: (1)系统以菜单方式工作 (2)职工信息录入功能(职工信息用文件保存)——输入 (3)职工信息浏览功能——输出 (4)查询或排序功能:按工资查询,按学历查询等 (5)职工信息删除功能 (6)职工信息修改功能 (7)按照某种信息进行排序显示(比如工资从低到高等)
时间: 2023-12-17 20:02:33 浏览: 70
C 语言 职工信息管理系统设计
5星 · 资源好评率100%
这是一个比较典型的文件处理程序,需要用到结构体和文件操作函数,以下是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORKER 100
typedef struct {
int id;
char name[20];
char gender[5];
char birth[20];
char edu[20];
char position[20];
float salary;
char address[50];
char phone[20];
} Worker;
Worker workers[MAX_WORKER];
int worker_count = 0;
void load_workers() {
FILE *fp = fopen("workers.dat", "rb");
if (fp == NULL) {
return;
}
while (fread(&workers[worker_count], sizeof(Worker), 1, fp) == 1) {
worker_count++;
}
fclose(fp);
}
void save_workers() {
FILE *fp = fopen("workers.dat", "wb");
if (fp == NULL) {
printf("Error: cannot open file.\n");
return;
}
fwrite(workers, sizeof(Worker), worker_count, fp);
fclose(fp);
}
int find_worker_by_id(int id) {
for (int i = 0; i < worker_count; i++) {
if (workers[i].id == id) {
return i;
}
}
return -1;
}
void add_worker() {
if (worker_count >= MAX_WORKER) {
printf("Error: worker count exceeds maximum.\n");
return;
}
Worker worker;
printf("Enter worker id: ");
scanf("%d", &worker.id);
if (find_worker_by_id(worker.id) >= 0) {
printf("Error: worker id already exists.\n");
return;
}
printf("Enter worker name: ");
scanf("%s", worker.name);
printf("Enter worker gender: ");
scanf("%s", worker.gender);
printf("Enter worker birth: ");
scanf("%s", worker.birth);
printf("Enter worker edu: ");
scanf("%s", worker.edu);
printf("Enter worker position: ");
scanf("%s", worker.position);
printf("Enter worker salary: ");
scanf("%f", &worker.salary);
printf("Enter worker address: ");
scanf("%s", worker.address);
printf("Enter worker phone: ");
scanf("%s", worker.phone);
workers[worker_count] = worker;
worker_count++;
save_workers();
printf("Worker added successfully.\n");
}
void print_worker(int index) {
printf("%5d %10s %5s %12s %10s %10s %8.2f %20s %15s\n",
workers[index].id, workers[index].name, workers[index].gender,
workers[index].birth, workers[index].edu, workers[index].position,
workers[index].salary, workers[index].address, workers[index].phone);
}
void list_all_workers() {
printf("%5s %10s %5s %12s %10s %10s %8s %20s %15s\n",
"ID", "Name", "Gender", "Birth", "Edu", "Position", "Salary", "Address", "Phone");
for (int i = 0; i < worker_count; i++) {
print_worker(i);
}
}
void query_by_salary() {
float min_salary, max_salary;
printf("Enter the minimum salary: ");
scanf("%f", &min_salary);
printf("Enter the maximum salary: ");
scanf("%f", &max_salary);
printf("%5s %10s %5s %12s %10s %10s %8s %20s %15s\n",
"ID", "Name", "Gender", "Birth", "Edu", "Position", "Salary", "Address", "Phone");
for (int i = 0; i < worker_count; i++) {
if (workers[i].salary >= min_salary && workers[i].salary <= max_salary) {
print_worker(i);
}
}
}
void query_by_edu() {
char edu[20];
printf("Enter the education: ");
scanf("%s", edu);
printf("%5s %10s %5s %12s %10s %10s %8s %20s %15s\n",
"ID", "Name", "Gender", "Birth", "Edu", "Position", "Salary", "Address", "Phone");
for (int i = 0; i < worker_count; i++) {
if (strcmp(workers[i].edu, edu) == 0) {
print_worker(i);
}
}
}
void delete_worker() {
int id;
printf("Enter worker id to delete: ");
scanf("%d", &id);
int index = find_worker_by_id(id);
if (index < 0) {
printf("Error: worker not found.\n");
return;
}
for (int i = index; i < worker_count - 1; i++) {
workers[i] = workers[i + 1];
}
worker_count--;
save_workers();
printf("Worker deleted successfully.\n");
}
void modify_worker() {
int id;
printf("Enter worker id to modify: ");
scanf("%d", &id);
int index = find_worker_by_id(id);
if (index < 0) {
printf("Error: worker not found.\n");
return;
}
printf("Enter worker name (old: %s): ", workers[index].name);
scanf("%s", workers[index].name);
printf("Enter worker gender (old: %s): ", workers[index].gender);
scanf("%s", workers[index].gender);
printf("Enter worker birth (old: %s): ", workers[index].birth);
scanf("%s", workers[index].birth);
printf("Enter worker edu (old: %s): ", workers[index].edu);
scanf("%s", workers[index].edu);
printf("Enter worker position (old: %s): ", workers[index].position);
scanf("%s", workers[index].position);
printf("Enter worker salary (old: %.2f): ", workers[index].salary);
scanf("%f", &workers[index].salary);
printf("Enter worker address (old: %s): ", workers[index].address);
scanf("%s", workers[index].address);
printf("Enter worker phone (old: %s): ", workers[index].phone);
scanf("%s", workers[index].phone);
save_workers();
printf("Worker modified successfully.\n");
}
void sort_by_salary() {
for (int i = 0; i < worker_count - 1; i++) {
for (int j = i + 1; j < worker_count; j++) {
if (workers[i].salary > workers[j].salary) {
Worker temp = workers[i];
workers[i] = workers[j];
workers[j] = temp;
}
}
}
printf("%5s %10s %5s %12s %10s %10s %8s %20s %15s\n",
"ID", "Name", "Gender", "Birth", "Edu", "Position", "Salary", "Address", "Phone");
for (int i = 0; i < worker_count; i++) {
print_worker(i);
}
}
void menu() {
printf("=== Worker Management System ===\n");
printf("1. Add worker\n");
printf("2. List all workers\n");
printf("3. Query by salary\n");
printf("4. Query by education\n");
printf("5. Delete worker\n");
printf("6. Modify worker\n");
printf("7. Sort by salary\n");
printf("0. Exit\n");
printf("Enter your choice: ");
}
int main() {
load_workers();
while (1) {
menu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_worker();
break;
case 2:
list_all_workers();
break;
case 3:
query_by_salary();
break;
case 4:
query_by_edu();
break;
case 5:
delete_worker();
break;
case 6:
modify_worker();
break;
case 7:
sort_by_salary();
break;
case 0:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
注意,这个程序使用的是二进制文件,而不是文本文件,因此不能用普通的文本编辑器打开和查看。如果要查看可以使用二进制编辑器或者专门的文件查看工具。另外,由于没有进行输入格式验证和异常处理,如果输入错误可能导致程序崩溃或者出现不可预期的行为。在实际使用中需要根据实际情况进行修改和完善。
阅读全文