logic question..

  • Thread starter Thread starter Michelle S
  • Start date Start date
M

Michelle S

just a quick question about the logif for a program to add dollar and
cents memebers from too different objects together


public static Money operator + (Money m1, Money m2) {
Money tempMoney = new Money(m1);
tempMoney.dollars += m2.dollars;
tempMoney.cents += m2.cents;
if (tempMoney.cents > 99) {
dollars += cents div 100;
cents = cents - dollars % 100;
}
return tempMoney;
}


I am not sure exactly how to make use of mod to get the correct amount
of cents..

any suggestions appreciated
-----
 
Well, if we are to assume that m1 & m2 are properly normalized (cents <=
99) to start with, then we know that tempMoney.cent can never be greater
than 198. This reduces the problem to just:

if (tempMoney.cents > 99)
{
tempMoney.cents -=100;
tempMoney.dollars ++;
}

If you cannot assume that m1 & m2 are normalized --- Then fix that
problem.

Actually, what you really want to a normalizing function, this should be
called in the ctor & any modifying function:

private Normalize()
{
dollar += cents / 100;
cent = cents % 100;
}

With that, your operator reduces to:

public static Money operator + (Money m1, Money m2)
{
Money tempMoney = new Money(m1);
tempMoney.dollars += m2.dollars;
tempMoney.cents += m2.cents;
tempMoney.Normalize();
return tempMoney;
}

Note that you don't need the if (cent > 99) because Normalize will work
correctly, even if cent <= 99. It will also work correctly if cent > 199:

Assume this.dollar = 5 & this.cents = 345:

dollar += cents /100 == dollar += (345/100) == 5 + 3 = 8
cents = cent % 100 == 345 % 100 == 45

result = $8.45

Unfortunately Normalize doesn't work exactly right if cents is negative,
so you need a bit more before you implement oper-() but that is left as an
exercise for the student......

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Michelle said:
just a quick question about the logif for a program to add dollar and
cents memebers from too different objects together


public static Money operator + (Money m1, Money m2) {
Money tempMoney = new Money(m1);
tempMoney.dollars += m2.dollars;
tempMoney.cents += m2.cents;
if (tempMoney.cents > 99) {
dollars += cents div 100;
cents = cents - dollars % 100;
}
return tempMoney;
}


I am not sure exactly how to make use of mod to get the correct amount
of cents..

any suggestions appreciated
-----
tempMoney.dollars += m1.dollars + m2.dollars + math.floor((m1.cents +
m2.cents)/100);
tempmoney.cents = (m1.cents + m2.cents) % 100;

HTH
JB
 
The said:
tempMoney.dollars += m1.dollars + m2.dollars + math.floor((m1.cents +
m2.cents)/100);
tempmoney.cents = (m1.cents + m2.cents) % 100;

That doesn't work. Note that tempMoney is initialize to m1, so you're
adding it in a second time. You could fix that by initializing tempMoney
to zero, and using = instead of +=, but that's a waste of effort.
 
James said:
That doesn't work. Note that tempMoney is initialize to m1, so you're
adding it in a second time. You could fix that by initializing tempMoney
to zero, and using = instead of +=, but that's a waste of effort.
You are correct, I meant to type = instead of +=
JB
 
I dont understand, more info needed.

Are you trying to understand the Mod operator usage? Or trying to
find/convert currency?
 
public static Money operator + (Money m1, Money m2)
{
Money tempMoney = new Money(m1);
tempMoney.dollars += m2.dollars;
tempMoney.cents += m2.cents;
if (tempMoney.cents > 99)
{
dollars += cents / 100; //I assume 'cents' is of type 'int'
cents %= 100;
}
return tempMoney;
}

I dont understand, more info needed.

Are you trying to understand the Mod operator usage? Or trying to
find/convert currency?



--
Regards,
Nurchi BECHED

P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it blows away your whole leg."
--Bjarne Stroustrup
 
This is a school test-question, right? not some impl of something you
would like to sell?

Having a class for Money, which furthermore splits into dollars and
cents sound very much like complicating simple things... atleast to me.

If you want to represent cents exactly, you get rounding problems at
$.01/2 and so on. A much better representation to base your calculations
on would be double.

You are also on thin

Now for your answer:

1. You haven't even run this through a compiler,... The line with the
comment would produce an error message, and good thing operator+ is
static, otherwise your code would compile but be really bad.

2. in C#, % (atleast for ints) are defined as "the value produced by
x – (x / y) * y", which actually makes your %-calculations work, also
for negative numbers :)

3. why not write it a bit more readable:

public static Money operator+(Money m1, Money m2)
{
int cents = m1.cents + m2.cents;
return new Money(m1.dollars + m2.dollars + cents / 100, cents % 100);
}
 
Back
Top