/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// custom function for conversion of number in string to number in decimal
int convertFunction(string strNumber, int pBase);
// TODO 1: develop the DecimalAddFunction(string, string) return the decimal
int decimalAddFunction(string strNumberA, string strNumberB, int pBase);
// TODO 2: develop the ConvertDecimalToBinary(int) return the binary string
// hint on log2 function to determine the length of the binary with given decimal, https://www.omnicalculator.com/math/log-2
string convertDecimalToBinary(int pDecimal);
char convertIntToChar(int pValue);
// TODO 3: use functions from TODO 1 and 2 in BinaryAdd(string pBinNum1, string pBinNum2) return the binary string
string binaryAdd(string pBinNum1, string pBinNum2);
int main()
{
// local variables
string strNumA, strNumB;
int nBase=2;
int nDecimalA=0, nDecimalB=0;
cout << "What is the base type (2, 10)?";
cin >> nBase;
cout << "Enter string A:";
cin >> strNumA;
cout << "Enter string B:";
cin >> strNumB;
// loop to convert the binary string into decimal
nDecimalA = convertFunction(strNumA, nBase);
// loop to convert the binary string into decimal
nDecimalB = convertFunction(strNumB, nBase);
int sum=nDecimalA+nDecimalB;
cout << "\nSum: " << sum << endl;
return 0;
}
char convertIntToChar(int pValue){
// TODO: validate on the pValue to ensure the range of 0 or 1
return (char)(pValue+48);
}
int convertFunction(string strNumber, int pBase){
// local variables
int nDecimal=0;
int nLength = strNumber.length();
for(int i=0; i<nLength; i++){
cout << strNumber[i];
nDecimal+=((strNumber[i]-48)*pow(pBase,nLength-1-i));
}
cout << "\nFinal nDecimal: " << nDecimal << endl;
return nDecimal;
}