forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operations.c
234 lines (199 loc) · 5.02 KB
/
Operations.c
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
/*
This is a program that does 4 operations, addition,
subtraction, multiplication, and division.
written by;
randerson112358
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <time.h>
# include <conio.h>
int correct = 0, wrong = 0;
int choice;
int x,y;
int numProblems;
int lowRange, highRange;
void menu();
void printGrade(int grade);
void changeRandom(int y);
int main(void)
{
/* Seed random number generator */
int seed = time(NULL);
int i;
double grade;
double answer;
int startTime, endTime;
srand(seed);
do{
menu();
if(choice != 0){
system("cls");// windows clear screen command
printf("How many problems would you like?\n");
scanf("%d",&numProblems);
printf("What's the range of numbers you would like?\n");
printf("Low range: ");
scanf("%d",&lowRange);
printf("High range: ");
scanf("%d",&highRange);
system("cls");// windows clear screen command
startTime = time(NULL);
for(i=1; i<=numProblems; i++)
{
x = rand_mid(lowRange,highRange);
y = rand_mid(lowRange,highRange);
if(choice == 1)//add
{
printf("%d)%d + %d = ", i,x,y);
scanf("%lf", &answer);
checkADD(x, y, answer);
}
else if(choice == 2)//sub
{
if(x < y) // THIS IS SO THERE ARE NO NEGATIVE NUMBERS
Switch();
printf("%d)%d - %d = ", i,x,y);
scanf("%lf", &answer);
checkSUB(x, y, answer);
}
else if(choice == 3)//mult
{
printf("%d)%d x %d = ", i,x,y);
scanf("%lf", &answer);
checkMULT(x, y, answer);
}
/* else if(choice == 4)//division
{
// FIGURE OUT DIVISION BY 0 PROBLEM FOR BOTH X AND Y!!!!!!!!
if(y == 0)
changeRandom(y);
if(x < y && x !=0 )
Switch();
if(y == 0)
changeRandom(y);
printf("%d)%d / %d = ", i,x,y);
scanf("%lf", &answer);
checkDIV(x, y, answer);
}*/
}//end for loop
endTime = time(NULL);
grade = ((double)correct*100 / (double)numProblems) ;
printf("\nCorrect: %d , Wrong: %d \n", correct, wrong);
printf("Grade: %.1lf ", grade);
printGrade(grade);
printf("Total time: %d seconds\n", endTime - startTime);
correct = 0;
wrong = 0;
system("pause");// windows pause command
}// end if statement
}while(choice != 0);
printf("Good Bye\n");
system("pause");// windows pause command
}
//Function Creates random number between low - high
int rand_mid(int low, int high)
{
return low+rand()%(high-low+1);
}
//Checks if the multiplication is correct
int checkMULT(int x, int y, double answer)
{
printf("ANSWER: %d\n",x*y);
if(answer == x*y)
{
correct ++;
return 1;
}
else
{
wrong++;
return 0;
}
}
//Checks if the division is correct
int checkDIV(int x, int y, double answer)
{
printf("ANSWER: %d\n",x/y);
if(answer == x/y)
{
correct ++;
return 1;
}
else
{
wrong++;
return 0;
}
}
//Checks if the addition is correct
int checkADD(int x, int y, double answer)
{
printf("ANSWER: %d\n",x+y);
if(answer == x+y)
{
correct ++;
return 1;
}
else
{
wrong++;
return 0;
}
}
//Checks if the subtraction is correct
int checkSUB(int x, int y, double answer)
{
printf("ANSWER: %d\n",x-y);
if(answer == x-y)
{
correct ++;
return 1;
}
else
{
wrong++;
return 0;
}
}
void changeRandom(int y)
{
y = rand_mid(lowRange,highRange);
if( y == 0 )
return changeRandom(y);
return;
}
//switches x and y values
int Switch()
{
int temp;
temp = x;
x = y;
y = temp;
return 1;
}
void printGrade(int grade)
{
if(grade >= 90)
printf(" A");
else if(grade >= 80)
printf(" B");
else if(grade >= 70)
printf(" C");
else if(grade >= 60)
printf(" D");
else if(grade < 60)
printf(" F");
printf("\n");
}
void menu()
{
system("cls");// windows clear screen command
printf("Choose the type of problems you want:\n");
printf("1)Addition\n");
printf("2)Subtraction\n");
printf("3)Multiplication\n");
printf("4)division\n");
printf("0)exit\n");
scanf("%d", &choice);
}