用C++写一个简单系统,要求包括: 员工档案:工号,姓名,性别,年龄,增加员工信息,菜品信息 修改员工信息,修改菜品信息;删除员工信息,删除菜品信息,删除顾客信息 查询菜品信息,查询员工信息,将菜品按价格排序;并用文件保存信息,读取数据,运用数组,类,模板,不用链表指针
时间: 2023-12-14 16:04:39 浏览: 86
简单的基于C++的员工管理系统
好的,这是一个比较复杂的问题,但我会尽力回答。首先,我们可以使用类来表示员工和菜品信息,如下所示:
```c++
class Employee {
private:
int id;
string name;
char gender;
int age;
public:
Employee(int id, string name, char gender, int age) {
this->id = id;
this->name = name;
this->gender = gender;
this->age = age;
}
// getter and setter functions
};
class Dish {
private:
string name;
double price;
public:
Dish(string name, double price) {
this->name = name;
this->price = price;
}
// getter and setter functions
};
```
然后我们可以使用数组来存储员工和菜品信息,如下所示:
```c++
const int MAX_EMPLOYEES = 1000;
const int MAX_DISHES = 1000;
Employee employees[MAX_EMPLOYEES];
int numEmployees = 0;
Dish dishes[MAX_DISHES];
int numDishes = 0;
```
接下来,我们可以编写一些函数来添加、修改、删除和查询员工和菜品信息。这些函数可以使用模板来处理不同类型的数据,如下所示:
```c++
// add a new item to the array
template <typename T>
void addItem(T item, T* array, int& numItems, const int MAX_ITEMS) {
if (numItems < MAX_ITEMS) {
array[numItems++] = item;
} else {
cout << "Error: array is full" << endl;
}
}
// update an existing item in the array
template <typename T>
void updateItem(int index, T item, T* array, int numItems) {
if (index >= 0 && index < numItems) {
array[index] = item;
} else {
cout << "Error: index out of range" << endl;
}
}
// remove an item from the array
template <typename T>
void removeItem(int index, T* array, int& numItems) {
if (index >= 0 && index < numItems) {
for (int i = index; i < numItems - 1; i++) {
array[i] = array[i+1];
}
numItems--;
} else {
cout << "Error: index out of range" << endl;
}
}
// find an item in the array by name
template <typename T>
int findItem(string name, T* array, int numItems) {
for (int i = 0; i < numItems; i++) {
if (array[i].getName() == name) {
return i;
}
}
return -1;
}
// sort the dishes by price in ascending order
void sortDishesByPrice() {
for (int i = 0; i < numDishes - 1; i++) {
for (int j = i + 1; j < numDishes; j++) {
if (dishes[i].getPrice() > dishes[j].getPrice()) {
Dish temp = dishes[i];
dishes[i] = dishes[j];
dishes[j] = temp;
}
}
}
}
```
最后,我们可以使用文件来保存和读取数据。我们可以使用流运算符 `<<` 和 `>>` 来将对象写入文件和从文件中读取对象,如下所示:
```c++
void saveEmployees() {
ofstream file("employees.txt");
for (int i = 0; i < numEmployees; i++) {
file << employees[i];
}
file.close();
}
void loadEmployees() {
ifstream file("employees.txt");
while (file) {
int id;
string name;
char gender;
int age;
file >> id >> name >> gender >> age;
Employee employee(id, name, gender, age);
addItem(employee, employees, numEmployees, MAX_EMPLOYEES);
}
file.close();
}
void saveDishes() {
ofstream file("dishes.txt");
for (int i = 0; i < numDishes; i++) {
file << dishes[i];
}
file.close();
}
void loadDishes() {
ifstream file("dishes.txt");
while (file) {
string name;
double price;
file >> name >> price;
Dish dish(name, price);
addItem(dish, dishes, numDishes, MAX_DISHES);
}
file.close();
}
// stream operators for Employee and Dish classes
ostream& operator<<(ostream& os, const Employee& employee) {
os << employee.getId() << " " << employee.getName() << " "
<< employee.getGender() << " " << employee.getAge() << endl;
return os;
}
istream& operator>>(istream& is, Employee& employee) {
int id;
string name;
char gender;
int age;
is >> id >> name >> gender >> age;
employee.setId(id);
employee.setName(name);
employee.setGender(gender);
employee.setAge(age);
return is;
}
ostream& operator<<(ostream& os, const Dish& dish) {
os << dish.getName() << " " << dish.getPrice() << endl;
return os;
}
istream& operator>>(istream& is, Dish& dish) {
string name;
double price;
is >> name >> price;
dish.setName(name);
dish.setPrice(price);
return is;
}
```
这是一个简单的系统,它使用了类、数组、模板和文件。当然,这只是一个示例,您可以根据自己的需求进行修改和扩展。
阅读全文