存款利息算法

维c银翘片 2009-10-20 07:48:27
银行存款利息分为三种情况:1.利息按年计算。例如第一年存款100元,年利率2%,则第一年的利息为:x1:100*2%,第一年的结算余额y1:100+x1;
第二年的利息X2:y1*2%,第二年的结算余额为y2:y1+x2;
2.利息按月计算是每月计算一次应得的利息,并将应得利息作为新存款添加到用户存款金额中。例如,第一次存入1000元 年利率为2%,则第一个月的利息为:1000*(0.02/12)元。第二个月的利息为(1000+第一个月的利息)*(0.02/12)元,依次类推。
3.利息按季度计算。例如首次存款1000元,年利率为2% ,则第一个季度的利息为;1000*(0.02/4)。。。。。依次类推
怎样用编程的思想实现?
...全文
3834 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
VistaKobe 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 changyuming 的回复:]
按照策略模式的定义,我写了下面的一些代码
首先定义了一个接口
using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
    public interface RateInterface
    {
        float GetValue(float baseValue,int year,float rate);
    }
}

然后分别定义3个类实现这个接口,分别是按年计算、按月计算和按季度计算
using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
    class YearRate : RateInterface
    {
        #region RateInterface 成员

        /// <summary>
        /// 按年计算本息
        /// </summary>
        /// <param name="baseValue">存款基值 </param>
        /// <param name="year">年数 </param>
        /// <param name="rate">利率 </param>
        /// <returns> </returns>
        public float GetValue(float baseValue, int year, float rate)
        {
            float result = baseValue;

            for (int i = 0; i < year; i++)
            {
                result += result * rate;
            }
            return result;
        }

        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
    class MonthRate:RateInterface
    {
        #region RateInterface 成员

        public float GetValue(float baseValue, int year, float rate)
        {
            float result = baseValue;
            rate = rate / 12;

            for (int i = 0; i < year; i++)
            {
                result += result * rate;
            }
            return result;
        }

        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
    class SeasonRate:RateInterface
    {
        #region RateInterface 成员

        public float GetValue(float baseValue, int year, float rate)
        {
            float result = baseValue;
            rate = rate / 4;

            for (int i = 0; i < year; i++)
            {
                result += result * rate;
            }
            return result;
        }

        #endregion
    }
}
在使用的时候
首先定义接口的变量
RateInterface rateTool;
rateTool=new YearRate();
rateTool.GetValue(1000,5,0.02);
rateTool = new MonthRate();
rateTool.GetValue(1000,5,0.02);
rateTool = new SeasonRate();
rateTool.GetValue(1000,5,0.02);


[/Quote]


顶!
changyuming 2009-10-20
  • 打赏
  • 举报
回复
按照策略模式的定义,我写了下面的一些代码
首先定义了一个接口
using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
public interface RateInterface
{
float GetValue(float baseValue,int year,float rate);
}
}

然后分别定义3个类实现这个接口,分别是按年计算、按月计算和按季度计算
using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
class YearRate : RateInterface
{
#region RateInterface 成员

/// <summary>
/// 按年计算本息
/// </summary>
/// <param name="baseValue">存款基值</param>
/// <param name="year">年数</param>
/// <param name="rate">利率</param>
/// <returns></returns>
public float GetValue(float baseValue, int year, float rate)
{
float result = baseValue;

for (int i = 0; i < year; i++)
{
result += result * rate;
}
return result;
}

#endregion
}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
class MonthRate:RateInterface
{
#region RateInterface 成员

public float GetValue(float baseValue, int year, float rate)
{
float result = baseValue;
rate = rate / 12;

for (int i = 0; i < year; i++)
{
result += result * rate;
}
return result;
}

#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace testapp
{
class SeasonRate:RateInterface
{
#region RateInterface 成员

public float GetValue(float baseValue, int year, float rate)
{
float result = baseValue;
rate = rate / 4;

for (int i = 0; i < year; i++)
{
result += result * rate;
}
return result;
}

#endregion
}
}
在使用的时候
首先定义接口的变量
RateInterface rateTool;
rateTool=new YearRate();
rateTool.GetValue(1000,5,0.02);
rateTool = new MonthRate();
rateTool.GetValue(1000,5,0.02);
rateTool = new SeasonRate();
rateTool.GetValue(1000,5,0.02);

zhongjiekangping 2009-10-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gisyellow 的回复:]
这个使用策略模式太合适了。。
[/Quote]

思考
fengling2001 2009-10-20
  • 打赏
  • 举报
回复
很好搞吧
首先三个case
然后分别计算就可以(递归就可以)
case 1:
BankYearInterest();
break;
case 2:
BankMouthInterest();
break;
case 3:
BankQuarterInterest();
break;

private double BankYearInterest(double deposit,float rate,int years)
{
debug.assert(years>=0);
if(years == 0)
return deposit;
else if(years == 1)
return deposit*(1 + rate);
else
return BankYearInterest(deposit,rate, years - 1)*(1 + rate);
}
其他两个自己看这写
changyuming 2009-10-20
  • 打赏
  • 举报
回复
int32 GetYearValue(int baseValue,int year,float rate)
{
int32 result= baseValue;

for(int i=0;i<year;i++)
{
result+= result*rate;
}
return result;
}
这是按年计算的函数,以此类推吧,应该很简单。当然我这里只是随便写的,没有经过验算和仔细的思考编程技巧
gisyellow 2009-10-20
  • 打赏
  • 举报
回复
这个使用策略模式太合适了。。
jxwangjm 2009-10-20
  • 打赏
  • 举报
回复
y=最终金额
x=本金
m=时间期段,第一年末为1
n=利息
y=x(1+n)^m

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧