C# 如何把钱分类成不同的纸币?

C# 如何把钱分类成不同的纸币?,c#,loops,C#,Loops,我正忙着制作一个“现金支付”程序,并试图找出如何从收银机中将一笔钱按从高到低的不同价值(纸币和硬币)分类。我真的在努力思考如何以有效的方式做到这一点。 我的想法是这样的: while (sum>=0){ if(sum <0 R200&& sum % 200 !>0){ //R200 is the amount of 200 rand notes in the register sum = sum - 200.00; }//endif }//

我正忙着制作一个“现金支付”程序,并试图找出如何从收银机中将一笔钱按从高到低的不同价值(纸币和硬币)分类。我真的在努力思考如何以有效的方式做到这一点。 我的想法是这样的:

while (sum>=0){
   if(sum <0 R200&& sum % 200 !>0){   //R200 is the amount of 200 rand notes in the register
   sum = sum - 200.00;
   }//endif
}//endwhile
enum CashType { c1, c5, c10, c25, c50, d1, d2, d5, d10, d20, d50, d100 }
var denominationsToCentValue = new Dictionary<CashType, int>()
{
    { CashType.d100, 10000 },
    { CashType.d50, 5000 },
    { CashType.d20, 2000 },
    { CashType.d10, 1000 },
    { CashType.d5, 500 },
    { CashType.d2, 200 },
    { CashType.d1, 100 },
    { CashType.c50, 50 },
    { CashType.c25, 25 },
    { CashType.c10, 10 },
    { CashType.c5, 5 },
    { CashType.c1, 1 }
};


var sortedCash = new Dictionary<CashType, int>();
var cashInRegister = 342.11M;
int cashInRegisterInCents = (int)(100 * cashInRegister);

foreach (var denom in denominationsToCentValue)
{
    int count = cashInRegisterInCents / denom.Value;
    cashInRegisterInCents -= count * denom.Value;

    sortedCash.Add(denom.Key, count);
}
while(总和>=0){
如果(总和0){//R200是寄存器中200兰特纸币的金额
总和=总和-200.00;
}//恩迪夫
}//结束时

但是,当我尝试这个方法时,我最终得到了一个无限循环,或者它实际上没有减去数量,你不需要使用循环,你可以简单地除以钱,然后使用余数操作符从总的钱中减去它

可编译示例:

public class Program
{
    static void Main(string[] args)
    {
        decimal money = 255.50M;

        int hundredDollarBills = subtract(ref money, 100);
        int fiftyDollarBills = subtract(ref money, 50);
        int twentyDollarBills = subtract(ref money, 20);
        int tenDollarBills = subtract(ref money, 10);
        int singleDillarBills = subtract(ref money, 1);
        int fiftyCentCoins = subtract(ref money, 0.50M);
        int twentyCentCoins = subtract(ref money, 0.20M);
        int tenCentCoins = subtract(ref money, 0.10M);
        int fiveCentCoins = subtract(ref money, 0.05M);
        int twoCentCoins = subtract(ref money, 0.02M);
        int oneCentCoins = subtract(ref money, 0.01M);
        Console.WriteLine("Total amount of change: " + hundredDollarBills + " $100,\n" + fiftyDollarBills + " $50,\n" + twentyDollarBills + " $20,\n" + tenDollarBills + " $10,\n" + singleDillarBills + " $1,\n" + fiftyCentCoins + " $0.50,\n" + twentyCentCoins + " $0.20,\n" + tenCentCoins + " $0.10,\n" + fiveCentCoins + " $0.05,\n" + twoCentCoins + " $0.02,\n" + oneCentCoins + " $0.01");
        Console.WriteLine("Total money left over: " + money);
    }

    private static int subtract(ref decimal money, decimal amount)
    {
        int amtOfChange = (int) Math.Floor(money / amount);
        money %= amount;
        return amtOfChange;
    }
}
对于$255.50的输入,此代码打印以下结果:

变更总额:2$100

1元50分

0$20

0$10

5元1角

1元0.5角

0.20美元

0元0.1角

0$0.05

0$0.02

0元0.01


编辑:显然这段代码可以改进很多,它是如何进行计算的一个例子

假设您有这样一个枚举:

while (sum>=0){
   if(sum <0 R200&& sum % 200 !>0){   //R200 is the amount of 200 rand notes in the register
   sum = sum - 200.00;
   }//endif
}//endwhile
enum CashType { c1, c5, c10, c25, c50, d1, d2, d5, d10, d20, d50, d100 }
var denominationsToCentValue = new Dictionary<CashType, int>()
{
    { CashType.d100, 10000 },
    { CashType.d50, 5000 },
    { CashType.d20, 2000 },
    { CashType.d10, 1000 },
    { CashType.d5, 500 },
    { CashType.d2, 200 },
    { CashType.d1, 100 },
    { CashType.c50, 50 },
    { CashType.c25, 25 },
    { CashType.c10, 10 },
    { CashType.c5, 5 },
    { CashType.c1, 1 }
};


var sortedCash = new Dictionary<CashType, int>();
var cashInRegister = 342.11M;
int cashInRegisterInCents = (int)(100 * cashInRegister);

foreach (var denom in denominationsToCentValue)
{
    int count = cashInRegisterInCents / denom.Value;
    cashInRegisterInCents -= count * denom.Value;

    sortedCash.Add(denom.Key, count);
}
您可以这样做:

while (sum>=0){
   if(sum <0 R200&& sum % 200 !>0){   //R200 is the amount of 200 rand notes in the register
   sum = sum - 200.00;
   }//endif
}//endwhile
enum CashType { c1, c5, c10, c25, c50, d1, d2, d5, d10, d20, d50, d100 }
var denominationsToCentValue = new Dictionary<CashType, int>()
{
    { CashType.d100, 10000 },
    { CashType.d50, 5000 },
    { CashType.d20, 2000 },
    { CashType.d10, 1000 },
    { CashType.d5, 500 },
    { CashType.d2, 200 },
    { CashType.d1, 100 },
    { CashType.c50, 50 },
    { CashType.c25, 25 },
    { CashType.c10, 10 },
    { CashType.c5, 5 },
    { CashType.c1, 1 }
};


var sortedCash = new Dictionary<CashType, int>();
var cashInRegister = 342.11M;
int cashInRegisterInCents = (int)(100 * cashInRegister);

foreach (var denom in denominationsToCentValue)
{
    int count = cashInRegisterInCents / denom.Value;
    cashInRegisterInCents -= count * denom.Value;

    sortedCash.Add(denom.Key, count);
}
var-deminationstocentvalue=newdictionary()
{
{CashType.d100,10000},
{CashType.d505000},
{CashType.d202000},
{CashType.d10,1000},
{CashType.d5500},
{CashType.d2,200},
{CashType.d1,100},
{CashType.c50,50},
{CashType.c25,25},
{CashType.c10,10},
{CashType.c5,5},
{CashType.c1,1}
};
var sortedCash=新字典();
var cashInRegister=3.4211亿;
int cashinregisterments=(int)(100*cashInRegister);
foreach(面额TocentValue中的var denom)
{
int count=现金登记/名称值;
cashinregistrations-=计数*初始值;
sortedCash.Add(名称键、计数);
}

您确定这是正确的吗<代码>看起来不像我识别的代码。我认为您必须提供输入和预期输出的最小示例。现在还不清楚程序必须做什么。第二个答案基本上概括了我想做的事情。您需要了解有关
DRY
。不要重复你自己。将常用步骤放在一个单独的方法中,该方法执行此操作
(int)Math.Floor(money/xxxx)
并将
xxxx
作为参数。我知道
DRY
,只是没有足够的时间在这里应用它。正如我在编辑中所说,这只是一个如何进行OP想要的计算的示例。我将在稍后编辑我的帖子。用更合适的方法更新我的帖子。