计算得分和问题总数

时间:2017-11-20 03:30:14

标签: c++

我正在尝试输出最终测试函数中每次加法,减法,乘法,除法的分数。以及问的总数。 **任何帮助将不胜感激

BTW这只是使用本地功能,并通过引用传递分数。 IT应该输出您正确回答的答案数。

还有一种方法可以阻止程序让您多次选择选项吗?

这是我到目前为止所得到的:

#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;

}

1 个答案:

答案 0 :(得分:1)

要澄清代码,请声明枚举

enum { ADDITION=0, SUBTRACTION, MULTIPLICATION, DIVISION };

您可以拥有全局数组

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";

然后在switch

    case 'A':
    case 'a':
      used[ADDITION] = 1; // tells addition was used
      addition(addscore);
      break;

添加代码变为

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";
        }
    }
}

endtest 成为

int endtest() {

   cout << endl << endl;
   if (used[ADDITION]) cout << "Addition" << " " << score[ADDITION] << " / " << total[ADDITION] << endl;

    // same for SUBTRACTION ... 

    int grandtotal = total[ADDITION] + ....;

    cout << "Grand Total" << " " << grandtotal << endl;

}
声明了

endtest ,并像那样调用

int endtest();
...
endtest();

由于这是C ++,你也可以创建一个类Operation,然后每个操作创建一个子类,保存操作的名称并覆盖&#39;执行&#39;执行特定操作的方法,然后声明包含每个操作的实例的父类的数组...

根据OP的评论,在do {

之后
// 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";
        }
    }
}