forked from ChicoState/cpp-gtest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrando.cpp
More file actions
133 lines (119 loc) · 1.74 KB
/
rando.cpp
File metadata and controls
133 lines (119 loc) · 1.74 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
#include "rando.h"
#include<assert.h>
#include<cmath>
#include <stdlib.h>
/**
* Worry if only one child is smiling or if all children are smiling
**/
bool Rando::shouldWorry(bool childASmile, bool childBSmile, bool childCSmile)
{
if( childASmile && childBSmile && childCSmile )
return true;
else if( childASmile ^ childBSmile ^ childCSmile )
return true;
else
return false;
}
/**
* Determines if first or second are evenly divisible by the other.
**/
bool Rando::isDivisbleBy(int first, int second)
{
if( (first % second) == 0)
{
return true;
}
else
{
if( (second % first) == 0)
{
return true;
}
else
{
return false;
}
}
}
/**
* Given a number, num, says whether or not that number is prime
* (that is, only evenly divisible by itself and 1. For the sake
* of this function, do NOT consider 0 prime)
**/
bool Rando::isPrime(int num)
{
for(int i = 2; i < num; i++)
{
if( (num%i) == 0 )
{
return false;
}
}
return true;
}
/**
* Given two numbers, first and second, return the value of the one
* that is closest to zero without actually being zero.
**/
int Rando::nearestToZero(int a, int b)
{
if(a == 0 || b == 0)
{
return 1;
}
else
{
assert(a != 0 && b != 0);
if(a < 0 || b < 0)
{
if(a < 0 && b > 0)
{
int a_abs = abs(a);
if(a_abs < b)
{
return a;
}
else
{
return b;
}
}
else if(a > 0 && b < 0)
{
int b_abs = abs(b);
if(a < b_abs)
{
return a;
}
else
{
return b;
}
}
else
{
assert(a < 0 && b < 0);
if(a < b)
{
return b;
}
else
{
return a;
}
}
}
else
{
assert(a > 0 && b > 0);
if(a < b)
{
return a;
}
else
{
return b;
}
}
}
}