话不多说,先上程序:具体细节以后慢慢补!
项目结构:
CMakeLists.txt内容如下:
cmake_minimum_required(VERSION 3.0)
project (census)
include_directories(include)
add_executable(main_cmake main.cpp src/person.cpp src/personManager.cpp)
main.cpp
#include <iostream>
#include <string>
#include "person.hpp"
#include "personManager.hpp"
#include <stdbool.h>
using namespace std;
int main()
{
//实例化管理对象
PersonManager PM;
//定义接收用户的选项
int choice = 0;
bool isRunning = true; // 控制循环的标志
while(isRunning)
{
//调用展示菜单成员
PM.Show_Menu();
cout << "请输入您的选择:" << endl;
cin >> choice; //接受用户的选项
switch (choice)
{
case 0: //退出系统
isRunning = false; // 设置退出条件
PM.ExitSystem();
break;
case 1: //添加职工
PM.Add_person();
break;
case 2: //显示人口信息
PM.displayDetails();
break;
case 3: //显示相同职业
PM.Show_same_position();
break;
default:
system("clear");
break;
}
}
system("read -p '请按任意建继续...' var");
return 0;
}
// system("read -p '请按任意建继续...' var");
// system("clear");
personManager.cpp
#include "personManager.hpp"
using namespace std;
//构造函数
PersonManager::PersonManager()
{
//存放人员的容器
this -> m_personArray = new vector<Person*>;
//记录人员个数
this -> personNum = 0;
}
//展示菜单的具体实现
void PersonManager::Show_Menu()
{
cout << "********************************************" << endl;
cout << "********** 欢迎使用人口统计系统! **********" << endl;
cout << "************* 0.退出系统程序 *************" << endl;
cout << "************* 1.增加人口信息 *************" << endl;
cout << "************* 2.显示人口信息 *************" << endl;
cout << "************* 3.相同职业人口 *************" << endl;
cout << "********************************************" << endl;
cout << endl;
}
//退出系统
void PersonManager::ExitSystem()
{
cout << "欢迎下次使用" << endl;
return; //退出程序
}
//1.添加人口信息
void PersonManager::Add_person()
{
if (!this -> m_personArray)
{
this -> m_personArray = new vector<Person*>; //如果没有容器,现在重新创建
cout << "again vector" << endl;
}
cout << "请输入人员信息:" << endl;
while(true)
{
string name;
int age,sex;
string position;
string workContent;
char flag;
cout << "请输入人员姓名:" << endl;
cin >> name;
while(true)
{
cout << "请输入人员年龄:" << endl;
cin >> age;
if(age > 0 && age < 120)
{
// 用户输入了有效的性别值 (1 或 2),可以退出循环
break;
}
else {
cout << "无效的输入,请重新输入年龄!" << endl;
}
}
while(true)
{
cout << "请输入人员性别:" << endl; // 1-男 2-女
cout << "1-男 2-女" << endl;
cin >> sex;
if(sex == 1 || sex == 2)
{
// 用户输入了有效的性别值 (1 或 2),可以退出循环
break;
}
else {
cout << "无效的输入,请重新输入性别 (1 或 2)" << endl;
}
}
cout << "请输入人员职业:" << endl;
cin >> position;
cout << "请输入人员工作内容:" << endl;
cin >> workContent;
Person* per = nullptr; //Person类指针
per = new Person(name,age,sex,position,workContent);
this ->m_personArray ->push_back(per); //把新增人员放在容器里
this->personNum++; //人数增加
cout << "录入成功!" << endl;
// 更新相同职业人数统计
CountSamePosition(position);
cout << "是否继续录入信息?(y继续录入,n结束录入)" <<endl;
cin >> flag;
if (flag == 'y') continue;
else
{
system("read -p '请按任意建继续...' var");
system("clear");
}
break;
}
}
//2.显示每个人信息
void PersonManager::displayDetails()
{
for(int i = 0; i < this->m_personArray->size();i++)
{
this->m_personArray->at(i)->onePersonInfo();
}
system("read -p '请按任意建继续...' var");
system("clear");
}
// 统计相同职业人数的函数
void PersonManager::CountSamePosition(const string& pos) {
// 检查职业是否已经在统计中
if (positionCount.find(pos) != positionCount.end()) {
// 如果存在,增加人数计数
positionCount[pos]++;
} else {
// 如果不存在,添加新的职业并设置人数为1
positionCount[pos] = 1;
}
}
//3.显示相同职业的人
void PersonManager::Show_same_position()
{
// 显示相同职业的人数统计
cout << "相同职业的人数统计:" << endl;
for (const auto& entry : positionCount) {
cout << "职业: " << entry.first << ", 人数: " << entry.second << endl;
}
system("read -p '请按任意建继续...' var");
system("clear");
}
// 析构函数
PersonManager::~PersonManager()
{
cout << "进来了马?" << endl;
if (this->m_personArray) {
// 使用迭代器遍历 m_personArray 并释放动态分配的 Person 对象
for (auto it = m_personArray->begin(); it != m_personArray->end(); ++it)
{
delete *it;
cout << "析构Person" << endl;
}
this->m_personArray->clear();
delete this->m_personArray;
this->m_personArray = nullptr; // 将指针设置为 NULL
cout << "析构PersonManager" << endl;
}
}
personManager.hpp
#include <vector>
#include <iostream>
#include "person.hpp"
#include <map>
using namespace std;
//管理员类
class PersonManager {
public:
PersonManager();//构造函数
vector<Person*> *m_personArray; //存放增加的人口信息
//退出系统
void ExitSystem();
//显示系统主菜单
void Show_Menu();
//1.添加人口信息
void Add_person();
//2.展示每个人的信息
void displayDetails();
//获取职业
map<string, int> positionCount;
void CountSamePosition(const string& pos);
//3.显示相同职业的人
void Show_same_position();
~PersonManager();//析构函数
//定义vector容器存放 人员信息
int personNum;
};
person.cpp
#include "Person.hpp"
#include <iostream>
using namespace std;
//构造函数
Person::Person(string name,int age,int sex,
string position,string workContent)
{
this ->m_name = name;
this ->m_age = age;
this ->m_sex = sex;
this ->m_position = position;
this ->m_workContent = workContent;
}
void Person::onePersonInfo()
{
std::cout << "姓名: " << m_name << std::endl;
std::cout << "年龄: " << m_age << std::endl;
std::cout << "性别: " << (m_sex == 1 ? "男" : "女") << std::endl;
std::cout << "职业: " << m_position << std::endl;
std::cout << "工作内容: " << m_workContent << std::endl;
std::cout << "-------------------------------------" << std::endl;
}
//析构函数
Person::~Person()
{
}
person.hpp
#ifndef PERSON_HPP
#define PERSON_HPP
#include <string>
using namespace std;
//人员类
class Person {
public:
std::string m_name; //姓名
int m_age; //年龄
int m_sex; //性别
std::string m_position; //职位
std::string m_workContent; //工作内容
public:
//有参构造函数
Person(string name,int age,int sex,
string position,string workContent);
//显示一个人的信息
void onePersonInfo();
~Person();
};
#endif
经过测试,可以完美运行,没有bug!