Thread: How to multiply and Divide Huge int numbers

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    How to multiply and Divide Huge int numbers

    I need some help on a program for school that I am supposed to be able to input a integer number of unlimited size and mulitply it with another as well as divide,subtract, and add them. I have the add and subtract working. I read them as a string then convert each digit to a interger stored in a vector array and work it a digit at a time for the add and subtract. It works although its a bit awkward. I have no clue on the multiply and divide. Help before I loose all my hair.

    thanks

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    i remember i made that exercise a long time a go i cant help you a lot but will try to give you the basics of the multiply and division how i did it.

    as you said you have no problems with the add, the rest of operations reduce to the add operations ie:

    to multiply 2 numbers:
    4*8 = 32
    the result is the sum of the first number until you get the second number
    or the sum of 8 times 4
    4+4+4+4+4+4+4+4 = 32

    to divide 2 numbers:
    is the inverse of the multiply
    8/2 = 4

    2+2+2+2 = 8 as you see you have "4" "2's" and 4 is the answer

    ok, as i told you i made that exercise a long time a go and cant remember how i did it or where's the source code, but thats the "key" i found to solve that problem .
    will try to find it, should be somewere

    please excuse my poor english and good luck

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You can also use basic elementary school multiplication and division methods that go one digit at a time.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    Post

    you can implement the multiply and divide using addition and substraction which you had implemented like this:
    code:
    Code:
    class longInteger {
    public:
    .........
        longInteger operator+(const longInteger& oprand);    
        longInteger operator+(int opint);    
        longInteger operator-(longInteger& oprand);
        longInteger operator*(longInteger& oprand);
        longInteger operator/(longInteger& oprand);
    ........
    private:
    ......
    
    };
    
    longInteger longInteger::operator*(longInteger& oprand) {
    longInteger result(*this),i;
        for(i=0;i<oprand;i=i+1)
            result = result + *this
        }
        return result
    }
    
    
    longInteger longInteger::operator/(longInteger& oprand) {
    longInteger result(0), val(*this);
    
             while((val = (val - oprand)) >0) {
                    result = result +1;
              }
        return result
    }

    also you have to implement the operator>, constructor,copy constructor, operator=, ...
    I just give you a algorithm, my code might not work cuz I haven't had it tested.
    Hope it could help you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Calculator - Past the basics of 2+2
    By Revekius in forum C Programming
    Replies: 11
    Last Post: 07-29-2006, 11:36 PM
  3. Help with a huge integer problem
    By MetalSmasher86 in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2006, 01:21 AM
  4. sooo close - need a little help
    By bnmwad in forum C Programming
    Replies: 3
    Last Post: 03-06-2005, 02:44 PM
  5. Functions- newbie nedds rescue
    By bnmwad in forum C Programming
    Replies: 9
    Last Post: 03-03-2005, 09:40 PM