需求分析: 1、设计一个运算类,里面有四个成员函数,负责进行加减乘除的运算 2、设计一个管理运算类,里面包含两个运算数和运算符号的成员,并且有一个正确答案的成员,这个正确答案的成员的数值由“运算类”的成员函数计算得出 3、使用“管理类”的成员函数访问两个运算数和运算符号让用户计算,然后把用户给出的答案与“管理类”中的正确答案比较,并且判断正误 4、如果用户计算正确,则以追加的形式写入文件
负责进行计算的类
#pragma once
#include<iostream>
using namespace std;
class calculate
{
public: //功能函数
double add(double num1, double num2); //加法
double sub(double num1, double num2); //减法
double mul(double num1, double num2); //乘法
double remove(double num1, double num2); //除法
};
//实现上面类中的方法
#include "calculate.h"
double calculate::add(double num1, double num2) { return num1 + num2; }
double calculate::sub(double num1, double num2) { return num1 - num2; }
double calculate::mul(double num1, double num2) { return num1 * num2; }
double calculate::remove(double num1, double num2) { return num1 / num2; }
管理类
#pragma once
#include<fstream>
#include<ctime>
#include<cmath>
#include"calculate.h"
using namespace std;
#define MAX 20 //简单模式随机数的最大区间
#define MAX1 80 //困难模式随机数的最大区间
#define NUM 3 //选择相应算术后在同算术下的出题数目
#define FILE "text.txt" //文件名称
class manageMent
{
enum Calculate //枚举,便于后面的分支循环
{
add = 1,
sub,
mul,
remove,
help,
Exit,
show
};
public:
manageMent(); //打开文件,读取信息
~manageMent(); //关闭文件
public:
void run(); //启动函数
void menu(); //菜单函数
void sameFun(); //运算函数相同的部分
void writeTxt(); //将信息写入文件
void exitSystem(); //退出系统
void introduce(); //使用说明
void showInformation(); //显示记录
public:
void setSym(char sym); //设置运算符号
double getNum1();
double getNum2();
char getSym();
bool isRight(); //判断计算结果是否正确
void setResult(double result); //设置运算结果
public:
void creatNum(int num); //根据困难程度创建随机数
void creatNum1(int num); //根据困难程度创建两个是否可以整除的数
private:
int m_num1; //操作数1
int m_num2; //操作数2
char m_sym; //运算符号
private:
double m_Tresult; //正确答案
double m_result; //用户计算出的答案
};
//实现管理类的方法
manageMent::manageMent() :m_num1(0), m_num2(0), m_sym(0), m_Tresult(0), m_result(0) {}
manageMent::~manageMent() {}
void manageMent::menu()
{
cout << "********************************************" << endl;
cout << "********************************************" << endl;
cout << "--------------- 算术运算器 -----------------" << endl << endl;
cout << "***** 1、练习加法 + 2、练习减法 - *******" << endl;
cout << "-------------------------------------------" << endl;
cout << "***** 3、练习加法 * 4、练习除法 / *******" << endl;
cout << "-------------------------------------------" << endl;
cout << "***** 5、使用说明 6、退出 *******" << endl;
cout << "-------------------------------------------" << endl;
cout << "************ 7、显示计算记录 ***************" << endl;
cout << "-------------------------------------------" << endl;
cout << "-------------------------------------------" << endl;
cout << "********************************************" << endl << endl;
}
void manageMent::run()
{
int num = 0; //选择分支序号
calculate flag; //创建一个对象便于使用成员函数
this->menu();
while (true) {
int i = NUM; //记录循环次数
cout << "请进行选择:";
while (true) {
cin >> num;
cout << endl;
if (num >= 1 && num <= 7)break;
else {
cout << "选择有误,请重新进行正确的选择:";
}
}
int num1 = 0;
if (num != 6 && num != 5 && num != 7) {
cout << endl;
cout << "请选择模式: 1、简单 2、困难: ";
while (true) {
cin >> num1;
if (num1 == 1 || num1 == 2)break;
else cout << "选择有误,请重新选择:";
}
cout << endl; //便于美观的换行
}
switch (num)
{
case manageMent::add: {
this->setSym('+'); //设置运算符号
while (i) {
this->creatNum(num1);
this->m_Tresult = flag.add(m_num1, m_num2); //计算出正确结果
this->sameFun();
i--;
}
break;
}
case manageMent::sub: {
this->setSym('-');
while (i) {
this->creatNum(num1);
this->m_Tresult = flag.sub(m_num1, m_num2);
this->sameFun();
i--;
}
break;
}
case manageMent::mul: {
this->setSym('*');
while (i) {
this->creatNum(num1);
this->m_Tresult = flag.mul(m_num1, m_num2);
this->sameFun();
i--;
}
break;
}
case manageMent::remove: {
this->setSym('/');
while (i) {
this->creatNum1(num1); //选择难易
this->m_Tresult = flag.remove(m_num1, m_num2); //计算出正确结果
if (num1 == 2) {
this->m_Tresult = (int)(100.0 * m_Tresult + 0.5) / 100.0; //如果选择难,则将正确答案保留两位小数
}
double num1;
while (true) {
cout << "请计算: " << this->getNum1() << " " << this->getSym() << " " << this->getNum2() << " = ";
cin >> num1;
this->setResult(num1);
if (this->isRight()) {
cout << "恭喜你计算正确!" << endl << endl;
this->writeTxt();
system("pause");
system("cls");
this->menu();
break;
}
else {
cout << "算错了哦!请再试一次!" << endl << endl;
system("pause");
system("cls");
this->menu();
}
}
i--;
}
break;
}
case manageMent::help: {
this->introduce();
system("pause");
system("cls");
this->menu();
break;
}
case manageMent::Exit: {
this->exitSystem();
break;
}
case manageMent::show: {
cout << "已计算的题目如下:" << endl << endl;
this->showInformation();
system("pause");
system("cls");
this->menu();
}
}
}
}
void manageMent::sameFun()
{
double num;
while (true) {
cout << "请计算: " << this->getNum1() << " " << this->getSym() << " " << this->getNum2() << " = ";
cin >> num;
this->setResult(num);
if (this->isRight()) {
cout << "恭喜你计算正确!" << endl << endl;
this->writeTxt();
system("pause");
system("cls");
this->menu();
break;
}
else {
cout << "算错了哦!请再试一次!" << endl << endl;
system("pause");
system("cls");
this->menu();
}
}
}
void manageMent::creatNum(int num)
{
srand((unsigned int)time(NULL)); //设置随机数种子,随机数随系统时间变化
if (num == 1) {
this->m_num1 = rand() % MAX + 1.0;
while (true) {
int temp = rand() % MAX + 1.0;
if (temp < this->m_num1 || temp == this->m_num1) {
this->m_num2 = temp;
break;
}
}
}
else {
this->m_num1 = rand() % MAX1 + 20.0;
while (true) {
int temp = rand() % MAX1 + 20.0;
if (temp < this->m_num1 || temp == this->m_num1) {
this->m_num2 = temp;
break;
}
}
}
}
void manageMent::creatNum1(int num)
{
srand((unsigned int)time(NULL));
if (num == 1) { //可整除
this->m_num1 = rand() % MAX + 1.0;
while (true) {
int temp = rand() % MAX + 1.0;
if ((temp < this->m_num1 || temp == this->m_num1) && m_num1 % temp == 0) {
this->m_num2 = temp;
break;
}
}
}
else {
this->m_num1 = rand() % MAX1 + 20.0;
while (true) {
int temp = rand() % MAX1 + 20.0;
if (temp < this->m_num1 || temp == this->m_num1) {
this->m_num2 = temp;
break;
}
}
}
}
void manageMent::writeTxt()
{
fstream ofs;
ofs.open(FILE, ios::in | ios::app); //打开文件
ofs << this->m_num1 << "\t" << this->m_sym << "\t" << this->m_num2 << "\t=\t" << this->m_Tresult << endl;
ofs.close();
}
void manageMent::exitSystem()
{
int num = 0;
cout << "确认退出? 1、是 2、否" << endl;
while (true) {
cin >> num;
if (num == 1 || num == 2)break;
else {
cout << "选择有误,请重新进行选择: ";
}
}
if (num == 1) {
cout << endl << "程序已退出!欢迎下次使用!" << endl;
exit(666);
}
}
void manageMent::introduce()
{
cout << "\t\t\t使用说明\t\t" << endl;
cout << "---------------------------------------------------" << endl;
cout << "1、在选择相应的加减乘除运算后,需要选择相应的难易程度" << endl;
cout << "---------------------------------------------------" << endl;
cout << "2、在选择一个运算后,程序默认会出3道运算符号相同的题目" << endl;
cout << "---------------------------------------------------" << endl;
cout << "3、难易程度说明:" << endl;
cout << "---------------------------------------------------" << endl;
cout << "\t简单模式: 题目的两个运算数在1 - 20之间,并且在除法练习的时候可以整除" << endl;
cout << "-----------------------------------------------------------------------------" << endl;
cout << "\t困难模式: 题目的两个运算数在20 — 100之间,在进行除法练习的时候不一定可以整除(不能整除的将结果保留两位小数)" << endl;
cout << "---------------------------------------------------------------------------------------------------------------------" << endl << endl;
}
void manageMent::showInformation()
{
ifstream ifs;
ifs.open(FILE, ios::in); //以只读的形式打开
if (!ifs.is_open())cout << "文件打开失败!" << endl;
char temp[1024];
while (!ifs.eof()) {
ifs.getline(temp, 1024); //从TXT文件中读取一排数据到temp中
cout << temp << endl;
}
}
void manageMent::setSym(char sym) { this->m_sym = sym; }
double manageMent::getNum1() { return this->m_num1; }
double manageMent::getNum2() { return this->m_num2; }
char manageMent::getSym() { return this->m_sym; }
bool manageMent::isRight() { return this->m_Tresult == this->m_result; } //相同返回真,表示运算正确
void manageMent::setResult(double result) { this->m_result = result; }
主函数
#include<iostream>
#include"manageMent.h"
using namespace std;
int main() {
manageMent a;
a.run();
system("pause");
return 0;
}
程序运行结果如下所示:
标签:manageMent,num1,num2,算术,double,void,temp,运算器 From: https://blog.51cto.com/u_15433911/7665203