1

好的,所以我正在创建一个数学测验。现在只是一些基本的东西,但是在我的 while 循环结束时,我想累积输出,这意味着如果你做 5 个加法问题,输出将存储所有 5 个问题的表达式,并使用条件运算符来判断答案是正确的还是错误的,它会显示在最后。

我在 Java 中有相同的程序,但我想将它切换到 C++,因为我真的很喜欢 C++,并且想要更多它,这就是我想弄清楚这个的原因。

爪哇:

output += "\n" + number1 + " - " + number2 + " = " + answer + ((number1 - number2 == answer) ? " CORRECT" : " WRONG");

我的 while 循环带有 C++ 中的输出累加器:

while (count <= NUMBER_OF_QUESTIONS) {

    num1 = 1 + rand() % 50;
    num2 = 1 + rand() % 50;

    if (num1 < num2) {
        temp = num2;
        num2 = num1;
        num1 = temp;
    }

    cout << "\n"<< num1 << " + " << num2 << " = " << endl;
    cin >> answer;

    if (num1 + num2 == answer) {
        cout << "Right!" << endl;
        correctCount++;
    }
    else
        cout << "Wrong! Should be " << (num1 + num2) << endl;

// Increase count
count++;

    // Prepare all questions if correct or wrong, for output
    output += // The rest...
} 

//and for final output

cout << output;
4

2 回答 2

0

不使用任何 STL 库:只需将变量存储在数组中。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>


#define NUMBER_OF_QUESTIONS 5

int main()
{
    srand ( time(NULL) );
    int count = 0;
    //Init a
    int output[NUMBER_OF_QUESTIONS][4];

    while (count <= NUMBER_OF_QUESTIONS) {

        int num1 = 1 + rand() % 50;
        int num2 = 1 + rand() % 50;
        int answer = 0;

        if (num1 < num2) {
            int temp = num2;
            num2 = num1;
            num1 = temp;
        }

        std::cout << "\n"<< num1 << " + " << num2 << " = " << std::endl;
        std::cin >> answer;

        if (num1 + num2 == answer) {
            std::cout << "Right!" << std::endl;
            //correctCount++;
        }
        else
        {
            std::cout << "Wrong! Should be " << (num1 + num2) << std::endl;
        }

        // Prepare all questions if correct or wrong, for output
        output[count][0] = num1;
        output[count][1] = num2;
        output[count][2] = answer;
        output[count][3] = (num1 + num2 == answer);

        // Increase count
        count++;
    } 
    for (int i =0; i < NUMBER_OF_QUESTIONS; i++)
    {
        std::cout << "Q" << i << " : " ;
        std::cout << output[i][0] << " + " << output[i][1];
        std::cout << " = " << output[i][2] << " . ";
        if (output[i][3])
            std::cout << "Right Answer ! " << std::endl;
        else
            std::cout << "Wrong! Should be " << (output[i][0] +  output[i][1]) << std::endl;

    }
}
于 2012-11-27T12:07:18.180 回答
0

所以,如果我没看错的话,你想遍历一下让我们说 5 个问题。
所以你会有类似...
5+12 =
用户输入
13+32 =
用户输入
31 + 1 =
用户输入
27+ 15 =
用户输入
11+11 =
用户输入

然后程序将输出以下内容:

5+12 = 17
正确
13+32 = 45
正确
31 + 1 = 33
错误
27+ 15 = 42
正确
11+11 = 11
错误

如果这就是你想要的,那么你可以尝试这样的事情:

#define MAX_QUESTIONS 5
#define OPERANDS 2
#define LEFT 0
#define RIGHT 1 
int main()
{
   int ques[MAX_QUESTIONS][OPERANDS];
   int ans[MAX_QUESTIONS];
   for(int i=0; i < MAX_QUESTIONS; i++)
   {
      ques[i][LEFT] = 1 + rand() % 50;
      ques[i][RIGHT] = 1 + rand() % 50;
      if (ques[i][LEFT] < ques[i][RIGHT]) 
      {
         int temp = ques[i][RIGHT];
         ques[i][RIGHT] = ques[i][LEFT];
         ques[i][LEFT] = temp;
      }
      cout << "\n" << ques[i][LEFT] << " + " << ques[i][RIGHT] << " = " << endl;
      cin >> answer[i];
   }

   for(int i = 0; i < MAX_QUESTIONS; i++)
   {
      cout << "\n" << ques[i][LEFT] << " + " << ques[i][RIGHT] << " = " << answer[i] << "\n" << (((ques[i][LEFT] + ques[i][RIGHT]) == answer[i])?"CORRECT":"WRONG") << endl;
   }
}
于 2012-11-27T12:07:53.273 回答