Log in

View Full Version : oshi - C# programming question.


Jean Poutine
November 18th, 2008, 11:03 PM
so I have a function that takes an amount entered by the user (let's say 2.48) and decomposes it optimally into 2.00, 1.00, 0.25, 0.10, 0.05 and 0.01 coins, then reads the amounts of coins and stores them in a listbox. problem is that to do that, I used a series of loops that aren't very aesthetically pleasing, especially as my other functions were so short.

I was simply wondering if there were a cuter way to do this. I'm all about the style, babeh, and I certainly wouldn't bring my function into bed, since it's so boring and always says the same things.

it seemed logical at first but then again I never bother to write an algorithm first and now I'm not so sure since the function is so redundant. I'm also not so sure if for is the right loop to use, although I used do...while and it screwed up. I haven't tried while, but for seems appropriate, plus it works.


private void button2_Click(object sender, EventArgs e)
{

//declares variables
double montant;
int pieces=0;
string resultat;

//takes value from textbox and stores it into variable
montant = Convert.ToDouble(txtMontant.Text);

//multiple loops counting the number of coins (1 loop = 1 value) inside the balance and countin' each loop...any better way to do this?
for (montant = montant; montant >= 2.00; pieces++)
montant = montant - 2.00;

resultat = string.Format("{0:d} pièces de 2.00$", pieces);
lbPenis.Items.Add(resultat);
pieces = 0;

for (montant = montant; montant >= 1.00; pieces++)
montant = montant - 1.00;

resultat = string.Format("{0:d} pièces de 1.00$", pieces);
lbPenis.Items.Add(resultat);
pieces = 0;

for (montant = montant; montant >= 0.25; pieces++)
montant = montant - 0.25;

resultat = string.Format("{0:d} pièces de 25 cents", pieces);
lbPenis.Items.Add(resultat);
pieces = 0;

for (montant = montant; montant >= 0.10; pieces++)
montant = montant - 0.10;

resultat = string.Format("{0:d} pièces de 10 cents", pieces);
lbPenis.Items.Add(resultat);
pieces = 0;

for (montant = montant; montant >= 0.05; pieces++)
montant = montant - 0.05;

resultat = string.Format("{0:d} pièces de 5 cents", pieces);
lbPenis.Items.Add(resultat);
pieces = 0;

for (montant = montant; montant >= 0.01; pieces++)
montant = montant - 0.01;

resultat = string.Format("{0:d} pièces de 1 cent", pieces);
lbPenis.Items.Add(resultat);
pieces = 0;

}

//done...though it's not very aesthetically pleasing


yeah...I called my listbox "penis". the main clutter is that I need to show the result after every loop and reset "pieces" (which counts the coins) to 0 after every loop. if I do it inside the loop it'll keep resetting itself, and if I don't do it it'll take the other coins in consideration. I could technically use a separate variable for every coin type, but why do that? I can't figure out how to do that with less loops. any help is appreciated.

keep in mind the function DOES work...it's just not very tidy. or stylish. and as I said, style is the name of the game for meh.

thanks!

Kiros
November 19th, 2008, 08:25 AM
This can be optimized to be less code, but the memory size will be just a bit more because of the arrays. However, I'm pretty sure that the performance will also be improved.

//KiroS

struct MoneyInfo
{
double PieceValue;
unsigned long Pieces;
string PieceName;
};

private void button2_Click(object sender, EventArgs e)
{
double *montant = new double(Convert.ToDouble(txtMontant.Text));
MoneyInfo *CurrMoney = new MoneyInfo[6];

CurrMoney[0]->PieceValue = 2.00;
CurrMoney[1]->PieceValue = 1.00;
CurrMoney[2]->PieceValue = 0.25;
CurrMoney[3]->PieceValue = 0.10;
CurrMoney[4]->PieceValue = 0.05;
CurrMoney[5]->PieceValue = 0.01;

CurrMoney[0]->PieceName = "$2.00\0";
CurrMoney[1]->PieceName = "$1.00\0";
CurrMoney[2]->PieceName = "25 cents\0";
CurrMoney[3]->PieceName = "10 cents\0";
CurrMoney[4]->PieceName = "5 cents\0";
CurrMoney[5]->PieceName = "1 cent\0";

for (usigned short i = 0; *montant > 0.00 && i < 6; ++i)
{
while (*montant > CurrMoney[i]->PieceValue)
{
*montant -= CurrMoney[i]->PieceValue;
++CurrMoney[i]->Pieces;
}

lbPenis.Items.Add(CurrMoney[i]->Pieces + " pièces de " + CurrMoney[i]->PieceName);
}

delete montant;
delete CurrMoney[];
}

I would just like to stress that this may not work because of a few factors: 1) I forgot if the null character ('\0') is needed for strings, but I've added them anyway; 2) I've never needed to use structs in any form of C before, so I may be forgetting something with them; I'm pretty sure that the double class has a constructor that can take a value, but I could be wrong; and 4) I haven't in native code for quite some time, so there may be errorous code in there that I'm overlooking.

Anyway, that's the idea of it.

Kiros
November 20th, 2008, 11:10 AM
Does that code solve your problem? ô.o

Jean Poutine
November 20th, 2008, 03:52 PM
I understand the concept, but I turned my project in before : (

Kiros
November 21st, 2008, 03:56 AM
Oh, well if your doing this for a class like Computer Science, then it would be best for you to learn as you go within that class. You don't want to show off work that you didn't design and write ;)

Jean Poutine
November 21st, 2008, 02:48 PM
you're absolutely right, in fact as I am a proud person I probably wouldn't have used it anyway. I like to turn in my own work.

that being said, i understand the logic behind your code and I'll keep this method in mind next time I end up with over 9000 loops. : D

Kiros
November 21st, 2008, 03:17 PM
Good to hear! Good luck with your future programs.

Skhorpion
November 22nd, 2008, 02:24 AM
I wrote a calculator in C once.

It was amazing.