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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
char Menu(); //prototypes
void difficulty(int &);
void generate(double &, double &, int );
void addition(double &, double, double);
void subtraction(double &, double, double);
void multiplication(double &, double, double);
void division(double &, double, double, char);
void Ans(double &);
int main()
{
double correctans, num1, num2;
char choice;
char line[80];
int diff;
time_t now = time(0);
string name;
srand(now); // Seed the random number generator.
//cout << ctime(&now) << endl;
cout << "what is your first and last name " << endl;
getline(cin, name);
for (unsigned i = 0; i < name.length(); i++)
if (name[i] == ' ') name[i] = '_';
name += ".txt";
cout << endl << endl << endl;
cout << "Welcome to the Math Skills Program " << endl;
cout << "This program allows you to practice your math skills" << endl;
cout << "Choose what to practice in the menu shown below." << endl;
cout << endl << endl << endl;
difficulty( diff);
bool test = true;
while (test)
{
now = time(0);
// cout << endl << "Problem start time:" << ctime(&now) << endl;
generate(num1, num2, diff);
choice = Menu();
switch (choice)
{
case 'a':
addition(correctans, num1, num2);
break;
case 'b':
subtraction(correctans, num1, num2);
break;
case 'c':
multiplication(correctans, num1, num2);
break;
case 'd':
division(correctans, num1, num2, choice);
break;
case 'q': test = false;// The user chose to quit the program.
cout << "Thank you for using Math Tutor program.\n\n";
return 0;
break;
}
}
if (choice != 'q')
{
cout << "incorect input please try again" << endl;
cin >> choice; // Loop again
}
return 0;
}
char Menu()
{
char choice;
{
cout << "Arithmetic Practice program\n" << endl;
// Display the menu and get a choice.
//cout << "------------------------------\n";
cout << "---------------------------------------------------------" << endl;
cout << "ARITHMETIC PRACTICE PROGRAM " << endl;
cout << "MAIN MENU" << endl;
cout << "-------------------------------------------------------- - " << endl;
cout << "a. Addition \n";
cout << "b. Subtraction\n";
cout << "c. Multiplication\n";
cout << "d. Division\n";
cout << "q. Quit the program\n";
cout << "------------------------------\n";
cout << "Enter your choice (a b c d q): ";
cin >> choice;
}
// Validate the choice.
choice = tolower(choice);
if (choice >= 'a' && choice <= 'd' || choice == 'q')
cout << "You entered: '" << choice << "', your input is valid " << endl;
else
{
cout << "The valid choices are a, b, c, "
<< "d, and q. Please choose: ";
cin >> choice;
}
cout << endl;
return choice;
}
void difficulty(int & diff)
{
//double studentans;
char line[80];
cout << "Select diffculty. Please choose" << endl;
cout << " 1 for 1 digit\n"
<< " 2 for 2 digits\n"
<< " 3 for 3 digits\n"
<< " 4 for 4 digits\n"
<< " 5 for 5 digits\n"
<< endl;
cout << endl << "> ";
cin >> line;
diff = atoi(line);
while (diff < 1 || diff >5)
{
cout << "Enter 1-5 only " << endl;
cin >> line;
diff = atoi(line);
cin.clear();
cin.ignore(1000, '\n');
}
}
void generate( double & num1, double & num2, int diff)
{
int min, max;
min = pow(10., diff - 1);
max = pow(10., diff);
num1 = min + rand() % (max - min);
num2 = min + rand() % (max - min);
}
void Ans(double & correctans)
{
double studentans;
// char choice;
// if (choice >= 'a' && choice <= 'd')
// {
cin >> studentans;
if (studentans == correctans)
cout << "\n\nCongratulations! That's right.\n\n";
else
cout << "\n\nSorry, the correct answer is " << correctans << ".\n\n";
// }
}
void addition(double & correctans, double num1, double num2)
{
double studentans; //Generate two random numbers in the range 1 - 500.
cout << "Prepare to add" << endl;
//difficulty(num1, num2);
// num1 and num2 haven't been randomly generated yet
correctans = num1 + num2;
// Display the problem.
cout << "\n\n";
cout << " " << setw(4) << num1 << endl;
cout << " +" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
Ans(correctans);
}
void subtraction(double & correctans, double num1, double num2)
{
double studentans;
//difficulty(num1, num2);
//if ('h' : num2 > num1) //num2 = 1 + rand() % 999;
correctans = num1 - num2;
// Display the problem.
cout << "\n\n";
cout << " " << setw(8) << num1 << endl;
cout << " -" << setw(8) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
Ans(correctans);
}
void multiplication(double & correctans, double num1, double num2)
{
double studentans;
//difficulty(num1, num2);
correctans = num1 * num2;
cout << "\n\n";
cout << " " << setw(4) << num1 << endl;
cout << " *" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
Ans(correctans);
}
void division(double & correctans, double num1, double num2, char choice)
{
double studentans;
num1 = num2 * num1;
//difficulty(num1, num2);
/*if (choice == 'b')
{
num2 = 1 + rand() % 9;
num1 = num2 * (rand() % 50 + 1);
}
if (choice == 'm')
{
num2 = 1 + rand() % 20;
num1 = num2 * (rand() % 100 + 1);
}
else if (choice == 'h')
{
num2 = 1 + rand() % 15;
num1 = num2 * (rand() % 110 + 1);
}
*/
// Calculate the correct answer.
correctans= num1 / num2;
cout << "\n\n";
cout << " " << num1 << " / " << num2 << " = ";
Ans(correctans);
}
|