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
|
/*
Exception and Recursion
Updated by: Piggy - #FIXED#
Date: 4/3/2019 - 4/10/2019 - #FIXED#
Program Info: Number system converter
The program uses recursion to find the binary, and octal representation of
given decimal number and vice versa.
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;
using llu = unsigned long long int;
// System specific macro function sysclear that
// clears console text
#ifdef _WIN32
#define sysclear() system ("cls") // not secure!
#else
#define sysclear() system ("clear") // not secure!
#endif
// Recursively converts decimal number to binary representation
string decToBin(llu num);
// Recursively converts binary number to decimal representation
llu binToDec(string binaryNum, int weight);
// Recursively converts decimal number to octal representation
string decToOct(llu num);
// Recursively converts octal number to decimal representation
llu octToDec(string octNum, int weight);
int main()
{
llu decimalNum;
int numPosition;
int binaryNum;
numPosition = 0;
bool valid = false;
string response = "yes";
while (response == "yes")
{
sysclear();
valid = false;
cout << "Program converts decimal number into binary \n and octal number systems and vice versa.\n\n";
// User input with validation
do
{
cout << "Enter a number in decimal: ";
try
{
cin >> decimalNum;
if (cin.fail())
{
throw "Invalid input. Try again!";
}
valid = true;
}
catch (const char *error)
{
cin.clear(); // clear the cin failure state
cin.ignore(100, '\n'); // ignore next 100 characters or up to \n char
printf("%s\n", error);
}
} while (!valid);
cout << endl;
string binary = decToBin(decimalNum);
printf("(%llu) base 10 = (%s) base 2\n", decimalNum, binary.c_str());
printf("(%s) base 2 = (%llu) base 10\n", binary.c_str(), binToDec(binary, numPosition));
// FIXME1 - call decToOct function passing proper argument and -
// display the values with proper descriptions. (10 points - FIXED)
cout << "Decimal to Octal Representation = " << decToOct(decimalNum) << '\n';
// FIXME2 - call octToDec function passing proper argument and
// display the values with proper descriptions. (10 points - FIXED)
cout << "Octal to Decimal Representation = " << octToDec(binary, numPosition) << '\n';
cin.ignore();
cout << "Would you like to convert another number? Enter yes/no:\n";
cin >> response;
}
cin.ignore();
cout << "Good bye! Enter to quit!";
cin.get();
return 0;
}
string decToBin(llu num) {
/*
Algorithm steps:
1. base case: binary of decimal num less than 2 is that number itself
2. Otherwise,
a. Concatenate all the remainders in reverse order and return it
*/
if (num < 2) {
return to_string(num);
}
else
return decToBin(num/2) + to_string(num%2);
}
llu binToDec(string binaryNumber, int position)
{
/*
binary number is received as a string value
Algorithm steps:
1. base case: if binaryNumber is empty or 0, decimal is 0
2. otherwise,
i. extract the rightmost bit
ii. convert char bit into int , e.g., '1' is converted to 1
iii. get the decimal value for the bit position by using formula bit*pow(2, position)
iv. recursively find the decimal value of the rest of the bits and add all the returned
values
3. return the final decimal value
*/
char bit;
llu dec = 0;
if (binaryNumber.length() == 0) // base case
return 0;
else
{
unsigned int index = binaryNumber.length() - 1;
bit = binaryNumber[index]; // 2.i
bit = int(bit) - int('0'); // 2.ii
llu bitValue = bit * static_cast<int>(pow(2.0, position)); // 2.iii
dec = bitValue;
binaryNumber = binaryNumber.substr(0, binaryNumber.length() - 1); //remove the last bit
position++; // increment the position
dec += binToDec(binaryNumber, position); // 2.iv
return dec; // 3
}
}
// FIXME3 - implement decToOct function (40 points - FIXED)
string decToOct(llu num)
{
/*
Algorithm steps:
1. base case: if octal of decimal num < 8 is that number itself
2. Otherwise,
a. Concatenate all the remainders in reverse order and return it
*/
if (num < 8)
return to_string(num);
else
return decToOct(num / 8) + to_string(num % 8);
}
// FIXME4 - implement octToDec function (40 points - Work in progress. Having an issue with, but should be working??)
llu octToDec(string octalNumber, int position) {
/*
Octal number is received as a string value and
the first position from the back
Algorithm steps:
1. base case: if octalNumber is empty or 0, decimal is 0
2. otherwise,
i. extract the rightmost number
ii. convert char value into int , e.g., '7' is converted to 7
iii. get the decimal value for the octal position using formula
octal*pow(8, position)
iv. recursively find the decimal value of the rest of the octal
numbers and add all the returned values
3. return the final decimal value
*/
char octal;
llu octalNum;
llu decTotal = 0;
// base case
if (octalNumber.length() == 0) {
return 0; // Step 1 of algorithm steps
}
// general case
else {
// Extracts the rightmost number.
unsigned int index = octalNumber.length() -1;
octal = octalNumber[index]; // 2.i
octalNum = int(octal) - int('0'); // 2.ii
decTotal = octalNum % 8;
octalNum = octalNum / 8;
position++;
// Issue is with the recursion:
octToDec(to_string(octalNum), position);
return decTotal; // step 3
}
}
|