|
-
Jan 31st, 2011, 02:30 PM
#1
Thread Starter
Just Married
[RESOLVED] Trying to find best code
Friend I am trying to find best code so I can convert cents into minimum format here is my function
Code:
public static Change getChange(int cents, Change availableChange)
{
/*
* This method should take two parameters
1.cents as an Integer, and
2.availableChange as a Change object (see below)
* It should return the same amount, or the maximum amount
possible,
in dollars and coins given what change is available in the
availableChange parameter.
* It should use the minimum number of coins possible,
given what is available in availableChange.
For example: 164 cents = 1 dollar, 2 quarters, 1 dime and 4
cents.
* Return null if the parameter is negative.
*/
int dollars; // 100 cents
int quarters; //25 cents
int dimes; // 10 cents
int nickels; // 5 cents
int centbalance; // 1 cent
if (cents < 0)
return null;
if (cents >= 100)
quarters = Math.DivRem(cents, 100, out dollars);
//if (quarters >= 25)
// quarters = Math.DivRem(quarters, 25, out dollars);
//availableChange = new Change(dollars, quarters, dimes, nickels, centbalance);
return availableChange;
}
public class Change
{
private int _dollars; // 100 cents
private int _quarters; //25 cents
private int _dimes; // 10 cents
private int _nickels; // 5 cents
private int _cents; // 1 cent
public Change(int dollars, int quarters, int dimes, int nickels,
int cents)
{
_dollars = dollars;
_quarters = quarters;
_dimes = dimes;
_nickels = nickels;
_cents = cents;
}
public int getDollars()
{
return _dollars;
}
public int getQuarters()
{
return _quarters;
}
public int getDimes()
{
return _dimes;
}
public int getNickels()
{
return _nickels;
}
public int getCents()
{
return _cents;
}
}
Update me if you have idea about it or have shortest code!
Shakt
-
Jan 31st, 2011, 03:40 PM
#2
Re: Trying to find best code
If you are using 3.0, you can use auto-implemented properties for variables.
c# Code:
public int dollars {get; set;}
will do that job instead of
c# Code:
private int _dollars; public int dollars{ return _dollars; }
That's one way of making your code more readable.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jan 31st, 2011, 03:44 PM
#3
Thread Starter
Just Married
Re: Trying to find best code
I did this code let me know some one have better way!
C# Code:
public static Change getChange(int cents, Change availableChange)
{
int dollars = 0; // 100 cents
int quarters = 0; //25 cents
int dimes = 0; // 10 cents
int nickels = 0; // 5 cents
int centbalance = 0; // 1 cent
if (cents >= 100)
dollars = Math.DivRem(cents, 100, out quarters);
else
quarters = cents;
if (quarters >= 25)
quarters = Math.DivRem(quarters, 25, out dimes);
else
dimes = quarters;
if (dimes >= 10)
dimes = Math.DivRem(dimes, 10, out nickels);
else
nickels = dimes;
if (nickels >= 5)
nickels = Math.DivRem(nickels, 5, out centbalance);
else
centbalance = nickels;
availableChange = new Change(dollars, quarters, dimes, nickels, centbalance);
return availableChange;
}
-
Jan 31st, 2011, 04:07 PM
#4
Re: Trying to find best code
Here's what I did to your code
c# Code:
public struct Change
{
public int dollars;
public int quarters;
public int dimes;
public int nickles;
public int pennies;
}
public static Change getChange(int cents)
{
Change mychange = new Change();
if (cents > 100)
mychange.dollars = Math.DivRem(cents, 100, out mychange.quarters);
else
mychange.quarters = cents;
if (mychange.quarters > 25)
mychange.quarters = Math.DivRem(mychange.quarters, 25, out mychange.dimes);
else
mychange.dimes = mychange.quarters;
if (mychange.dimes > 10)
mychange.dimes = Math.DivRem(mychange.dimes, 10, out mychange.nickles);
else
mychange.nickles = mychange.dimes;
if (mychange.nickles > 5)
mychange.nickles = Math.DivRem(mychange.nickles, 5, out mychange.pennies);
return mychange;
}
You can use a class called change instead of a struct
Here's the calling program
c# Code:
static void Main(string[] args)
{
Change myc = new Change();
myc = getChange(2430);
Console.WriteLine(String.Format("Dollars:{0},Quarters:{1},Dimes:{2},Nickles:{3},Pennies{4}",myc.dollars, myc.quarters, myc.dimes, myc.nickles, myc.pennies));
}
The only thing I would like to change is send the value as an argument to Main instead of hardcoding it like that.
Does this make it easier to read?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jan 31st, 2011, 04:10 PM
#5
Re: Trying to find best code
I just realized that above code has a bug in it.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jan 31st, 2011, 04:19 PM
#6
Re: Trying to find best code
I think this change should fix it.
c# Code:
public static Change getChange(int cents)
{
Change mychange = new Change();
if (cents > 100)
mychange.dollars = Math.DivRem(cents, 100, out mychange.quarters);
else
{
mychange.quarters = cents;
mychange.dollars = 0;
}
if (mychange.quarters > 25)
mychange.quarters = Math.DivRem(mychange.quarters, 25, out mychange.dimes);
else
{
mychange.dimes = mychange.quarters;
mychange.quarters = 0;
}
if (mychange.dimes > 10)
mychange.dimes = Math.DivRem(mychange.dimes, 10, out mychange.nickles);
else
{
mychange.nickles = mychange.dimes;
mychange.dimes = 0;
}
if (mychange.nickles > 5)
mychange.nickles = Math.DivRem(mychange.nickles, 5, out mychange.pennies);
else
{
mychange.pennies = mychange.nickles;
mychange.nickles = 0;
}
return mychange;
}
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jan 31st, 2011, 06:12 PM
#7
Re: Trying to find best code
Code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MoneyChange
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("How much money should be changed to the best common amounts?");
string sAmount = Console.ReadLine();
decimal iAmount = 0m;
if(!decimal.TryParse(sAmount.Replace("$", ""), out iAmount))
{
Console.WriteLine("The amount entered could not be converted to a number. Program exiting.");
return;
}
int iCents = (int) (iAmount * 100m);
List<MoneyDenomination> MoneyList = MoneyDenomination.CreateKnownMoney();
List<MoneyAmount> MoneyAmounts = new List<MoneyAmount>();
//The following assumes the money list is sorted from greatest to least
foreach(MoneyDenomination MoneyItem in MoneyList)
{
//See if we can divide it in to the remaining amount
int iNumberInAmount = iCents / MoneyItem.CentsValue;
if(iNumberInAmount > 0)
{
//This is significant change
MoneyAmounts.Add(new MoneyAmount(MoneyItem, iNumberInAmount));
iCents -= MoneyItem.CentsValue * iNumberInAmount;
if(iCents == 0)
break;
}
}
Console.WriteLine("-----------------------------------");
Console.WriteLine(string.Format("The best way to represent ${0} in change is:", iAmount));
foreach(MoneyAmount Amount in MoneyAmounts)
{
Console.WriteLine(string.Format("\t{0}\t{1}", Amount.Amount, Amount.MoneyItem.CommonName));
}
Console.WriteLine("Press <ENTER> Key To Exit");
Console.ReadLine();
}
}
public class MoneyAmount
{
public MoneyDenomination MoneyItem { get; set; }
public int Amount { get; set; }
public MoneyAmount(MoneyDenomination Money, int Amounts)
{
MoneyItem = Money;
Amount = Amounts;
}
}
public class MoneyDenomination
{
public string CommonName { get; set; }
public int CentsValue { get; set; }
public MoneyDenomination(string Name, int Cents)
{
CommonName = Name;
CentsValue = Cents;
}
public static List<MoneyDenomination> CreateKnownMoney()
{
List<MoneyDenomination> Money = new List<MoneyDenomination>();
Money.Add(new MoneyDenomination("Hundred Dollar Bills", 10000));
Money.Add(new MoneyDenomination("Ten Dollar Bills", 1000));
Money.Add(new MoneyDenomination("Five Dollar Bills", 500));
Money.Add(new MoneyDenomination("One Dollar Bills", 100));
Money.Add(new MoneyDenomination("Half Dollars", 50));
Money.Add(new MoneyDenomination("Quarters", 25));
Money.Add(new MoneyDenomination("Dimes", 10));
Money.Add(new MoneyDenomination("Nickels", 5));
Money.Add(new MoneyDenomination("Pennies", 1));
return Money;
}
}
}
Sample Output (tested):
Code:
How much money should be changed to the best common amounts?
233.68
-----------------------------------
The best way to represent $233.68 in change is:
2 Hundred Dollar Bills
3 Ten Dollar Bills
3 One Dollar Bills
1 Half Dollars
1 Dimes
1 Nickels
3 Pennies
Press <ENTER> Key To Exit
Comment out the Half Dollars line if you don't want them.
Last edited by Lord_Rat; Jan 31st, 2011 at 06:19 PM.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|