Dividing strings filled with numeric values

Discussion in 'C++' started by M. Akkerman, Dec 13, 2003.

  1. M. Akkerman

    M. Akkerman Guest


    I've been working on this problem for a while now but I just can't
    seem to find a good solution for it.

    Here is the situation.

    I have the following class

    class TBigNumber {
    vector<char> number;
    void operator/=(TBigNumber &Divider);

    The actual class is bigger (it can also do Add, Subtract and Multiply)
    but that's not important rightnow.

    vector<char> number is set with numeric char values, no need to check
    wether it is or is not numeric because that's already been done while
    creating the instance.

    For simplification you could say I have this:
    char numberone[128]; // filled with numeric characters
    char numbertwo[128]; // filled with numeric characters
    char result[128]; // filled with numeric characters

    what I want todo is result = numberone/numbertwo;

    Could anybody give me a few pointers on howto do this?

    M. Akkerman, Dec 13, 2003
    1. Advertisements

  2. M. Akkerman

    Jeff Schwab Guest

    void operator/=(TBigNumber &Divider);

    Why is that void? Return *this by const reference. Then, you can
    provide the top-level division operator like this:

    TBigNumber operator / ( TBigNumber const& a, TBigNumber const& b )
    TBigNumber result = a;

    return result /= b;

    Jeff Schwab, Dec 13, 2003
    1. Advertisements

  3. M. Akkerman

    Jon Bell Guest

    If you already have addition, subtraction and multiplication operations,
    you should be able to emulate the procedure that you use when dividing
    numbers by hand.
    Jon Bell, Dec 13, 2003

  4. The only C++-related part of that question is how to be able to use
    ordinary expression syntax. For that, have your 'operator /' return
    a TBigNumber, either by value or as reference to const.

    While implementing an acceptable division operation you might find it
    useful to have some way of checking the results.

    For that, consider first implementing a divide-by-2, and using that and
    and a simple search (bisection, Newton's, whatever) to compute the quotient
    in a simple-to-program, correct but extremely inefficient way.
    Alf P. Steinbach, Dec 13, 2003
  5. M. Akkerman

    M. Akkerman Guest

    because /= doesn't have to retun anything, it doesn't make sense to
    have it return something, even in your example.

    a = b / c means you want to divide the value of b with the value of c
    and assign that to a. so the equation has to return something or else
    a won't have anything to be assigned.

    a /= b means that you want to divide the value of a with the value of
    b. The equation doesn't have to return anything because the operation
    is already being performed on a.

    Anywayz, thanks for trying to help but could we please stick to the
    subject? :)
    M. Akkerman, Dec 13, 2003
  6. M. Akkerman

    Jeff Schwab Guest

    Suppose i and j are fundamental C++ types, e.g. int's. You can do this:

    int k = i /= j;

    But if i and j are types for which /= doesn't return anything, the above
    statement breaks. It's a good idea to have these operators behave as
    much as possible like the built-ins.
    The value assigned to a is the value returned by b/c, not the value
    returned by the = operator.
    It doesn't have to, but it ought to. The statement may have other
    purposes than the side-effect of putting a value into a.
    I thought this was the subject... If you're asking how to divide big
    numbers by each other, check out Gnu GMP.
    Jeff Schwab, Dec 14, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.