用c++写一个职工管理系统
时间: 2023-03-25 16:03:58 浏览: 66
好的,我可以回答这个问题。职工管理系统可以包括以下功能:添加职工信息、删除职工信息、修改职工信息、查询职工信息、显示所有职工信息等。可以使用结构体来存储职工信息,使用文件来保存和读取数据。具体实现可以参考相关的C语言教程和资料。
相关问题
帮我用c++写一个职工管理系统的代码
好的,以下是一个简单的职工管理系统的 C 代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100
struct Employee {
int id;
char name[50];
int age;
char gender[10];
char department[50];
char position[50];
double salary;
};
// 声明全局变量
struct Employee employees[MAX_EMPLOYEES];
int numEmployees = 0;
// 声明函数原型
void addEmployee();
void removeEmployee();
void displayAllEmployees();
void displayEmployee(int id);
void saveToFile();
void readFromFile();
int main() {
int choice;
// 从文件读取职工信息
readFromFile();
do {
printf("\n");
printf("职工管理系统菜单\n");
printf("1. 添加职工\n");
printf("2. 删除职工\n");
printf("3. 显示所有职工\n");
printf("4. 查找职工\n");
printf("5. 保存到文件\n");
printf("0. 退出\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee();
break;
case 2:
removeEmployee();
break;
case 3:
displayAllEmployees();
break;
case 4:
printf("请输入要查找的职工 ID:");
int id;
scanf("%d", &id);
displayEmployee(id);
break;
case 5:
saveToFile();
break;
case 0:
break;
default:
printf("无效的选择,请重新输入!\n");
}
} while (choice != 0);
// 将职工信息保存到文件
saveToFile();
printf("程序已退出,谢谢使用!\n");
return 0;
}
void addEmployee() {
printf("添加职工\n");
if (numEmployees >= MAX_EMPLOYEES) {
// 如果已超过最大职工人数,则无法添加新职工
printf("已达到最大职工人数,无法添加新职工!\n");
return;
}
struct Employee newEmployee;
newEmployee.id = numEmployees + 1;
printf("请输入职工姓名:");
scanf("%s", newEmployee.name);
printf("请输入职工年龄:");
scanf("%d", &newEmployee.age);
printf("请输入职工性别:");
scanf("%s", newEmployee.gender);
printf("请输入所在部门:");
scanf("%s", newEmployee.department);
printf("请输入职位:");
scanf("%s", newEmployee.position);
printf("请输入工资:");
scanf("%lf", &newEmployee.salary);
employees[numEmployees] = newEmployee;
numEmployees++;
printf("职工信息已添加!\n");
}
void removeEmployee() {
printf("删除职工\n");
printf("请输入要删除的职工 ID:");
int id;
scanf("%d", &id);
// 找到要删除的职工索引
int index = -1;
for (int i = 0; i < numEmployees; i++) {
if (employees[i].id == id) {
index = i;
break;
}
}
if (index >= 0) {
// 找到了要删除的职工,将其从数组中删除
for (int i = index; i < numEmployees - 1; i++) {
employees[i] = employees[i + 1];
}
numEmployees--;
printf("职工信息已删除!\n");
} else {
printf("未找到要删除的职工!\n");
}
}
void displayAllEmployees() {
printf("所有职工信息\n");
if (numEmployees == 0) {
printf("当前没有任何职工信息!\n");
return;
}
printf("| %-5s | %-10s | %-5s | %-10s | %-20s | %-20s | %-10s |\n", "ID", "姓名", "年龄", "性别", "部门", "职位", "工资");
printf("--------------------------------------------------------------------------\n");
for (int i = 0; i < numEmployees; i++) {
struct Employee e = employees[i];
printf("| %-5d | %-10s | %-5d | %-10s | %-20s | %-20s | %-10.2lf |\n", e.id, e.name, e.age, e.gender, e.department, e.position, e.salary);
}
}
void displayEmployee(int id) {
printf("查找职工\n");
// 找到要查找的职工索引
int index = -1;
for (int i = 0; i < numEmployees; i++) {
if (employees[i].id == id) {
index = i;
break;
}
}
if (index >= 0) {
// 找到了要查找的职工,显示其信息
struct Employee e = employees[index];
printf("| %-5s | %-10s | %-5s | %-10s | %-20s | %-20s | %-10s |\n", "ID", "姓名", "年龄", "性别", "部门", "职位", "工资");
printf("--------------------------------------------------------------------------\n");
printf("| %-5d | %-10s | %-5d | %-10s | %-20s | %-20s | %-10.2lf |\n", e.id, e.name, e.age, e.gender, e.department, e.position, e.salary);
} else {
printf("未找到要查找的职工!\n");
}
}
void saveToFile() {
FILE* file = fopen("employees.txt", "w");
if (file == NULL) {
printf("无法打开保存文件!\n");
return;
}
for (int i = 0; i < numEmployees; i++) {
struct Employee e = employees[i];
fprintf(file, "%d,%s,%d,%s,%s,%s,%.2lf\n", e.id, e.name, e.age, e.gender, e.department, e.position, e.salary);
}
fclose(file);
printf("职工信息已保存到文件!\n");
}
void readFromFile() {
FILE* file = fopen("employees.txt", "r");
if (file == NULL) {
printf("无法打开读取文件,可能是文件还不存在!\n");
return;
}
char line[256];
while (fgets(line, sizeof(line), file)) {
// 逐行读取职工信息并解析
struct Employee e;
sscanf(line, "%d,%[^,],%d,%[^,],%[^,],%[^,],%lf", &e.id, e.name, &e.age, e.gender, e.department, e.position, &e.salary);
employees[numEmployees] = e;
numEmployees++;
}
fclose(file);
printf("职工信息已从文件读取!\n");
}
```
该代码实现了添加、删除、查找和显示所有职工信息的基本功能,并可以将职工信息保存到文件中,以便下次启动程序时从文件中读取。注意,在实际使用时应该对输入进行一些错误处理,例如对输入的年龄和工资进行范围检查等等。
用c++写一个员工工资管理系统
好的,下面是一个简单的员工工资管理系统的C++代码:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Employee {
string name;
int id;
double salary;
};
vector<Employee> employees;
void addEmployee() {
Employee e;
cout << "Enter employee name: ";
cin >> e.name;
cout << "Enter employee ID: ";
cin >> e.id;
cout << "Enter employee salary: ";
cin >> e.salary;
employees.push_back(e);
}
void displayEmployees() {
cout << "Employees:\n";
for (Employee e : employees) {
cout << e.name << ", ID: " << e.id << ", Salary: $" << e.salary << endl;
}
}
int main() {
bool quit = false;
while (!quit) {
cout << "Menu:\n1. Add employee\n2. Display employees\n3. Quit\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
addEmployee();
break;
case 2:
displayEmployees();
break;
case 3:
quit = true;
break;
default:
cout << "Invalid choice.\n";
break;
}
}
return 0;
}
```
这个程序使用了一个结构体来存储每个员工的姓名、ID和薪资,并将这些员工存储在一个vector中。主函数提供了一个菜单,让用户选择添加员工、显示员工或退出程序。如果用户选择添加员工,程序将提示用户输入员工的姓名、ID和薪资,并将这个员工添加到vector中。如果用户选择显示员工,程序将遍历vector,并显示每个员工的姓名、ID和薪资。如果用户选择退出程序,程序将设置一个标志,退出while循环,然后返回0表示程序成功结束。