-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPlusPlusTests.cpp
More file actions
263 lines (205 loc) · 8.71 KB
/
CPlusPlusTests.cpp
File metadata and controls
263 lines (205 loc) · 8.71 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
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
// CPlusPlusTests.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
using namespace std;
#include "TestBase.h"
// Declarations for functions in the tests
extern int Double(int a);
extern bool IsNegative(int a);
extern bool IsPrime(int a);
extern int GCD(int a, int b);
// Test1
int REF_Double(int a)
{
return a*2;
}
//Test2
bool REF_IsNegative(int a)
{
return a<0;
}
//Test3
bool REF_IsPrime(int a)
{
if(a <2 )
return false;
for(int i=2; i <=a/2; i++)
{
int v = a/i;
if( (v*i) == a)
return false;
}
return true;
}
//Test3
bool GoesInto(int a, int b)
{
if( ((b/a)*a) ==b )
return true;
return false;
}
int REF_GCD(int a, int b)
{
int min = a<b ? a : b;
for(int i=min; i>0; i--)
{
if( GoesInto(i,a) && GoesInto(i,b))
return i;
}
return 1;
}
void Test1()
{
#if TEST==1
int num,t1,t2;
cout << "\nThis test will ask for a number and tell you what the number is doubled\n";
while(1)
{
cout << "\nEnter a number (or 0 to exit)\n";
cin >> num;
if(num==0) break;
t1 = Double(num);
t2 = REF_Double(num);
if(t1==t2)
{
cout << "\nCORRECT the answer is " << t1 << "\n";
}
else
{
cout << "\nINCORRECT the answer is " <<t2 << ", your answer was " << t1 << "\n";
}
}
#endif
}
void Test2()
{
#if TEST==2
int num;
bool t1,t2;
cout << "\nThis test will ask for a number and tell you if the number is negative or not\n";
while(1)
{
cout << "\nEnter a number (or 0 to exit)\n";
cin >> num;
if(num==0) break;
t1 = IsNegative(num);
t2 = REF_IsNegative(num);
if(t1==t2)
{
cout << "\nCORRECT the answer is " << (t1 ? "true" : "false") << "\n";
}
else
{
cout << "\nINCORRECT the answer is " << (t2 ? "true" : "false") << ", your answer was " << (t1 ? "true" : "false") << "\n";
}
}
#endif
}
void Test3()
{
#if TEST==3
int num;
bool t1,t2;
cout << "\nThis test will ask for a number and tell you if the number is prime or not\n";
while(1)
{
cout << "\nEnter a number (or 0 to exit)\n";
cin >> num;
if(num==0) break;
if((num >0) && (num <0x7fffffff))
{
t1 = IsPrime(num);
t2 = REF_IsPrime(num);
if(t1==t2)
{
cout << "\nCORRECT the answer is " << (t1 ? "true" : "false") << "\n";
}
else
{
cout << "\nINCORRECT the answer is " << (t2 ? "true" : "false") << ", your answer was " << (t1 ? "true" : "false") << "\n";
}
}
else
{
cout << "\nNumber out of range, Try again \n";
}
}
#endif
}
void Test4()
{
#if TEST==4
int num1, num2;
int t1,t2;
cout << "\nThis test will ask for two numbers and tell you the Greates Common Divisor\n";
while(1)
{
cout << "\nEnter a number (or 0 to exit)\n";
cin >> num1;
if(num1==0) break;
cout << "\nEnter another number\n";
cin >> num2;
t1 = GCD(num1,num2);
t2 = REF_GCD(num1,num2);
if(t1==t2)
{
cout << "\nCORRECT the answer is " << t1 << "\n";
}
else
{
cout << "\nINCORRECT the answer is " << t2 << ", your answer was " << t1 << "\n";
}
}
#endif
}
void Test5() { }
void Test6() { }
void Test7() { }
void Test8() { }
void Test9() { }
int main()
{
printf("Test %d\n\n", TEST);
switch (TEST)
{
case 1:
Test1();
break;
case 2:
Test2();
break;
case 3:
Test3();
break;
case 4:
Test4();
break;
case 5:
Test5();
break;
case 6:
Test6();
break;
case 7:
Test7();
break;
case 8:
Test8();
break;
case 9:
Test9();
break;
default:
printf("Unknown Test %d\n\n", TEST);
break;
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file