Jump to content

Recommended Posts

Posted (edited)

I wanted to write a program that can add numbers. I avoided using built in addition and for loops . I avoided less than and greater than signs. I did use however equal to signs and not equal to signs.

 

If the forum moderators can guarantee this topic won't be lost I will permit them to move it to developer's corner. I notice developer's corner topics fade away.

 

YW84ABw.png

WARNING: Try to avoid the first 2 to 3 digits in the second number unless you're willing to wait for hours or maybe a day.

 

http://codepad.org/FZ1zXFNb

 

#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 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 << "Press q to quit otherwise input something else to continue." << endl;
        if (GetInput() == "q")
        {
            break;
        }
    }


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

Edited by Prosper
  • Like 1

redacted

Posted

 

#include<iostream>
#include <conio.h>
using namespace std;
main()
{
int a,b,c;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
c=a+b;
cout<<"Sum of two numbers are"<<c;
cout<<c<<endl;
getch();
}

*** "The words of someone who feels ever more the ent among saplings when playing CRPGs" ***

 

Posted (edited)

For multiplication. Multiplication of course can be even slower if you pick high enough values.

 

2RpT50Z.png

 

http://codepad.org/fd1ijnze

 

 

#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();
}
Edited by Prosper

redacted

Posted (edited)
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
main(){
int a,b;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
cout<<"Sum of two numbers is: "<< a + b <<endl;
system("PAUSE");
}

fixed  :brows: 

Prosper did say "avoid + sign", so you don't get a cookie, I'm afraid  :skull:

Edited by sorophx
  • Like 1
Walsingham said:

I was struggling to understand ths until I noticed you are from Finland. And having been educated solely by mkreku in this respect I am convinced that Finland essentially IS the wh40k universe.

Posted

Prosper: I draw my gun before even reading your post correctly, it seems! Sorry.

 

sorophx: Yup! One include had to go then, and then cue system("PAUSE"); Thank you, or should I say, Kiitos! ;)

*** "The words of someone who feels ever more the ent among saplings when playing CRPGs" ***

 

Posted

Would it work better with 2D arrays? I.e. adding 3 and 2 => result in array element [3][2].

"It has just been discovered that research causes cancer in rats."

Posted

logical operations are the way to go

Walsingham said:

I was struggling to understand ths until I noticed you are from Finland. And having been educated solely by mkreku in this respect I am convinced that Finland essentially IS the wh40k universe.

Posted

While in practice you'd never do it this way, it's actually an interesting exercise in terms of getting practice.

 

I probably would use a switch statement as, for whatever reason, I find them more pleasing to look at than a series of if-else-if statements.

 

 

Note that a line such as the following:

else if (strNumber[SymbolToNativeIndex(chIndex)] == '9')
Given that your first if statement checks the entire space that is mutually exclusive from this, you don't need to perform the logical check here.

 

 

A useful exercise would be to look over the code and determine, specifically, why it's so slow with particularly large numbers and to see if there's any way you could improve upon the existing code to speed it up (potentially a lot).

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...