-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathProblem.cs
More file actions
159 lines (138 loc) · 4.87 KB
/
Problem.cs
File metadata and controls
159 lines (138 loc) · 4.87 KB
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
namespace ConsoleApp1
{
class Problem
{
private string equation = "";
private int solution;
private int moduloCheck;
private int maxVal;
private void SetMaxVal(int difficulty)
{
switch (difficulty)
{
case 1:
{
maxVal = 10;
break;
}
case 2:
{
maxVal = 50;
break;
}
case 3:
{
maxVal = 100;
break;
}
}
}
public void GenerateEquation(int operation = 5, int difficulty = 1)
{
Random random = new();
SetMaxVal(difficulty);
if (operation == 5)
{
operation = random.Next(0, 4)+1;
}
switch (operation)
{
// Addition
case 1:
{
Random randint1 = new();
Random randint2 = new();
int addend1 = randint1.Next(0, maxVal);
int addend2 = randint2.Next(0, maxVal);
equation = $"{addend1} + {addend2}";
solution = addend1 + addend2;
break;
}
// Subtraction
case 2:
{
do
{
Random randint1 = new();
Random randint2 = new();
int minuend = randint1.Next(0, maxVal);
int subtractend = randint2.Next(0, maxVal);
equation = $"{minuend} - {subtractend}";
solution = minuend - subtractend;
}
while (solution < 0);
break;
}
//Multiplication
case 3:
{
Random randint1 = new();
Random randint2 = new();
int factor1 = randint1.Next(0, maxVal);
int factor2 = randint2.Next(0, maxVal);
equation = $"{factor1} * {factor2}";
solution = factor1 * factor2;
break;
}
// Division
case 4:
{
do
{
Random randint1 = new();
Random randint2 = new();
int dividend = randint1.Next(0, maxVal);
int divisor = randint2.Next(0, maxVal);
equation = $"{dividend} \u00f7 {divisor}";
try
{
solution = dividend / divisor;
}
catch (DivideByZeroException ex)
{
continue;
}
moduloCheck = dividend % divisor;
//try
//{
//}
//catch (DivideByZeroException ex)
//{
// continue;
//}
}
while (moduloCheck != 0 || solution <= 0);
break;
}
}
}
public void DisplayEquation()
{
Console.WriteLine("What is the solution to the following equation?");
Console.WriteLine(equation);
//Console.WriteLine(Solution);
}
public int CheckSolution(string input)
{
int Input;
if (int.TryParse(input, out Input))
{
if (Input == solution)
{
Console.WriteLine("Correct!");
return 1;
}
else
{
Console.WriteLine($"Incorrect. The correct answer is {solution}.");
return 0;
}
}
else
{
Console.WriteLine($"Invalid input. The correct answer is {solution}.");
return 0;
}
}
}
}