-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsUtility.c
More file actions
53 lines (45 loc) · 943 Bytes
/
csUtility.c
File metadata and controls
53 lines (45 loc) · 943 Bytes
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
#include "csUtility.h"
int randInt(int low, int high, bool inclusive)
{
return low + (rand() % (high + inclusive));
}
/** \brief Gets the amount of digits in num.
*
* \param num - your number
* \return the number of digits in num.
*/
int digits(int num)
{
if (num < 0)
num *= -1;
if (num == 0)
return 1;
return 1 + log10(num);
}
/** \brief free() with automatic NULL setting.
* Use: x = freeThisMem(x);
* \param x - memory address
* \return NULL
*/
void* freeThisMem(void* x)
{
free(x);
return NULL;
}
char* removeNewline(char* stuff, char replacement, int maxLength)
{
for(int i = maxLength - 1; i >= 0; i--)
{
if (i < 0)
return stuff;
if (stuff[i] == '\n')
{
stuff[i] = replacement;
}
}
return stuff;
}
double getDistance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}