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
|
#include <iostream>
#include <string>
using namespace std;//the names defined in iostream are to be interpreted in the standard way.
void addTwoHexDigits(const char firstAddend, const char secondAddend, char& sumDigit, char& carryDigit);
void addTwoHexadecimalNumbers(const char addendOne[], const char addendTwo[], char total[], const unsigned int size);
char convertHexDigitToDecimalNumber(const char digit);
int main()
{
char firstAddend;
char secondAddend;
char answer;
char addendOne[10];
char addendTwo[10];
char total[10];
int size = 2;
char sumDigit;
char carryDigit;
int i;
char firstHexNumber[10];
char secondHexNumber[10];
do
{
cout << "Input value for the first digit/letter:\n";
for( i = 0; i < (size); i++)
{
cin >> firstHexNumber[i];
firstHexNumber[i] = convertHexDigitToDecimalNumber(firstHexNumber[i]);
}
cout << "Input value for the second digit/letter:\n";
for( i = 0; i < size; i++)
{
cin >> secondHexNumber[i];
secondHexNumber[i] = convertHexDigitToDecimalNumber(secondHexNumber[i]);
}
for( i = 0; i < size; i++)
{
firstHexNumber[i] = convertHexDigitToDecimalNumber(firstHexNumber[i]);
secondHexNumber[i] = convertHexDigitToDecimalNumber(secondHexNumber[i]);
}
for (i = 0; i < size; i++)
{
firstAddend = firstHexNumber[i];
secondAddend = secondHexNumber[i];
addTwoHexDigits(firstAddend, secondAddend, sumDigit, carryDigit);
}
addTwoHexadecimalNumbers(addendOne, addendTwo, total, size);
cout << "Do you wish to run the program again? (Y/N):\n";
cin >> answer;
}while ((answer == 'Y') || (answer == 'y'));
system("pause");
return 0;
}
void addTwoHexadecimalNumbers(const char addendOne[], const char addendTwo[], char total[], const unsigned int size)// viod function adds two hexadecimal numbers
{
char sumDigit;// sumdigit will retuen a number or letter
char carryDigit = '0'; // carry digit starts off as zero when no calculations are done
char sum_temp; //holds the value of the sum of sum
char carry_temp; // char variables used to hold the sum digit and carry digit
int i;// used in for loop
for (i = (size - 1); i >= 0; i--)// conditions for for loop
{
if (carryDigit == '1')// if carry digit is 1 runs if statement
{
carry_temp = carryDigit;
addTwoHexDigits(carry_temp, addendOne[i], sumDigit, carryDigit);
sum_temp = sumDigit;
addTwoHexDigits(sum_temp, addendTwo[i], sumDigit, carryDigit);
total[i] = sumDigit;
}
else
{
addTwoHexDigits(addendOne[i], addendTwo[i], sumDigit, carryDigit);
total[i] = sumDigit;
}
}
if (carryDigit == '1') // if carry digit is equal to '1', then
{
cout << "Overflow error : ";
for (i = 0; i < size; i++)
{
cout << "*" << endl;
}
}
else
{
cout << endl << "The sum is: ";
for (i = 0; i < size; i++)
{
cout << total[i] << endl;
}
}
return;
}
void addTwoHexDigits(const char firstAddend, const char secondAddend, char& sumDigit, char& carryDigit)//void function that will calculate the value of the hexadecimal input buy running
//converted values through a loop up chart and then dispaly the sum and carry over number
{
char sumDigitTable[16][16] = //look up chart that looks up the place of input and then returns that output to the sumDigit
{
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'},
{'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0'},
{'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1'},
{'3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2'},
{'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3'},
{'5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4'},
{'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5'},
{'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6'},
{'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7'},
{'9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8'},
{'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
{'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A'},
{'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'},
{'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'},
{'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D'},
{'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'},
};
char carryDigitTable[16][16] = //look up chart that looks up the place of the carry digit and then returns that output to the carryDigit
{
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1'},
{'0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1'},
{'0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
{'0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
};
sumDigit = sumDigitTable[firstAddend][secondAddend];// returns value of the imputs after converting it to hexadecimal
carryDigit = carryDigitTable[firstAddend][secondAddend];// returns the carry overr number
}
char convertHexDigitToDecimalNumber(const char digit)
{
// convert the hex digit parameter into its decimal equivalent ('1' becomes 1, 'A' becomes 10, etc.)
int digitValue;
char i;
switch (digit)
{
case ('0'):
digitValue = 0;
break;
case ('1'):
digitValue = 1;
break;
case ('2'):
digitValue = 2;
break;
case ('3'):
digitValue = 3;
break;
case ('4'):
digitValue = 4;
break;
case ('5'):
digitValue = 5;
break;
case ('6'):
digitValue = 6;
break;
case ('7'):
digitValue = 7;
break;
case ('8'):
digitValue = 8;
break;
case ('9'):
digitValue = 9;
break;
case ('A'):
digitValue = 10;
break;
case ('B'):
digitValue = 11;
break;
case ('C'):
digitValue = 12;
break;
case ('D'):
digitValue = 13;
break;
case ('E'):
digitValue = 14;
break;
case ('F'):
digitValue = 15;
break;
default:
digitValue = -1; // parameter out of range
} // end switch
return digit;
}
|