2

下面的类只有一个方法,它返回某个百分比的除数,所以如果我传递它 5,它将返回 0.05。

public class Adjustment
{
    public decimal Amount {get;set;}
    public bool IsCompounded {get;set;}
    public bool Add{get;set;}

    public decimal Calculate(decimal amount)
    {
        return (amount / 100.0M);
    }
 }

我的主要方法定义如下:

void Main()
{

    Adjustment a1 = new Adjustment {Amount = 10.0M, IsCompounded = false, Add = true};
    Adjustment a2 = new Adjustment {Amount = 7.0M, IsCompounded = false, Add = true};


    List<Adjustment> adjustments = new List<Adjustment>();
    adjustments.Add(a1);
    adjustments.Add(a2);

    decimal value = 0.0M;
    decimal total = 100.0M;

    foreach(Adjustment a in adjustments)
    {
        if(a.IsCompounded)
        {
            value = (1 + a.Calculate(a.Amount));

            if(a.Add)
                 total *= value;
            else
                total /= value;
        }
        else if(!a.IsCompounded)
        {
             value += a.Calculate(a.Amount);

             if(a.Add)
                 total *= value;
         else
                 total /= value;
        }
    }

    Console.WriteLine(total);
}

在上面的主要方法中,我从 100 开始,如果所有的税都是复合的,它工作正常,我得到 117.7,如果我拿 117.7 并去掉税,我回到 100。对于非复合,我只需要在最后加1,然后除以总数,但我不知道该怎么做。目前,当我循环时,我只是添加除数,例如 0.1 + 0.07 = 0.17。在循环之外,我需要将 1 加到 1.17,然后乘以或除以分别添加或删除税款。然后是复合和非复合调整的问题,这变得更加复杂。在这种情况下,我需要执行以下操作:

例如,假设我有 3 个税,10、7 和 3。10 和 7 是复合的,3 是非复合的,所以公式是:

100 * (1+((1+0.10) * (1+0.07)−1)+((1+0.03)−1)) 简化为 100 * ((1+0.10) * ( (1+0.07)+ 0.03) = 120.70

我不确定如何实现上述内容。

4

2 回答 2

0

如果我正确理解您的要求,我认为您只需要在迭代调整时保留单独的复合分数和非复合分数,并在最后将它们组合起来。

decimal total = 100.0M;

decimal compounded = 1.0M;
decimal nonCompounded = 0.0M;
foreach(Adjustment a in adjustments)
{
    if(a.IsCompounded)
    {
        decimal value = (1.0m + a.Calculate(a.Amount));

        if (a.Add)
            compounded *= value;
        else
            compounded /= value;
    }
    else
    {
        decimal value = a.Calculate(a.Amount);

        if (a.Add)
            nonCompounded += value;
        else
            nonCompounded -= value;
    }
}

if (nonCompounded >= 0)
{
  total *= (compounded + nonCompounded);
}
else
{
  total *= (compounded / (1.0m - nonCompounded));
}

你只会a.Amount进入a.Calculate吗?然后您可以从对象中读取金额,例如

public decimal Calculate()
{
    return (Amount / 100.0M);
}

或者您可以将其设为 get 属性,例如

public decimal AmountFraction
{
    get
    {
        return (Amount / 100.0M);
    }
}

然后您将其decimal value = a.AmountFraction;视为属性而不是函数调用。

编辑:修改组合行以根据评论删除非复合分数。

于 2011-04-14T16:59:28.857 回答
0

尝试这个:

public class Adjustment
{
    public decimal Amount {get;set;}
    public bool IsCompounded {get;set;}
    public bool Add{get;set;}

    public decimal Calculate()
    {
        if(IsCompounded)
            return 1 + Amount / 100.0M;
        else
            return Amount / 100.0M;
    }
 }

void Main()
{
    List<Adjustment> adjustments = new List<Adjustment>();
    adjustments.Add(new Adjustment() {Amount = 10.0M, IsCompounded = false, Add = true});
    adjustments.Add(new Adjustment() {Amount = 7.0M, IsCompounded = false, Add = true};);

    decimal value = 0.0M;
    decimal total = 100.0M;

    foreach(Adjustment a in adjustments)
    {
        value = a.Calculate();

        if(a.IsCompound)
        {
            if(a.Add)
                 total *= value;
             else
                 total /= value;
        }
        else
        {
            if(a.Add)
                 total += value; //should be a sum for nom-compounded
             else
                 total -= value;
        }
    }

    Console.WriteLine(total);
}
于 2011-04-14T17:02:01.267 回答