c++设计全国地区新冠感染人数统计系统(2)

1, 统计系统需求

新冠感染人数统计系统可以用来统计全国部分地区新冠感染人数的信息。
主要利用c++来实现一个基于多态的信息统计系统
主要统计全国部分地区:湖北,广东,浙江,湖南,江西,河南六个省份,后续可以增加其他省份。显示信息时,需要显示地区,日期,新增确诊人数,累计确诊人数,累计治愈人数,累计死亡人数。

统计系统中需要实现的功能如下:

  • 退出统计程序:退出当前系统
  • 增加统计信息:实现批量添加地区统计信息,将信息录入到文件中
  • 显示统计信息:显示所有统计的信息
  • 删除统计信息:按照地区+日趋删除信息
  • 修改统计信息:按照地区修改信息
  • 查找统计信息:按照地区或者日期或者地区+日期查询信息
  • 清空所有文档:清空文件中记录的所有信息(清空前需要再次确认,防止误删)

系统界面效果如下:
在这里插入图片描述

2,创建项目

打开vs2013创建新项目,添加新文件

3,创建统计类

统计类负责内容:

  • 负责界面显示功能
  • 对信息增删改查的功能
  • 对文件的读写操作

3.1 创建文件

在头文件和源文件的文件夹下分别创建StatisticalManage.h和StatisticalManage.cpp文件

3.2头文件实现

#pragma once
#include <iostream>
using namespace std;
class StatisticalManage
{
public:
	//构造函数
	StatisticalManage();
	//析构函数
	~StatisticalManage();
};

3.3 源文件实现

#include "StatisticalManage.h"

//构造函数实现
StatisticalManage::StatisticalManage()
{
}
StatisticalManage::~StatisticalManage()
{
}

4,菜单功能

4.1添加成员函数

在统计类StatisticalManage.h中添加成员函数 void Show_Menu()
在这里插入图片描述

4.2 菜单功能实现

在统计类StatisticalManage.cpp中实现成员函数Show_Menu()

void StatisticalManage::Show_Menu()
{
	cout << "********************************************" << endl;
	cout << "*******  欢迎使用新冠人数统计系统! ********" << endl;
	cout << "*************  0.退出统计程序  *************" << endl;
	cout << "*************  1.增加统计信息  *************" << endl;
	cout << "*************  2.显示统计信息  *************" << endl;
	cout << "*************  3.删除统计信息  *************" << endl;
	cout << "*************  4.修改统计信息  *************" << endl;
	cout << "*************  5.查找统计信息  *************" << endl;
	cout << "*************  6.清空所有文档  *************" << endl;
	cout << "********************************************" << endl;
	cout << endl;

}

5,退出功能

5.1提供功能接口

在main函数中提供分支选择,提供每个功能接口

int main()
{

	StatisticalManage sm;
	int choice = 0;
	while (true)
	{
		//展示菜单
		sm.Show_Menu();
		cout << "请输入您的选择:" << endl;
		cin >> choice;
		switch (choice)
		{
		case 0: //退出系统
			break;
		case 1: //添加信息
			break;
		case 2: //显示信息
			break;
		case 3: //删除信息
			break;
		case 4: //修改信息
			break;
		case 5: //查找信息
			break;
		case 6: //清空文件
			break;
		default:
			break;
		}

	}
	
	system("pause");
	system("cls");
	return 0;
}

5.2 实现退出功能

在统计类StatisticalManage.h中提供退出系统的成员函数void exitSystem()

在统计类StatisticalManage.cpp中实现成员函数exitSystem()

void StatisticalManage::exitSystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

6,创建省份类

6.1创建省份抽象类

省份分类目前只写三类:湖北,广东,浙江,其他的可以添加
将三种省份抽象到一个类(region)中,利用多态管理不同省份的信息
省份的属性有:日期,新增确诊人数,累计确诊人数,累计治愈人数,累计死亡人数,省份编号

头文件文件夹下,创建文件region.h文件并添加如下代码

#pragma once
#include <iostream>
#include <string>
using namespace std;

//地区抽象类
class Region
{
public:
	//纯虚函数,需要重写虚函数
	//显示信息
	virtual void showInfo()=0;  
	//不加等于0将报错,原因是,不加等于0为虚函数,需要在类里面实现
	//加入纯虚函数,因为父类的虚函数都是无意义的,主要都是调用子类重写的内容
	//获取地区名称
	virtual string getRegionName() = 0;

	//省份编号
	int m_regionId;
	//日期
	string m_date;

	//新增确诊人数
	int m_raiseNumber;

	//累计确诊人数
	int m_totalNumber;

	//累计治愈人数
	int m_cureNumber;

	//累计死亡人数
	int m_deathNumber;
};

6.2创建湖北省类

湖北省类继承省份类,并且重写父类中的纯虚函数
在头文件和源文件的文件夹中分别创建Hubei.h和Hubei.cpp文件

Hubei.h的代码

#pragma once
#include <iostream>
using namespace std;
#include "region.h"

//湖北省类
//公有继承,将继承父类的所有属性,不可访问私有属性,
//父类的保护属性在子类爱是保护属性
class Hubei :public Region
{
public:
	//构造函数
	Hubei(string date, int raiseNumber, int totalNumber, int cureNumber, int deathNumber, int regionId);
	virtual void showInfo(); //virtual 关键字可以省略,是重写父类的函数
	virtual string getRegionName();
};

Hubei.cpp代码

#include "hubei.h"

Hubei::Hubei(string date, int raiseNumber, int totalNumber, int cureNumber, int deathNumber, int regionId)
{
	this->m_date = date;
	this->m_raiseNumber = raiseNumber;
	this->m_totalNumber = totalNumber;
	this->m_cureNumber = cureNumber;
	this->m_deathNumber = deathNumber;
	this->m_regionId = regionId;

}


void Hubei::showInfo()
{
	cout << "省份:" << this->getRegionName()
		<< "\t日期:" << this->m_date
		<< "\t新增确诊人数:" << this->m_raiseNumber
		<< "\t累计确诊人数:" << this->m_totalNumber
		<< "\t累计治愈人数:" << this->m_cureNumber
		<< "\t累计死亡人数:" << this->m_deathNumber << endl;
}


string Hubei::getRegionName()
{
	return string("湖北");
}

6.3 创建广东省类和浙江省类

同创建湖北省类一样
见代码

#pragma once
#include <iostream>
using namespace std;
#include "region.h"

//广东省类
//公有继承,将继承父类的所有属性,不可访问私有属性,
//父类的保护属性在子类爱是保护属性
class Guangdong :public Region
{
public:
	//构造函数
	Guangdong(string date, int raiseNumber, int totalNumber, int cureNumber, int deathNumber, int regionId);
	virtual void showInfo(); //virtual 关键字可以省略,是重写父类的函数
	virtual string getRegionName();
};
#include "guangdong.h"

Guangdong::Guangdong(string date, int raiseNumber, int totalNumber, int cureNumber, int deathNumber, int regionId)
{
	this->m_date = date;
	this->m_raiseNumber = raiseNumber;
	this->m_totalNumber = totalNumber;
	this->m_cureNumber = cureNumber;
	this->m_deathNumber = deathNumber;
	this->m_regionId = regionId;

}


void Guangdong::showInfo()
{
	cout << "省份:" << this->getRegionName()
		<< "\t日期:" << this->m_date
		<< "\t新增确诊人数:" << this->m_raiseNumber
		<< "\t累计确诊人数:" << this->m_totalNumber
		<< "\t累计治愈人数:" << this->m_cureNumber
		<< "\t累计死亡人数:" << this->m_deathNumber << endl;
}


string Guangdong::getRegionName()
{
	return string("广东");
}
#pragma once
#include <iostream>
using namespace std;
#include "region.h"

//浙江省类
//公有继承,将继承父类的所有属性,不可访问私有属性,
//父类的保护属性在子类爱是保护属性
class Zhejiang :public Region
{
public:
	//构造函数
	Zhejiang(string date, int raiseNumber, int totalNumber, int cureNumber, int deathNumber, int regionId);
	virtual void showInfo(); //virtual 关键字可以省略,是重写父类的函数
	virtual string getRegionName();
};
#include "zhejiang.h"

Zhejiang::Zhejiang(string date, int raiseNumber, int totalNumber, int cureNumber, int deathNumber, int regionId)
{
	this->m_date = date;
	this->m_raiseNumber = raiseNumber;
	this->m_totalNumber = totalNumber;
	this->m_cureNumber = cureNumber;
	this->m_deathNumber = deathNumber;
	this->m_regionId = regionId;

}


void Zhejiang::showInfo()
{
	cout << "省份:" << this->getRegionName()
		<< "\t日期:" << this->m_date
		<< "\t新增确诊人数:" << this->m_raiseNumber
		<< "\t累计确诊人数:" << this->m_totalNumber
		<< "\t累计治愈人数:" << this->m_cureNumber
		<< "\t累计死亡人数:" << this->m_deathNumber << endl;
}


string Zhejiang::getRegionName()
{
	return string("浙江");
}

7 添加统计信息

批量添加信息,并且保存到文件中

在StatisticalManage.h头文件中添加成员属性

//记录文件中统计数据的条数
	int m_EmpNum;

	//省份数组的指针
	Region ** m_EmpArray;

在StatisticalManage.cpp构造函数中初始化属性

StatisticalManage::StatisticalManage()
{
	//初始化记录的条数
	this->m_EmpNum = 0;
	//初始化数组指针
	this->m_EmpArray = NULL;

}

在StatisticalManage.h中添加成员函数void Add_Emp()
在StatisticalManage.cpp中实现成员函数Add_Emp()

void StatisticalManage::Add_Emp()
{
	cout << "请输入增加统计的数量:" << endl;
	int addNum = 0;
	cin >> addNum;
	if (addNum > 0)
	{
		//计算新空间大小
		int newSize = this->m_EmpNum + addNum;

		//开辟新空间
		Region ** newspace = new Region*[newSize];

		//将原空间下内容存放到新空间下
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				newspace[i] = this->m_EmpArray[i];
			}
		}
		//输入新数据
		for (int i = 0; i < addNum; i++)
		{
			//日期
			string date;
			//新增确诊人数
			int raiseNumber;
			//累计确诊人数
			int totalNumber;
			//累计治愈人数
			int cureNumber;
			//累计死亡人数
			int deathNumber;
			int dSelect;

			cout << "请输入第" << i + 1 << "个数据的日期" << endl;
			cin >> date;
			cout << "请输入第" << i + 1 << "个数据的新增确诊人数" << endl;
			cin >> raiseNumber;
			cout << "请输入第" << i + 1 << "个数据的累计确诊人数" << endl;
			cin >> totalNumber;
			cout << "请输入第" << i + 1 << "个数据的累计治愈人数" << endl;
			cin >> cureNumber;
			cout << "请输入第" << i + 1 << "个数据的累计死亡人数" << endl;
			cin >> deathNumber;

			cout << "请选择省份:" << endl;
			cout << "1,湖北" << endl;
			cout << "2,广东" << endl;
			cout << "3,浙江" << endl;
			cin >> dSelect;
			Region * region = NULL;
			switch (dSelect)
			{
			case 1:  //湖北
				region = new Hubei(date, raiseNumber, totalNumber, cureNumber, deathNumber, 1);
				break;
			case 2: //广东
				region = new Guangdong(date, raiseNumber, totalNumber, cureNumber, deathNumber, 2);
				break;
			case 3: //浙江
				region = new Zhejiang(date, raiseNumber, totalNumber, cureNumber, deathNumber, 3);
				break;
			default:
				break;
			}
			

			newspace[this->m_EmpNum + i] = region;

		}
		//释放原有空间
		delete[] this->m_EmpArray;
		//更改新空间指向
		this->m_EmpArray = newspace;
		//更新新的数据条数
		this->m_EmpNum = newSize;
		
		//提示信息
		cout << "添加成功" << addNum << "条数据" << endl; 

	}
	else
	{
		cout << "输入有误" << endl;

	}
	system("pause");
	system("cls");
}

在StatisticalManage.cpp析构函数中释放堆区数据

//析构函数
StatisticalManage::~StatisticalManage()
{
	if (this->m_EmpArray != NULL)
	{
		delete[] this->m_EmpArray;
		m_EmpArray = NULL;
	}
}

8 文件交互-写文件

功能描述:对文件进行读写

​ 在上一个添加功能中,我们只是将所有的数据添加到了内存中,一旦程序结束就无法保存了

​ 因此文件管理类中需要一个与文件进行交互的功能,对于文件进行读写操作

8.1 设定文件路径

首先我们将文件路径,在StatisticalManage.h中添加宏常量,并且包含头文件 fstream

#include <fstream>
#define  FILENAME "empFile.txt"

8.2 成员函数声明

在StatisticalManage.h中类里添加成员函数 void save()

//保存文件
void save();

8.3 保存文件功能实现


//保存文件
void StatisticalManage::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out); //用输出的方式打开文件  --写文件
	for (int i = 0; i < this->m_EmpNum; i++)
	{
		ofs << this->m_EmpArray[i]->m_date << "  "
			<< this->m_EmpArray[i]->m_raiseNumber << "  "
			<< this->m_EmpArray[i]->m_totalNumber << "  "
			<< this->m_EmpArray[i]->m_cureNumber << "  "
			<< this->m_EmpArray[i]->m_deathNumber << "  "
			<< this->m_EmpArray[i]->m_regionId << endl;

	}
	ofs.close();

}

8.4 保存文件功能测试

在添加职工功能中添加成功后添加保存文件函数
在这里插入图片描述

运行代码
在这里插入图片描述
在这里插入图片描述

其他功能之后再实现!!!

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值