By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Dividing strings filled with numeric values

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> 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

M. Akkerman wrote:
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


Jul 22 '05 #2

P: n/a
In article <t5********************************@4ax.com>,
M. Akkerman <no@spam.com> wrote:

class TBigNumber {
private:
vector<char> number;
public:
void operator/=(TBigNumber &Divider);
}
Just out of curiosity, why are you using a 'vector<char>' rather than a
'string'?
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<char> numberone(128), etc, right?
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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #3

P: n/a
On Sat, 13 Dec 2003 19:47:11 +0100, M. Akkerman <no@spam.com> wrote:
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>

Jul 22 '05 #4

P: n/a
On Sat, 13 Dec 2003 17:26:40 -0500, Jeff Schwab <je******@comcast.net>
wrote:
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


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? :)
Jul 22 '05 #5

P: n/a


M. Akkerman wrote:
On Sat, 13 Dec 2003 17:26:40 -0500, Jeff Schwab <je******@comcast.net>
wrote:

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

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.

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.