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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include <iostream>
//#include <sstream>
//#include <istream>
#include <string>
#include<string.h>
#include <stdio.h>
#include <ctype.h>
using namespace std;
//Function Prototypes
void menu1();
void decToBin(long int);
void menu2();
void sizeErr(string);
void zeroOneErr(string);
void binToDec(string);
void menu3();
void decToHex(int);
void menu4();
void sizeHexErr(char [20]);
void hexError(char [20]);
void hexToDec(char [20]);
// Function decimal to binary
void decToBin(long int n)
{
// array
long int binNum[100];
// counter
int i = 0;
while (n > 0) {
// storing binary in array
binNum[i] = n % 2;
n = n / 2;
i++;
}
//display
for (int j = i - 1; j >= 0; j--)
cout << binNum[j];
}
//Function decimal to binary: accept input and error check
void menu1()
{
long int n = 0;
cout << "Enter a Positive Decimal number and I'll convert it to Binary. --> ";
cin >> n;
cout << endl;
while(n <= 0)
{
cout << "Please enter a Number Greater Than Zero and I'll convert it to Binary. --> ";
cin >> n;
cout << endl;
}
decToBin(n);
}
// Function convert binary to decimal
void binToDec(string n)
{
string num = n;
int dec_val = 0;
// Initialize base
int base = 1;
//Calulate decimal value
int len = num.length();
for (int i=len-1;i>=0;i--)
{
if (num[i] == '1')
dec_val += base;
base = base * 2;
}
cout << dec_val << endl;
}
//Function error check binary to decimal: entry other than zero or one
void zeroOneErr(string cnum)
{
bool counter = true;
int len = cnum.length();
//Check for zeroes and ones
for(int i = 0; i <= len -1; i++ )
{
if(cnum[i] != '0' && cnum[i] != '1')
counter = false;
}
if(counter == false)
{
counter = true;
cout << "\nYou didn't enter all zeroes or ones. ";
menu2();
}
else
{
binToDec(cnum);
}
}
//Function error check binary to decimal: greater than 8 chars
void sizeErr(string cnum)
{
int len = cnum.length();
while(len >= 9)
{
cout << endl << "Your number was longer than 8 characters, Enter a Binary number no longer than 8 characters and I'll convert it to Decimal. -->";
getline(cin, cnum);
len = cnum.length();
}
zeroOneErr(cnum);
}
void menu2()
{
string cnum;
cout << "Enter a Binary number no longer than 8 characters and I'll convert it to Decimal. --> ";
getline(cin, cnum);
sizeErr(cnum);
}
// function to convert decimal to hexadecimal
void decToHex(int n)
{
// char array to store hexadecimal number
char hexNum[100];
// counter for hex number array
int i = 0;
while(n!=0)
{
// store remainder
int temp = 0;
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexNum[i] = temp + 48;
i++;
}
else
{
hexNum[i] = temp + 55;
i++;
}
n = n/16;
}
// print hex array
for(int j=i-1; j>=0; j--)
cout << hexNum[j];
}
void menu3()
{
int num = -1;
cout << "Enter a Positive Decimal number and I'll convert it to Hexadecimal. --> " << endl;
cin >> num;
while (num <= 0)
{
cout << "Your number is not positive. Enter a Positive Decimal number and I'll convert it to Hexadecimal. --> " << endl;
cin.ignore();
cin >> num;
}
decToHex(num);
}
// Function to convert hex to decimal
void hexToDec(char hexVal[])
{
int len = strlen(hexVal);
// Initialize base
int base = 1;
int dec_val = 0;
// Loop through array, last first
for (int i=len-1; i>=0; i--)
{
// Add to decimal value, 0-9
if (hexVal[i]>='0' && hexVal[i]<='9')
{
dec_val += (hexVal[i] - 48)*base;
// increase to next higher power
base = base * 16;
}
// add to decimal value, a-f
else if (hexVal[i]>='A' && hexVal[i]<='F')
{
dec_val += (hexVal[i] - 55)*base;
// increase to next higher power
base = base*16;
}
}
//Display decimal value accumulated in dec_val
cout << dec_val << endl;
}
void hexError(char hexVal[])
{
bool counter = true;
int len = strlen(hexVal);
//Run toupper() and assign values to array
for(int i=0; i < len; i++)
{
hexVal[i] = toupper(hexVal[i]);
}
for (int i=0; i < len; i++)
{
if ((hexVal[i]>='0' && hexVal[i]<='9') || (hexVal[i]>='A' && hexVal[i]<='F'))
{
}
else
{
counter = false;
}
}
if(counter == false)
{
counter = true;
cout << "\nYou entered characters that aren't hexadecimal. ";
menu4();
}
else
{
hexToDec(hexVal);
}
}
void sizeHexErr(char hexVal[])
{
int len = strlen(hexVal);
while(len >= 5)
{
cout << "\nYour number was longer than 4 characters, please enter a Hexadecimal number no longer than 4 characters and I'll convert it to Decimal --> ";
cin.getline(hexVal, 20);
len = strlen(hexVal);
}
hexError(hexVal);
}
void menu4()
{
char hexNum[20];
cout << "Enter a Hexadecimal number no longer than 4 characters and I'll convert it to Decimal. --> " << endl;
cin.getline(hexNum, 20);
sizeHexErr(hexNum);
}
int main()
{
int choice;
//Constants for menu choice
const int dec_to_bin = 1;
const int bin_to_dec = 2;
const int dec_to_hex = 3;
const int hex_to_dec = 4;
const int end = 9;
do
{
//Display menu and get a choice
cout << "\n\nEnter 1, 2, 3, 4 or 9 to exit --> \n\n";
cout << "1. Decimal to Binary\n";
cout << "2. Binary to Decimal\n";
cout << "3. Decimal to Hex\n";
cout << "4. Hex to Decimal\n";
cout << "9. Quit\n\n";
cin >> choice;
switch (choice)
{
case dec_to_bin :
{
menu1();
break;
}
case bin_to_dec :
{
menu2();
break;
}
case dec_to_hex :
{
menu3();
break;
}
case hex_to_dec :
{
menu4();
break;
}
case end :
{
break;
}
default :
{
cout << "\nYour entry was invalid.\n";
}
}
} while (choice != 9);
//menu1();
//menu2();
//menu3();
//menu4();
return 0;
}
|