[ create a new paste ] login | about

Link: http://codepad.org/fd1ijnze    [ raw code | fork ]

C++, pasted on Jan 11:
#include <iostream>
#include <string>
using namespace std;


unsigned int SymbolToNativeIndex(unsigned char chVal)
{
    unsigned int nVal = 0;
    if (chVal == '0')
    {
        nVal = 0;
    }
    else if (chVal == '1')
    {
        nVal = 1;
    }
    else if (chVal == '2')
    {
        nVal = 2;
    }
    else if (chVal == '3')
    {
        nVal = 3;
    }
    else if (chVal == '4')
    {
        nVal = 4;
    }
    else if (chVal == '5')
    {
        nVal = 5;
    }
    else if (chVal == '6')
    {
        nVal = 6;
    }
    else if (chVal == '7')
    {
        nVal = 7;
    }
    else if (chVal == '8')
    {
        nVal = 8;
    }
    else if (chVal == '9')
    {
        nVal = 9;
    }

    return nVal;
}

unsigned char GetNext(unsigned char chVal)
{
    unsigned char chValB = chVal;

    if (chValB == '0')
    {
        chValB = '1';
    }
    else if (chValB == '1')
    {
        chValB = '2';
    }
    else if (chValB == '2')
    {
        chValB = '3';
    }
    else if (chValB == '3')
    {
        chValB = '4';
    }
    else if (chValB == '4')
    {
        chValB = '5';
    }
    else if (chValB == '5')
    {
        chValB = '6';
    }
    else if (chValB == '6')
    {
        chValB = '7';
    }
    else if (chValB == '7')
    {
        chValB = '8';
    }
    else if (chValB == '8')
    {
        chValB = '9';
    }
    else if (chValB == '9')
    {
        chValB = '0';
    }

    return chValB;
}

unsigned char GetNextDown(unsigned char chVal)
{
    unsigned char chValB = chVal;

    if (chValB == '0')
    {
        chValB = '9';
    }
    else if (chValB == '1')
    {
        chValB = '0';
    }
    else if (chValB == '2')
    {
        chValB = '1';
    }
    else if (chValB == '3')
    {
        chValB = '2';
    }
    else if (chValB == '4')
    {
        chValB = '3';
    }
    else if (chValB == '5')
    {
        chValB = '4';
    }
    else if (chValB == '6')
    {
        chValB = '5';
    }
    else if (chValB == '7')
    {
        chValB = '6';
    }
    else if (chValB == '8')
    {
        chValB = '7';
    }
    else if (chValB == '9')
    {
        chValB = '8';
    }

    return chValB;
}

std::string InitBlankNumber()
{
    std::string strVal;
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    strVal.push_back('0');
    return strVal;
}

// numbers must be of fixed size 10 characters
// 0000000000 to 9999999999 (expected input range)


std::string GetNextNumber(std::string strNumber)
{
    std::string strVal;
    strVal = InitBlankNumber();
    unsigned char chIndex = '9';

    if (strNumber[SymbolToNativeIndex(chIndex)] != '9')
    {

        strVal[SymbolToNativeIndex(chIndex)] = GetNext(strNumber[SymbolToNativeIndex(chIndex)]);
        chIndex = GetNextDown(chIndex);

        while (chIndex != '0')
        {
            strVal[SymbolToNativeIndex(chIndex)] = strNumber[SymbolToNativeIndex(chIndex)];
            chIndex = GetNextDown(chIndex);
        }

        strVal[SymbolToNativeIndex(chIndex)] = strNumber[SymbolToNativeIndex(chIndex)];


    }
    else if (strNumber[SymbolToNativeIndex(chIndex)] == '9')
    {

        strVal[SymbolToNativeIndex(chIndex)] = '0';
        chIndex = GetNextDown(chIndex);

        bool bStillNine = true;
        while (bStillNine == true)
        {
            bStillNine = (strNumber[SymbolToNativeIndex(chIndex)] == '9');
            if (bStillNine == true)
            {
                strVal[SymbolToNativeIndex(chIndex)] = '0';
            }
            else
            {
                strVal[SymbolToNativeIndex(chIndex)] = GetNext(strNumber[SymbolToNativeIndex(chIndex)]);
                chIndex = GetNextDown(chIndex);
                bStillNine = false;
                break;
            }

            chIndex = GetNextDown(chIndex);
            if (chIndex == '0' && bStillNine == true)
            {
                if (strNumber[SymbolToNativeIndex(chIndex)] == '9')
                {
                    strVal[SymbolToNativeIndex(chIndex)] = '0';
                }
                else
                {
                    bStillNine = false;
                }

                break;
            }
        }

        while (chIndex != '0')
        {
            strVal[SymbolToNativeIndex(chIndex)] = strNumber[SymbolToNativeIndex(chIndex)];
            chIndex = GetNextDown(chIndex);
        }

        if (chIndex == '0' && bStillNine == false)
        {
            strVal[SymbolToNativeIndex(chIndex)] = strNumber[SymbolToNativeIndex(chIndex)];
        }

    }

    return strVal;
}

std::string AddNumberToNumber(std::string l, std::string r)
{
    std::string strResult = l;

    std::string strCounter = InitBlankNumber();

    while (strCounter != r)
    {
        strResult = GetNextNumber(strResult);
        strCounter = GetNextNumber(strCounter);
    }

    return strResult;
}

std::string MultiplyNumberByNumber(std::string l, std::string r)
{
    std::string strResult = InitBlankNumber();

    std::string strCounter = InitBlankNumber();

    while (strCounter != r)
    {

        strResult = AddNumberToNumber(strResult, l);
        strCounter = GetNextNumber(strCounter);
    }

    return strResult;

}


std::string GetInput();
void Test();

int main()
{
    Test();
    return 0;
}

std::string GetInput()
{
    std::string strInput;
    getline(cin, strInput);
    return strInput;
}

void Test()
{

    while (true)
    {

        cout << "Input value A: " << endl;
        std::string strA = GetInput();
        if (strA.size() != 10)
        {
            strA = InitBlankNumber();
            cout << "Number should only be length of 10. Value will be default of: " << InitBlankNumber() << endl;
        }

        cout << "Input value B: " << endl;
        std::string strB = GetInput();
        if (strB.size() != 10)
        {
            strB = InitBlankNumber();
            cout << "Number should only be length of 10. Value will be default of: " << InitBlankNumber() << endl;
        }

        //cout << AddNumberToNumber(strA, strB) << endl;
        cout << MultiplyNumberByNumber(strA, strB) << endl;

        cout << "Press q to quit otherwise input something else to continue." << endl;
        if (GetInput() == "q")
        {
            break;
        }
    }


    cout << "Any key plus enter to exit." << endl;
    GetInput();
}


Create a new paste based on this one


Comments: