-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.cc
52 lines (43 loc) · 1.11 KB
/
convert.cc
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
#include <iostream>
#include <math.h>
#include "convert.h"
// These are called IntToX, but accept longs too.
using namespace std;
void _IntToDigits(unsigned long n, int count, int* digits);
int CountDigits(int n) {
if (n == 0) {
return 1;
}
return floor(log10(n)) + 1;
}
// Generates a 10 element array containing a count of how often each of the
// digits from 0 to 9 appeared
void IntToDigitRange(unsigned long n, int count, int* range) {
int digits[count];
_IntToDigits(n, count, digits);
for (int i = 0; i < count; i++) {
int digit = digits[i];
range[digit]++;
}
}
// Generates an array containing the digits from the int in the order they
// appear.
void IntToDigits(unsigned long n, int count, int* digits) {
_IntToDigits(n, count, digits);
}
int IntToDigitSum(unsigned long n, int count) {
int sum = 0;
for (int i = 0; i < count; ++i) {
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
void _IntToDigits(unsigned long n, int count, int* digits) {
for (int i = 0; i < count; i++) {
int digit = n % 10;
digits[count - (i + 1)] = digit;
n /= 10;
}
}