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