Dividing strings filled with numeric values

M

M. Akkerman

Hi,

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 {
private:
vector<char> number;
public:
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?

TIA,
Mike
 
J

Jeff Schwab

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
 
J

Jon Bell

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

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

Those should be vector said:
what I want todo is result = numberone/numbertwo;

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

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.
 
A

Alf P. Steinbach

I have the following class

class TBigNumber {
private:
vector<char> number;
public:
void operator/=(TBigNumber &Divider);
}
....

what I want todo is result = numberone/numbertwo;

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


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.

<ot>
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.
</ot>
 
M

M. Akkerman

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

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? :)
 
J

Jeff Schwab

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

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.
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.

The value assigned to a is the value returned by b/c, not the value
returned by the = operator.
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.

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.
Anywayz, thanks for trying to help but could we please stick to the
subject? :)

I thought this was the subject... If you're asking how to divide big
numbers by each other, check out Gnu GMP.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top