-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicfunctions.cpp
250 lines (208 loc) · 4.94 KB
/
basicfunctions.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
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
#include <iostream>
#include <string>
#include "basicfunctions.h"
#include "GlobalData.h"
#include "namespacealiases.h"
// Reads and returns a character from the input stream,
// ignoring newlines.
char BasicFunctions::readchr() {
char c = std::cin.get();
while ( c == '\n' ) {
c = std::cin.get();
}
return c;
}
// Reads the value of a sequence of letters
int BasicFunctions::readletval(char input) {
int result = 0;
if (bsf::ltoi(input) != -1)
{
result = bsf::ltoi(input);
}
else
{
return -1;
}
while (((std::cin.peek() <= '0' || std::cin.peek() >= '9')
&& std::cin.peek() != '\n'))
{
if ((bsf::ltoi(input) != -1 &&
(result <= ((gd::MAX - bsf::ltoi(input)) / alph::size))))
{
result *= alph::size;
result += bsf::ltoi(input);
}
else
{
// cin.get until cin.get == \n
while (std::cin.peek() != '\n')
{
std::cin.get();
}
return -1;
}
input = bsf::readchr();
}
return result;
}
// Reads and returns a positive integer from the input stream
int BasicFunctions::readint(int min, int max, char c) {
int num = 0;
bool overflow = false;
if (c < '0' || c > '9') {
return -1;
}
while ( c != '\n') {
if (c >= '0' && c <= '9' && !overflow) {
if ((max - (c - '0')) / 10 >= num) { //prevent overflow
num = num * 10 + (c - '0'); //which would
} else { //result in num
num = max; //becoming negative
overflow = true;
}
}
c = std::cin.get();
}
if (num > max) {
num = max;
} else if (num < min) {
num = min;
}
return num;
}
// Returns true if the character is a number, false otherwise
bool BasicFunctions::isnum(char c) {
return c >= '0' && c <= '9';
}
// Converts a letter to an integer based on its pos in the alphabet,
// starting from 1. Returns -1 if the character is not a letter.
int BasicFunctions::ltoi(char c) {
for (int i = 0; i < alph::size; i++) {
if (c == *alph::lower[i] || c == *alph::upper[i]) {
return i + 1;
}
}
return -1;
}
char BasicFunctions::itol(int i) {
if (*alph::lower[i]) {
return *alph::lower[i];
}
return '\0';
}
void BasicFunctions::action() {
std::cout << "Press actions not yet defined." << std::endl;
return;
}
int BasicFunctions::calcind(int boxWidth, int titleOrOptionLength) {
int indentation = 0;
indentation = (boxWidth - titleOrOptionLength) / 2;
return indentation;
}
int BasicFunctions::floor(float x) {
int y = int(x);
if (x < y) {
return y - 1;
} else {
return y;
}
}
int BasicFunctions::ceil(float x) {
int y = int(x);
if (x > y) {
return y + 1;
} else {
return y;
}
}
int BasicFunctions::min(int x, int y) {
if (x < y) {
return x;
} else {
return y;
}
}
int BasicFunctions::max(int x, int y) {
if (x > y) {
return x;
} else {
return y;
}
}
float BasicFunctions::abs(float x) {
if (x < 0) {
return -x;
} else {
return x;
}
}
float BasicFunctions::pow(float x, int n) {
float result = 1;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}
float BasicFunctions::sqrt(float x, float epsilon) {
if (x < 0) {
std::cout << "Error: cannot calculate square root of negative\
number.";
return 0;
}
if (x == 0 || x == 1) {
return x; // trivial cases
}
float guess = x / 2.0;
float newGuess;
do {
newGuess = (guess + x / guess) / 2.0;
if (abs(guess - newGuess) < epsilon) {
break;
}
guess = newGuess;
} while (true);
return newGuess;
}
long long BasicFunctions::perm(int n, int r) {
long long result = 1; // for trivial cases
if (n < r || n < 0 || r < 0) {
std::cout << "Error: please enter n >= r >= 0.";
return -1;
}
if (n > gd::LARGE_NUM_MAX || r > gd::LARGE_NUM_MAX) {
return -1;
}
// Check for potential overflow
for (int i = n; i > n - r; i--) {
if (result > gd::LARGE_NUM_MAX / i) {
return -1;
}
result *= i;
}
return result;
}
long long BasicFunctions::fact(int n) {
long long result = 1; // for trivial cases
if (n < 0) {
std::cout << "Error: please enter n >= 0.";
return 0;
}
for (int i = n; i > 0; i--) {
if (result > gd::LARGE_NUM_MAX / i) {
return -1;
}
result *= i;
}
return result;
}
// Stylise a long long with dots to make it more readable
std::string BasicFunctions::prettylong(long long num) {
std::string result = std::to_string(num);
int length = result.length();
int i = length - 3;
while (i > 0) {
result.insert(i, ".");
i -= 3;
}
return result;
}