c++计算单利和复利存款金额

//计算单利和复利存款金额
#include
using namespace std;
double simpleinterest(double principal,double interestrate,int year);
double compoundinterest(double principal,double interestrate,int year);
int main()
{
//A的本金
const double daphneprincipal = 100;
//B的本金
double Cleoprincipal = 100;
//A的利率
const double daphneinterestrate = 0.1;
//B的利率
const double Cleointerestrate = 0.05;
int year = 1;
bool flag = true;
while(flag)
{
double daphnemoney = simpleinterest(daphneprincipal,daphneinterestrate,year);
double cleomoney = compoundinterest(Cleoprincipal,Cleointerestrate, year);
if(daphnemoney >= cleomoney)
{
year++;
}
else
{
cout<<"year:"<
cout<<"money:"<
flag = false;
}

}
}
//计算单利模式时总金额
//principal 本金  interestrate 利率  year 年
double simpleinterest(double principal,double interestrate,int year)
{
return principal * (1 + interestrate*year);
}
//计算复利模式时总金额
//principal 本金  interestrate 利率  year 年
//复利利息计算公式(1 + n )^y  n是利率 y是时间(年)
double compoundinterest(double principal,double interestrate,int year)
{
double mutisum = 1;
for(int i = 0; i < year ; i++)
{
mutisum = mutisum*(1 + interestrate);
}
return principal*mutisum;
}

你可能感兴趣的:(C++,单利,复利,c++)