【发布时间】:2019-07-03 07:49:42
【问题描述】:
所以我正在创建一个程序,我必须在其中创建具有随机数和运算符的随机问题集。我制作随机数没有问题。但是,我对如何随机化我需要使用的三个运算符(加法、减法和乘法)感到困惑。我知道我必须使用数字来表示这三个运算符,但我不明白该怎么做。我必须使用随机数生成器来执行此操作以及 If & Then 语句。这是我的源代码。
我尝试创建一个名为 "const int MAXOP_VALUE = 3" 的单独常量。我被困在之后该怎么做。如何将加法、减法和乘法运算符表示为数字?
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
/*Constants*/
const int MIN_VALUE = 1;
const int MAX_VALUE = 100;
/*Variables*/
int number_1;
int number_2;
int math_op;
/*Get the System Time*/
unsigned seed = time(0);
/*Seed the Random Number Generator*/
srand(seed);
/*Generates Random Numbers for the Math Problems*/
number_1 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
number_2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
/*Answer to Problem*/
/*Explains How the Program Works*/
cout << "****************************************" << endl << endl;
cout << "Welcome to the awesome math tutor! \n";
cout << "Get ready to add, subtract, and multiply!" << endl << endl;
cout << "****************************************" << endl << endl;
cout << "How much is" << number_1 << math_op << number_2 << "?" <<
endl;
return 0;
}
我希望输出如下所示: “25+42是什么?” “什么是 54*3?” “什么是 76-2?”
【问题讨论】:
-
你应该使用
iso Rand(),见stackoverflow.com/q/32927722/2466431
标签: c++