C++建立一个人员信息管理系统,要求录入人员信息,包括人的姓名,年龄,显示人员信息,根据姓名和年龄可以查询人员信息,多文件程序,按文件给出代码
时间: 2024-05-03 16:17:35 浏览: 12
以下是一个简单的人员信息管理系统的 C++ 代码,实现了录入、显示、查询等功能。其中使用了 `fstream` 库来进行多文件操作,包括读取和写入文件。
person.h:
```
#ifndef PERSON_H
#define PERSON_H
#include <string>
class Person {
public:
std::string name;
int age;
Person(std::string n, int a);
Person();
};
#endif
```
person.cpp:
```
#include "person.h"
#include <string>
Person::Person(std::string n, int a) : name(n), age(a) {}
Person::Person() {}
```
main.cpp:
```
#include <iostream>
#include <fstream>
#include <vector>
#include "person.h"
using namespace std;
void writeToFile(vector<Person> persons, string filename) {
ofstream fout(filename, ios::out);
if (!fout) {
cout << "Error opening file " << filename << endl;
return;
}
for (Person p : persons) {
fout << p.name << ' ' << p.age << endl;
}
fout.close();
}
vector<Person> readFromFile(string filename) {
vector<Person> persons;
ifstream fin(filename, ios::in);
if (!fin) {
cout << "Error opening file " << filename << endl;
return persons;
}
string name;
int age;
while (fin >> name >> age) {
persons.push_back(Person(name, age));
}
fin.close();
return persons;
}
void addPerson(vector<Person>& persons) {
cout << "Enter name: ";
string name;
cin >> name;
cout << "Enter age: ";
int age;
cin >> age;
persons.push_back(Person(name, age));
cout << "Person added." << endl;
}
void showPersons(vector<Person> persons) {
if (persons.empty()) {
cout << "No person in the list." << endl;
return;
}
for (Person p : persons) {
cout << "Name: " << p.name << ", Age: " << p.age << endl;
}
}
void searchByName(vector<Person> persons) {
if (persons.empty()) {
cout << "No person in the list." << endl;
return;
}
cout << "Enter name to search: ";
string name;
cin >> name;
bool found = false;
for (Person p : persons) {
if (p.name == name) {
cout << "Name: " << p.name << ", Age: " << p.age << endl;
found = true;
}
}
if (!found) {
cout << "Person not found." << endl;
}
}
void searchByAge(vector<Person> persons) {
if (persons.empty()) {
cout << "No person in the list." << endl;
return;
}
cout << "Enter age to search: ";
int age;
cin >> age;
bool found = false;
for (Person p : persons) {
if (p.age == age) {
cout << "Name: " << p.name << ", Age: " << p.age << endl;
found = true;
}
}
if (!found) {
cout << "Person not found." << endl;
}
}
int main() {
vector<Person> persons = readFromFile("persons.txt");
while (true) {
cout << "1. Add person" << endl;
cout << "2. Show all persons" << endl;
cout << "3. Search by name" << endl;
cout << "4. Search by age" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
addPerson(persons);
writeToFile(persons, "persons.txt");
break;
case 2:
showPersons(persons);
break;
case 3:
searchByName(persons);
break;
case 4:
searchByAge(persons);
break;
case 5:
writeToFile(persons, "persons.txt");
return 0;
default:
cout << "Invalid choice." << endl;
break;
}
}
}
```
以上代码实现了以下功能:
- 从文件中读取已有的人员信息
- 添加新的人员信息,并将所有人员信息写入文件
- 显示所有人员信息
- 根据姓名查询人员信息
- 根据年龄查询人员信息
其中,所有人员信息保存在 `vector<Person>` 中,可以方便地进行添加和查询操作。对于文件读写操作,使用了 `fstream` 库,通过 `ofstream` 和 `ifstream` 类实现了文件写入和读取功能。