Design an algorithm that takes two numbers in base b 2 b is

Design an algorithm that takes two numbers in base b >= 2 (b is an input parameter) and adds them up generating the result also in base b. For instance, if b = 3, and the two inputs numbers (already in their list representations) are [1, 0, 2, 2] and [2, 2, 1, 2, 1), the result should be [0, 0, 1, 2, 2). Assume that b (and so also each digit in base b) can be stored in a single machine word. Test your program by adding these numbers the numbers here are given in the standard notation, they will be \"reversed\" in the list notation that your programs will operate on): 0 and 430020104 (b = 5) 430020104 and 430020104 (b = 5) 202210101212 and 2201220 (b =3) 1010111001011110 and 10011110101 (6 = 2) 1010111001011 and 1111111010110 (b = 2)

Solution

#include <iostream>
#include <string>
#include <cctype>
#include <list>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>

using namespace std;


int returnInt(const char c){
const string number = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz$@\";
return number.find(c);
}
char returnBaseChar(const int i){
const string number = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz$@\";
return number[i % number.size()];
}
bool checkValidity(const string& number,const int& base)
{
const string bases = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz$@\";
for(int pos = 0; pos != number.size(); ++pos){
if( unsigned(returnInt(number[pos])) > base ) return false;
}
return true;
}
string makeEqualfromFront(const string& number, int zeroCount){
string str = string(number.size() + zeroCount,\'0\');
for(int pos = zeroCount; pos != number.size()+ zeroCount; ++pos)
str[pos] = number[pos-zeroCount];
return str;
}
string makeEqualFromLast(const string& number){
string str;
if(number[0] != \'0\') return string(number);
int pos = 1;
while(number[pos] == \'0\') ++pos;
std::copy(number.begin()+pos,number.end(),std::back_insert_iterator<string>(str));
return str;
}
string sumFunction(const string& number1,const string& number2, const int& base){
if(!checkValidity(number1,base) || !checkValidity(number2,base) || base <= 0 || base > 64)
return \"NULL\";
  
string startnumber = makeEqualFromLast(number1);
string endnumber = makeEqualFromLast(number2);
  
int size = startnumber.size() - endnumber.size();
  
if(size > 0)
endnumber = makeEqualfromFront(endnumber,size);
else if(size < 0)
startnumber = makeEqualfromFront(startnumber,abs(size));
else ;
  
  
list<char> result = list<char>();
bool carry = false;
for(int pos = startnumber.size()-1; pos != -1; --pos)
{
  
int digitAdded = returnInt(startnumber[pos]) + returnInt(endnumber[pos]);
if(carry){ ++digitAdded; carry ^=1; }
result.push_back(returnBaseChar(digitAdded % base));
if(digitAdded >= base) carry ^= true;
}
if(carry){ result.push_back(\'1\'); }
  
string strnumber;
while(!result.empty()){
strnumber.push_back(result.back());
result.pop_back();
}
return strnumber;
}

int main()
{
std::vector<char> inp1={\'E\',\'F\',\'F\',\'F\'};
reverse(inp1.begin(),inp1.end());
std::string input1(inp1.begin(),inp1.end());
std::vector<char> inp2={\'F\',\'a\',\'@\',\'F\'};
reverse(inp2.begin(),inp2.end());
std::string input2(inp2.begin(),inp2.end());
string res = sumFunction(input1,input2,64);

cout << input1 << \" + \" << input2 << \" = \" << res << endl;
}

 Design an algorithm that takes two numbers in base b >= 2 (b is an input parameter) and adds them up generating the result also in base b. For instance, if
 Design an algorithm that takes two numbers in base b >= 2 (b is an input parameter) and adds them up generating the result also in base b. For instance, if

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site