1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<random>
using std::cout;
using std::cin;
using std::endl;
double getRandomNumber(int x, int y);
double randomNumberGenerator(int x, int y);
void decAddSub(void);
int main()
{
char repeat = ' ';
do
{
system("cls");
repeat = ' ';
decAddSub();
cout << "Press y to do some more questions or press any key to exit the program ";
cin >> repeat;
} while (repeat == 'y' || repeat == 'Y');
}
double getRandomNumber(int x,int y)
{
double integer = randomNumberGenerator(x,y);
double fraction = randomNumberGenerator(x,y);
double number = integer + fraction / 100;
return number;
}
double randomNumberGenerator(int x, int y)
{
std::random_device rd;
std::default_random_engine generator(rd()); // rd() provides a random seed
std::uniform_int_distribution<int> distribution(x, y);
double number = distribution(generator);
return number;
}
struct question
{
double num1;
double num2;
char operation;
bool correct;
};
void decAddSub(void)
{
int numberOfQuestions(0), count(0); //number of question the user wants to do and the count of how many are correctly answered
question* pquestions(nullptr);
cout << "How many questions would you like to do? ";
cin >> numberOfQuestions;
pquestions = new question[numberOfQuestions];
for (int i = 0; i< numberOfQuestions; i++)
{
if (getRandomNumber(-10,10)<0)
*(pquestions + i) = { getRandomNumber(0, 100), getRandomNumber(0, 100), '-', false };
else
*(pquestions + i) = { getRandomNumber(0, 100), getRandomNumber(0, 100), '+', false };
}
for (int i = 0;i< numberOfQuestions; i++)
{
double answer1(0); //correct answer
double answer2(0); // answer entered by the user
if (pquestions[i].operation == '+')
answer1 = pquestions[i].num1 + pquestions[i].num2;
else if (pquestions[i].operation == '-')
answer1 = pquestions[i].num1 - pquestions[i].num2;
cout << pquestions[i].num1 << ' ' << pquestions[i].operation << ' ' << pquestions[i].num2 << ' ' << '=' << ' ';
cin >> answer2;
if (answer1 == answer2)
{
pquestions[i].correct = true;
cout << "correct \n";
}
else
cout << "incorrect, the correct answer is " << answer1<<endl;
}
for (int i = 0; i < numberOfQuestions; i++)
{
if (pquestions[i].correct)
count++;
}
cout << "You're score is " << count << ' ' << '/' << ' ' << numberOfQuestions << ' ' << 'o' << 'r' << ' ' << count*1.0 / numberOfQuestions* 100*1.0 << '%' << endl;
delete[] pquestions;
pquestions = nullptr;
return;
}
|