-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWk9Labs1.cpp
108 lines (91 loc) · 2.39 KB
/
Wk9Labs1.cpp
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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
int readint() // input validation(no perimeters since return is x)
{
int x;
cin >> x;
while(cin.fail())
{
cin.clear();
string notint;
cin >> notint;
cout << "Input " << notint << " is not an integer. Enter an integer.";
cin >> x;
}
return x;
}
char signchar(int x) // make a * b value negative when x is below 0 ()
{
if(x >= 0)
{
return '+';
}
else {
return '-';
}
}
int main()
{
const int MAXINT = 10;
const int MININT = -10;
int x;
int score = 0;
char reply;
int tries = 0;
int a;
int b;
cout << "***********************************\n";
cout << "* CS111 math tutor *\n";
cout << "***********************************\n\n";
cout << "Enter 'L'' to try a linear and 'Q' to try a quadratic equation.\nAny other key to quit.";
cin >> reply;
while(reply == 'L' || reply == 'Q')
{
srand(time(0));
a = rand()% (MAXINT - MININT + 1) + MININT;
b = rand()% (MAXINT - MININT + 1) + MININT;
if(reply == 'L')
{
cout << "\nWhat is the solution to\n";
cout << a << " x " << signchar(a * b) << " " << fabs(a * b) << " = 0\n";
x = readint();
if( a * x + a * b == 0)
{
score++;
cout << "Great!\n\n";
tries++;
}
else
{
cout << "Oops!\n\n";
tries++;
}
}
else
{
cout << "\nWhat is a solution to\n";
cout << "x^2 "<< signchar(a + b) << " "<< fabs(a * b) <<" x " << signchar(a * b)<< " "<< fabs(a * b)<<" = 0\n";
cin >> x;
x = readint();
if( x * x + (a + b)* x + a * b == 0)
{
score++;
cout << "Great!\n\n";
tries++;
}
else
{
cout << "Oops!\n\n";
tries++;
}
}
cout << "Enter 'L'' to try a linear and 'Q' to try a quadratic equation.\nAny other key to quit.";
cin >> reply;
}
cout << "Your score is: " << score << endl;
cout << "Your tries are: " << tries << endl;
return 0;
}