无法通过以下方法弄清楚如何计算非复合税和混合税?

| 下面的类只有一个方法可返回某个百分比的除数,因此,如果我将其传递给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 我不确定如何实现以上内容。     
已邀请:
尝试这个:
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);
}
    
如果我正确理解了您的要求,我认为您在迭代调整项时只需要保留复合部分和非复合部分,最后将它们组合即可。
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);
}
或者您可以将其设置为一个属性,例如
public decimal AmountFraction
{
    get
    {
        return (Amount / 100.0M);
    }
}
然后将其读为“ 8”,即作为属性而不是函数调用。 编辑:修改了组合线以按照注释删除非复合部分。     

要回复问题请先登录注册