class Question{
protected:
int op1;
int op2;
string operate;
public:
Question();
};
class generateRandomQuiz:Question{
public:
generateRandomQuiz();
int getp1();
int getp2();
string getOp();
};
class checkAnswer:generateRandomQuiz{
private:
int Ans;
public:
checkAnswer(int ans);
};
Question::Question()
{
op1=23;
op2=12;
operate="/";
}
generateRandomQuiz::generateRandomQuiz():Question()
{
op1=rand()%50;
op2=rand()%50;
string s="+-/*";
int n=rand()%4;
operate=s[n];
}
int generateRandomQuiz::getp1()
{
return op1;
}
int generateRandomQuiz::getp2()
{
return op2;
}
string generateRandomQuiz::getOp()
{
return operate;
}
checkAnswer::checkAnswer(int ans):generateRandomQuiz()
{
Ans=ans;
string operate=getOp();
int op1=getp1();
int op2=getp2();
if (operate=="+")
{
if (op1+op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
if (operate=="-")
{
if (op1-op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
if (operate=="*")
{
if (op1*op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}if (operate=="/")
{
if (op1/op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
}
int main()
{
cout<<"This quiz is about evaluating an expression which is being generatedrandomly"
<<endl;
generateRandomQuiz Q;
int answer;
int op1=Q.getp1();
int op2=Q.getp2();
string opr=Q.getOp();
cout<<"What is: "<<op1<<op2<<op2<<"=?"<<endl;
cin>>answer;
checkAnswer A(answer);
system("PAUSE");
return 0;
}
クイズをランダムに生成し、ユーザーに次のような回答を求めるプログラムを作成しています: What is : 15 / 43 = ? 演算子と数字はランダムに生成されます.しかし、私が正解を与えると、間違った答えに対するコメントが出力されます.私は条件を非常に明確に書きました.誰かがそれを指摘できますか?ありがとう