/*
    Framework for implementing addition.

    We use a string to store the digits, this allows for arbitrary large numbers!
    I have made no assumptions about base 10, hence the code works with digits up
    up to base 35.

Characer  '0' '1'  ..  '8' '9' 'a'  'b'  'c' ..   'z'
base X:    0   1   ..   8   9   a    b    c  ..    z
base 10:   0   1   ..   8   9  10   11   12  ..   35

Hence in base 16, we would use digits  0123456789abcdef

   Since strings stores char, when converted to integers we get the ASCII
   value of the character  int('0')=48  and int('a')=97 we need to convert
   digits to integers with digit = int(c)-int('0'))   when the character is a digit
                      and  digit = int(c)-int('a')+10 when the character is a letter.

*/

#include <iostream>     // cin/cout
#include <algorithm>    // Reverse
#include <string>
using namespace std;

int getdigit(const string& s, int n)
// string  s="43210";  then we count from the left to the right:
//                     then s[0]= '4', s[2]='2', and s[5]='0'
//
// This function returns the digit as an integer, but counting from right to the left
// getdigit("43210",0) = 0, getdigit("43210",2) = 2, digit("43210",4) = 4
// getdigit("43210",5) does not give an error, but returns 0, since "043210" is the same number
//
{
    if (n>=s.length()) return 0;                               // n is 0 .. s.length()-1
    int m=s.length()-1-n;                                      // m is s.length()-1 .. 0
    
    if (s[m]>='0' && s[m]<='9') return int(s[m])-int('0');
    else return 0;                                             // wtf!?
}

void setdigit(string& s, int n, int digit)
// This function sets a the digit in a string assuming it is large enough and contains zeros.
// setdigit("0000",0,1) = "0001", digit("43210",2,9) = "43910"
//
{
    while (n>=s.length()) s="0"+s;                             // zero pad.
    int m=s.length()-1-n;                                      // m is s.length()-1 .. 0
    
    s[m]=char(digit+int('0'));
}


int main()
{
    string number1= "012345";
    string number2= "345678";
    string result ="0000000";   // Result should be large enough to fit the sum of number1 and number2.
    
    // Illustrating digit extraction
    cout << "digit(\"43210\",0) =" << getdigit("43210",0) << endl;
    cout << "digit(\"43210\",2) =" << getdigit("43210",2) << endl;
    cout << "digit(\"43210\",4) =" << getdigit("43210",4) << endl;
    cout << "digit(\"43210\",5) =" << getdigit("43210",5) << endl;
    cout << "digit(\"43210\",10)=" << getdigit("43210",10) << endl;
    
    // Illustrating setting digits
    cout << "result=" << result << "\n";    
    setdigit(result, 0, 9);
    cout << "result=" << result << "\n";    
    setdigit(result, 1, 8);
    cout << "result=" << result << "\n";    
    setdigit(result, 2, 6);
    cout << "result=" << result << "\n";    
    setdigit(result, 2, 7);
    cout << "result=" << result << "\n";    
    setdigit(result, 4, 9);
    cout << "result=" << result << "\n";    
    
    // Write your code here
    
    cout << number1 << "+" << number2 << " = " << result << endl;
    
    // Optional assignment:
    // when the code works in base 10, change it to work in any base.
    // NB You can check with base 16, since C++ can do hex math:
    //  cout << "0x10+0x20 = " << 0x10+0x20 << " (decimal) = " << hex << 0x10+0x20 << " (hex)" << endl;
    
    // Optional assignment:
    // Calculate the product. (more difficult)
}

