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 100 101 102 103 104 105
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void getProbsPerSet(int); // let user choose how many time probs
void doOneSet(char, int); // do one set of each problem
void doOneProblem(int&); // ask user the answer
void checkAnswer(int, int); // compare the answer if it's right
void generateOperands(char&); // declare which calculation it needs
void CalculateCorrectAnswer(int, int, char, int&); // Find correct answer
int main() {
int probsPerSet;
getProbsPerSet(probsPerSet); // calling out getProbsPerSet
srand(static_cast<unsigned>(time(0))); // generate randome number
doOneSet('+', probsPerSet); // calling out doOneSet with character +
doOneSet('-', probsPerSet); // calling out doOneSet with character -
doOneSet('*', probsPerSet); // calling out doOneSet with character *
system("pause");
return 0;
}
void getProbsPerSet(int probs) { // Many will have fixed value now
int probsPerSet;
cout << "Enter problems per set: ";
cin >> probsPerSet; // return Many as typed value
return probsPerSet;
}
void doOneSet(char Function, int Many) { // catches int Many, char Function from follwing than do doOneset
int i;
if (Function == '+') { // if when char +
for (i = 0; i < Many; i++) { // repeat with following Many
int First, Second, Answer, Final;
First = rand() % 101; // generate randome number under 100
Second = rand() % 101; // generate randome number under 100
cout << First; // show first generated number
generateOperands(Function); // show which function we are in
cout << Second << " = "; // show second function and =
doOneProblem(Answer); // ask for user to put value as answer
CalculateCorrectAnswer(First, Second, Function, Final); // get the correct answer
checkAnswer(Answer, Final); // compare with use's answer and right answer and show if it's right or wrong
}
}
if (Function == '-') { // if when char -
for (i = 0; i < Many; i++) { // repeat with following Many
int First, Second, Answer, Final;
First = rand() % 101; // generate randome number under 100
Second = rand() % 101; // generate randome number under 100
cout << First; // show first generated number
generateOperands(Function); // show which function we are in
cout << Second << " = "; // show second function and =
doOneProblem(Answer); // ask for user to put value as answer
CalculateCorrectAnswer(First, Second, Function, Final); // get the correct answer
checkAnswer(Answer, Final); // compare with use's answer and right answer and show if it's right or wrong
}
}
if (Function == '*') { // if when char *
for (i = 0; i < Many; i++) { // repeat with following Many
int First, Second, Answer, Final;
First = rand() % 101; // generate randome number under 100
Second = rand() % 101; // generate randome number under 100
cout << First; // show first generated number
generateOperands(Function); // show which function we are in
cout << Second << " = "; // show second function and =
doOneProblem(Answer); // ask for user to put value as answer
CalculateCorrectAnswer(First, Second, Function, Final); // get the correct answer
checkAnswer(Answer, Final); // compare with use's answer and right answer and show if it's right or wrong
}
}
}
void doOneProblem(int& Ans) {
cin >> Ans;
}
void CalculateCorrectAnswer(int X, int Y, char F, int& A) { // generate correct answer with givin value X,Y with given function F than return A
if (F == '+') {
A = X + Y;
}
if (F == '-') {
A = X - Y;
}
if (F == '*') {
A = X * Y;
}
}
void checkAnswer(int Ans, int A) { //compare right answer than user's answer than answer if it's right or wrong
if (Ans == A) {
cout << "Correct" << endl;
}
else {
cout << "Incorrect" << endl;
}
}
void generateOperands(char& F) { // just little trick for generate fixed function
cout << " " << F << " ";
}
|