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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
//quadquizimp.cpp
#include "QuadQuiz.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include <ctime>
#include <string>
using namespace std;
ofstream fout("QuadQuizOut.txt");
void QuadQuiz::title(){
cout << "============== Q U A D R A T I C F A C T O R I N G Q U I Z ==============\n\n";
fout << "============== Q U A D R A T I C F A C T O R I N G Q U I Z ==============\n\n";
cout << " by\n";
fout << " by\n";
cout << " Robert VanCleave\n";
fout << " Robert VanCleave\n";
cout << "=================================================================================\n";
fout << "=================================================================================\n";
cout << "You will be given a quadratic expression, for example (x^2 - 17x + 72), to factor.\n";
fout << "You will be given a quadratic expression, for example (x^2 - 17x + 72), to factor.\n";
cout << "To continue with this example, in this expression the factors are (x - 9) (x - 8)\n";
fout << "To continue with this example, in this expression the factors are (x - 9) (x - 8)\n";
cout << "You would enter the constants, separated by a space: -9 -8 (in either order)";
fout << "You would enter the constants, separated by a space: -9 -8 (in either order)";
cout << "=================================================================================\n\n";
fout << "=================================================================================\n\n";
cout << "How many problems are we solving today? ";
fout << "How many problems are we solving today? ";
cin >> myProblems;
fout << myProblems << endl;
cout << "=================================================================================\n\n";
fout << "=================================================================================\n\n";
}
void QuadQuiz::process(){
int factor1[] = {-2, -2, -7, 5, -6, -25, 7, -2, -1, -9, -4, -3};
int factor2[] = {-5, -3, -3, -3, -2, 4, 11, 10, 7, -2, 4, 3};
int product[] = {10, 6, 21, -15, 12,-100, 77, -20, -7, 18, -16, -9};
int sum[] = {-7, -5, -10, 2, -8, -21, 18, 8, 6, -11, 0, 0};
bool picked[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
numCorrect=0;
for (int i=0; i<myProblems; i++) {
int index=getUniqueNum(picked);
displayProblem(index, sum, product);
if (checkCorrect(index, sum, product)) {
cout << right << setw(25) << "Correct!";
displayFactors(index,factor1,factor2);
numCorrect++;
}
if (!checkCorrect(index, sum, product)) {
cout << right << setw(25) << "Incorrect, sorry.";
displayFactors(index,factor1,factor2);
}
}
cout << "\n";
fout << "\n";
cout << "You got " << numCorrect << " questions correct out of a total of " << myProblems << " questions.\n";
fout << "You got " << numCorrect << " questions correct out of a total of " << myProblems << " questions.\n";
}
bool QuadQuiz::checkCorrect(int index, int sum[], int product[]) const {
int answerOne;
int answerTwo;
cin >> answerOne >> answerTwo;
fout<< answerOne << " " << answerTwo << endl;
if (answerOne+answerTwo==sum[index] && answerOne*answerTwo==product[index]) {
return true;
}
else {
return false;
}
}
void QuadQuiz::displayFactors(int index, int factor1[], int factor2[]) const {
if (factor1[index] >= 0 && factor2[index] >= 0) {
cout << left << setw(20) << " (x + " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
fout << left << setw(20) << " (x + " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
}
if (factor1[index] >= 0 && factor2[index] < 0) {
cout << left << setw(20) << " (x + " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
fout << left << setw(20) << " (x + " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
}
if (factor1[index] < 0 && factor2[index] >= 0) {
cout << left << setw(20) << " (x - " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
fout << left << setw(20) << " (x - " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
}
if (factor1[index] < 0 && factor2[index] < 0) {
cout << left << setw(20) << " (x - " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
fout << left << setw(20) << " (x - " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
}
}
void QuadQuiz::displayProblem(int index, int sum[], int product[]) const {
cout << right << setw(25) << "Quadratic";
fout << right << setw(25) << "Quadratic";
if (sum[index] >= 0 && product[index] >= 0) {
cout << left << setw(20) << " (x^2 + " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
fout << left << setw(20) << " (x^2 + " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
}
if (sum[index] < 0 && product[index] < 0) {
cout << left << setw(20) << " (x^2 - " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
fout << left << setw(20) << " (x^2 - " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
}
if (sum[index] >= 0 && product[index] < 0) {
cout << left << setw(20) << " (x^2 + " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
fout << left << setw(20) << " (x^2 + " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
}
if (sum[index] < 0 && product[index] >= 0) {
cout << left << setw(20) << " (x^2 - " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
fout << left << setw(20) << " (x^2 - " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
}
cout << left << setw(16) << "Factors a & b: ";
fout << left << setw(16) << "Factors a & b: ";
}
int QuadQuiz::getUniqueNum(bool picked[]) const {
int index = -1;
bool done = false;
while(!done){
index = (rand() % 12);
if(picked[index] == false){
picked[index] = true;
done = 1;
}
}
return index;
}
|