-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdec_binary_conv.cpp
More file actions
84 lines (70 loc) · 1.73 KB
/
dec_binary_conv.cpp
File metadata and controls
84 lines (70 loc) · 1.73 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
/**
An implementation of decimal to binary conversion
dec_binary_conv.cpp
@author raph-son
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
/**
Convert a decimal number to decimal
@param int the integer to convert to binary
@return string a string containing the converted binaries
*/
string dec_to_bin(int num);
/**
Decide which symbol between 0 and 1 to return based on the number
given
@param int an even or odd number
@return char symbol 0 or 1
*/
char is_even(int num);
/**
Insert a symbol to the back of other symbols
@params string, char a string containing symbols where new symbol will
be added to. char the symbol to add to other symbol
@return void
*/
void to_back(string& str, char chr);
// Program's entry point
int main(int c, char* argv[]) {
if(c < 2) {
cout << "Usage: Type decimal number after programs's name" << endl;
return 1;
}
string conversion = dec_to_bin(atoi(argv[1]));
cout << conversion << endl;
return 0;
}
string dec_to_bin(int num) {
// Convert decimal number to binary
int number = num;
int quotient = number / 2;
string bin;
bin.append(sizeof(char), is_even(number));
while(quotient != 0) {
to_back(bin, is_even(quotient));
number = quotient;
quotient = number / 2;
}
// Algorithm completed
return bin;
}
char is_even(int num) {
// Return O if num is even
// return 1 if not
if(num % 2 == 0) {
return '0';
}
return '1';
}
void to_back(string& str, char chr) {
// Add a char to the back of string
if(str.length() > 0) {
str.insert(0 ,1, chr);
}
else {
str = chr;
}
}