C++ // NOTE: The ONLY files that should be #included for this assignment are ios
ID: 3749474 • Letter: C
Question
C++
// NOTE: The ONLY files that should be #included for this assignment are
iostream, vector, and string
// No other files should be #included
#include <iostream>
#include <vector>
#include <string>
// NOTE: The ONLY files that should be #included for this assignment are
iostream, vector, and string
// No other files should be #included
using namespace std;
string addbin(string, string);
string addhex(string, string);
int main()
{
cout<<"binary 1101 + 1000 = "<<addbin("1101", "1000")<<endl; //you
should get 10101
cout<<"binary 11000 + 1011 = "<<addbin("11000", "1011")<<endl; //you
should get 100011
cout<<"binary 11111111 + 1 = "<<addbin("11111111", "1")<<endl; //you
should get 100000000
cout<<"binary 101010 + 10 = "<<addbin("101010", "10")<<endl<<endl;
//you should get 101100
cout<<"hexadecimal A4 + A5 = "<<addhex("A4", "A5")<<endl; //you
should get 149
cout<<"hexadecimal 2B + C = "<<addhex("2B", "C")<<endl; //you
should get 37
cout<<"hexadecimal FABC + 789 = "<<addhex("FABC", "789")<<endl;
//you should get 10245
cout<<"hexadecimal FFFFFF + FF = "<<addhex("FFFFFF",
"FF")<<endl<<endl; //you should get 10000FE
system("PAUSE");
return 0;
}
string addbin(string bin1, string bin2)
{
//IMPLEMENT THIS FUNCTION
//IMPLEMENT THIS FUNCTION
//IMPLEMENT THIS FUNCTION
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
string addbin(string, string);
string addhex(string, string);
int hexToDecimal(string hexVal)
{
int len = hexVal.size();
// Initializing base value to 1, i.e 16^0
int base = 1;
int dec_val = 0;
// Extracting characters as digits from last character
for (int i=len-1; i>=0; i--)
{
// if character lies in '0'-'9', converting
// it to integral 0-9 by subtracting 48 from
// ASCII value.
if (hexVal[i]>='0' && hexVal[i]<='9')
{
dec_val += (hexVal[i] - 48)*base;
// incrementing base by power
base = base * 16;
}
// if character lies in 'A'-'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal[i]>='A' && hexVal[i]<='F')
{
dec_val += (hexVal[i] - 55)*base;
// incrementing base by power
base = base*16;
}
}
return dec_val;
}
string decimalToHex(int n)
{
// char array to store hexadecimal number
char hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
string result = "";
// result is reverse order of hexadecimal number array
for(int j=i-1; j>=0; j--)
result = result + hexaDeciNum[j];
return result;
}
int main()
{
cout<<"binary 1101 + 1000 = "<<addbin("1101", "1000")<<endl;
//you should get 10101
cout<<"binary 11000 + 1011 = "<<addbin("11000", "1011")<<endl;
//you should get 100011
cout<<"binary 11111111 + 1 = "<<addbin("11111111", "1")<<endl;
//you should get 100000000
cout<<"binary 101010 + 10 = "<<addbin("101010", "10")<<endl<<endl;
//you should get 101100
cout<<"hexadecimal A4 + A5 = "<<addhex("A4", "A5")<<endl; //you should get 149
cout<<"hexadecimal 2B + C = "<<addhex("2B", "C")<<endl; //you should get 37
cout<<"hexadecimal FABC + 789 = "<<addhex("FABC", "789")<<endl;
//you should get 10245
cout<<"hexadecimal FFFFFF + FF = "<<addhex("FFFFFF","FF")<<endl<<endl; //you should get 10000FE
return 0;
}
string addbin(string a, string b)
{
string result = ""; // Initialize result
int s = 0; // Initialize digit sum
// Travers both strings starting from last
// characters
int i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || s == 1)
{
// Comput sum of last digits and carry
s += ((i >= 0)? a[i] - '0': 0);
s += ((j >= 0)? b[j] - '0': 0);
// If current digit sum is 1 or 3, add 1 to result
result = char(s % 2 + '0') + result;
// Compute carry
s /= 2;
// Move to next digits
i--; j--;
}
return result;
}
string addhex(string a, string b)
{
return decimalToHex(hexToDecimal(a) + hexToDecimal(b));
}
Output:
-----------
binary 1101 + 1000 = 10101
binary 11000 + 1011 = 100011
binary 11111111 + 1 = 100000000
binary 101010 + 10 = 101100
hexadecimal A4 + A5 = 149
hexadecimal 2B + C = 37
hexadecimal FABC + 789 = 10245
hexadecimal FFFFFF + FF = 10000FE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.