#include
using namespace std;
#include
#include
//定义表达式
struct Expression
{
int iFirst; //第一个操作数
int iSecond; //第二个操作数
char oper; //操作
int result; //结果
//初始化
Expression()
{
iFirst = 0;
iSecond = 0;
oper = 0;
result = 0;
}
};
//随机数的上下限
int low = 0,up = 0;
//vector,存储生成的表达式
vector
//生成该操作类的表达式的函数
void SetExpression(char oper);
int main()
{
cout<<"请输入随机数的下限和上限!"<
if (low > up)
{
cout<<"下限大于上限,已自动转换!"<
int temp = low;
low = up;
up = temp;
}
srand(time(NULL));
//随机生成十道题目
char operSymb[4] = {'+','-','*','/'};
char oper;
for (int i = 0; i < 10; ++i)
{
oper = operSymb[rand()%4];
SetExpression(oper);
}
cout<<"本次测试一共十道题目,请依次作答!"<
int Score = 0;
//迭代器依次访问vector数组元素
vector
for (iter; iter != vector_Expression.end(); ++iter)
{
cout<
int result;
cin>>result;
if (result != iter->result)
cout<<"很遗憾回答错误,本题得分:0,正确答案为"<
{
cout<<"恭喜你回答正确,本题得分:10"<
Score += 10;
}
cout<
cout<<"测试结束,你总共得分为:"<
}
void SetExpression(char oper)
{
int iFirst = 0;
int iSecond = 0;
//获取两个操作数
if (oper != '/')
{
//生成两数
iFirst = rand()%(up - low) + low;
iSecond = rand()%(up - low) + low;
if (oper == '-' && iFirst < iSecond)
{
int temp = iSecond;
iSecond = iFirst;
iFirst = temp;
}
}
else if (oper == '/')
{
//最大循环次数,防止死循环
int count = 0;
while (1)
{
count++;
if (count == 10000)
break;
iFirst = rand()%(up - low) + low;
iSecond = rand()%(up - low) + low;
if (iFirst < iSecond)
{
int temp = iSecond;
iSecond = iFirst;
iFirst = temp;
}
if (iSecond == 0)
continue;
if ((iFirst % iSecond == 0))
break;
}
//若到了循环上限,那么就重新赋予一个表达式
if (count == 10000)
{
char c[3] = {'+','-','*'};
SetExpression(c[rand()%3]);
return;
}
}
//构造表达式,并加入容器
Expression info;
info.iFirst = iFirst;
info.iSecond = iSecond;
info.oper = oper;
info.result = iFirst / iSecond;
if (oper == '+')
info.result = iFirst + iSecond;
else if (oper == '-')
info.result = iFirst - iSecond;
else if (oper == '*')
info.result = iFirst * iSecond;
else if (oper == '/')
info.result = iFirst / iSecond;
vector_Expression.push_back(info);
}
这只是一个控制台程序,很单一,如果觉得可以就拿去用吧!复制粘贴就可以运行了。