C++ 计算分数和提问总数
我试图在结束测试函数中输出每个加法、减法、乘法、除法的分数。以及所问问题的总数。 **任何帮助都将不胜感激 顺便说一句,这只是使用本地函数,并通过参考分数。它应该输出正确答案的数量 还有一种方法可以防止程序让您多次选择选项吗 以下是我目前得到的信息:C++ 计算分数和提问总数,c++,C++,我试图在结束测试函数中输出每个加法、减法、乘法、除法的分数。以及所问问题的总数。 **任何帮助都将不胜感激 顺便说一句,这只是使用本地函数,并通过参考分数。它应该输出正确答案的数量 还有一种方法可以防止程序让您多次选择选项吗 以下是我目前得到的信息: #include < iostream > #include < iomanip > #include < stdlib.h > #include < time.h > #include < s
#include < iostream >
#include < iomanip >
#include < stdlib.h >
#include < time.h >
#include < stdlib.h >
using namespace std;
int addition(int addscore);
int subtraction(int subscore);
int multiplication(int multiscore);
int division(int divscore);
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore);
main() {
int end_final = 0;
do {
int addscore, subscore, multiscore, divscore;
char choice;
cout << "A- " << "Addition\n";
cout << "B- " << "Subtraction\n";
cout << "C- " << "Multiplication\n";
cout << "D- " << "Division\n";
cout << "E- " << "End of Test\n";
cin >> choice;
cout << endl << endl;
switch (choice) {
case 'A':
case 'a':
addition(addscore);
break;
case 'B':
case 'b':
subtraction(subscore);
break;
case 'C':
case 'c':
multiplication(multiscore);
break;
case 'D':
case 'd':
division(divscore);
break;
case 'E':
case 'e':
endtest(addscore, subscore, multiscore, divscore);
break;
}
}
while (end_final != 1);
return 0;
}
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
int total = 0;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
addscore++;
} else {
cout << "Incorrect \n";
}
}
}
int subtraction(int subscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen subtraction\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
if (randnum1 >= randnum2) {
cout << randnum1 << " " << "-" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 - randnum2) {
cout << endl;
cout << "Correct!\n ";
subscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int multiplication(int multiscore) {
int iRandom;
int total = 0;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Multiplication\n";
int randnum1, randnum2;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
cout << randnum1 << " " << "x" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 * randnum2) {
cout << endl;
cout << "Correct! \n";
multiscore++;
} else {
cout << "Incorrect\n ";
}
}
}
int division(int divscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Division\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 13 + 1;
randnum2 = rand() % 13 + 1;
if (randnum1 % randnum2 == 0) {
cout << randnum1 << " " << "/" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 / randnum2) {
cout << endl;
cout << "Correct! \n";
divscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore) {
int total = 0;
cout << endl << endl;
cout << "Addition" << " " << addscore++ << endl;
cout << "Subtraction" << " " << subscore << endl;
cout << "Multiplication" << " " << multiscore << endl;
cout << "Division" << " " << divscore << endl;
cout << "Total" << " " << total << endl;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
整数相加(整数相加分数);
整数减法(整数子核);
整数乘法(整数多核);
整数除法(整数除法);
内部测试(内部和添加分数、内部和子分数、内部和多分数、内部和divscore);
main(){
int end_final=0;
做{
int addscore、子核、多核、divscore;
字符选择;
cout要澄清代码,请声明一个枚举
您可以使用全局数组
int used[] = { 0,0,0,0 }; // not used
int total[] = { 0,0,0,0 }; // total questions per operation
int score[] = { 0,0,0,0 }; // score per operation
然后,在do{
if ( ! used[ADDITION]) cout << "A- " << "Addition\n";
// initialize variables to zero
int addscore=0, subscore=0, multiscore=0, divscore=0;
添加代码变为
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; // should not be
score[ADDITION] = 0; // ...necessary (but in case you call it again)
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
score[ADDITION]++; // score is incremented
} else {
cout << "Incorrect \n";
}
}
}
因为这是C++,还可以进行一个类操作,然后每个操作一个子类,保存操作的名称,重写执行特定操作的“执行”方法,然后声明持有每个操作实例的父类数组……/P>
根据OP的评论,在do{
if ( ! used[ADDITION]) cout << "A- " << "Addition\n";
// initialize variables to zero
int addscore=0, subscore=0, multiscore=0, divscore=0;
在开关中
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition( &addscore ); // <== gives the pointer to that variable
break;
案例“A”:
案例“a”:
used[ADDITION]=1;//表示已使用加法
添加(&addscore);//每次输入选项时,您都可以有另一个计数器。相应地递增计数器,然后显示相关消息。您的计算函数从不返回值。您正在递增函数参数。此外,所有分数变量都未初始化,因此使用它们是一种未定义的行为。我对enum不太熟悉:(有没有办法只使用局部函数来实现这一点?你可以用语义相似的int ADDITION=0;int减法=1;…
替换enum。局部函数?如果你是这个意思,你可以在每个函数声明前添加static
,或者是使用全局变量的问题?@ringøYou似乎对此非常了解,但我认为该程序应该是基本的。我可以通过电子邮件向您进一步解释我试图完成的任务吗?请尝试在问题中进行澄清,以便每个人都能看到,也许其他人会有更好的答案。@ringø基本上我的代码中所有内容都正常工作,但如果我选择end test w由于某些原因,在没有运行其他选择的情况下,减法和除法没有初始化为0。因此,当我将每个选择的分数相加时,它会给我一个奇怪的结果
// initialize variables to zero
int addscore=0, subscore=0, multiscore=0, divscore=0;
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition( &addscore ); // <== gives the pointer to that variable
break;
int addition(int *addscore) { // <== gets a pointer
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; //
score[ADDITION] = 0; // using your way, this is not necessary anymore
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
// score[ADDITION]++; // score is incremented (see above)
*addscore++; // <== increment variable pointed to by pointer
} else {
cout << "Incorrect \n";
}
}
}