-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (113 loc) · 3.41 KB
/
main.cpp
File metadata and controls
123 lines (113 loc) · 3.41 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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
string convert_decimal_to_binary(int decimal_number);
string convert_octal_to_binary(string octal_value);
string convert_hexadecimal_to_binary(string hexadecimal_value);
int greatest_common_divisor(int number1, int number2);
int main() {
cout << greatest_common_divisor(0,0);
}
// In this function, the user enters a decimal (base 10) number,
// and the binary result (base 2) is returned as a string.
string convert_decimal_to_binary(int decimal_number){
std::string binary_result= "";
bool function_completed = false;
while (!(function_completed)){
int resulting_int = decimal_number/2;
int modulo = decimal_number - resulting_int*2;
decimal_number = resulting_int;
if (modulo == 1){
binary_result = "1" + binary_result ;
}
else {
binary_result = "0" + binary_result;
}
if (resulting_int==0){
function_completed = true;
break;
}
}
return binary_result;
}
// The user enters an octal (base 8) value, and a binary (base 2) value is returned as a string
string convert_octal_to_binary(string octal_value) //, bool extra_zeros_at_beginning= false){
{
string resulting_octal;
int right_index = octal_value.length()-1;
while (right_index >= 0){
char right_value = octal_value.at(right_index);
int right_valuee = right_value - '0';
string current_binary;
int current_factor =1;
string ok = convert_decimal_to_binary(right_valuee);
while (ok.length()<3){
ok = "0" + ok;
}
right_index-=1;
resulting_octal = ok + resulting_octal;
}
/*
int octal_length = resulting_octal.length();
if (extra_zeros_at_beginning) {
while (octal_length % 4 != 0) {
resulting_octal = "0" + resulting_octal;
octal_length = resulting_octal.length();
}
}
*/
return resulting_octal;
}
string convert_hexadecimal_to_binary(string hexadecimal_value){
string final_value;
unordered_map<char,int> results= {
{'A',10},
{'B',11},
{'C',12},
{'D',13},
{'E',14},
{'F',15},
};
int right_index = hexadecimal_value.length() - 1;
while (right_index>=0){
int right_val;
char right_value = hexadecimal_value.at(right_index);
if (results.count(right_value)){
right_val = results.at(right_value);
}
else{
right_val = right_value - '0';
}
string binary_result = convert_decimal_to_binary(right_val);
final_value= binary_result + final_value;
right_index-=1;
}
return final_value;
}
//Utilizing the Euclidean Algorithm to find the greatest common divisor
int greatest_common_divisor(int number1, int number2){
int larger=0;
int smaller=0;
if (number1 >= number2){
larger = number1;
smaller = number2;
}
else{
larger = number2;
smaller = number1;
}
int remainder=1;
int times_divided;
while (remainder!=0){
times_divided = larger/smaller;
remainder = larger - (smaller*times_divided);
if (remainder==0){
return smaller;
}
larger =smaller;
smaller=remainder;
}
}