3 驿站币
//这是我写的
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(void)
{
double money=24.0; //本金
int year=379; //年限
double total=0;
cout<<fixed<<setprecision(2);
for(int i=5;i<=10;i++) // i 为利率
{
cout<<setw(10)<<"本金"<<setw(20)<<"利率"<<setw(20)<<"年限"<<setw(20)<<"收益"<<endl;
total=money*pow(1.0+i*0.01,year);
cout<<setw(10)<<24<<setw(20)<<0.01*i<<setw(20)<<year<<setw(20)<<total<<endl;
}
return 0;
}
//------------------对比------------------------------------------------------------------------------------------
//课后答案
#include <iostream>
#include <iomanip>
#include <cmath> // standard C++ math library
using namespace std;
int main()
{
double amount; // amount on deposit at end of each year
double principal = 24.0; // initial amount before interest
double rate; // interest rate
// set floating-point number format
cout << fixed << setprecision( 2 );
// loop through interest rates 5% to 10%
for ( rate = 5; rate <= 10; rate++ )
{
// display headers
cout << "\nInterest rate: " << rate << "%\n"
<< "Year" << setw( 30 ) << "Amount on deposit" << endl;
// calculate amount on deposit for each of 384 years
// calculate new amount for specified year
amount = principal * pow( 1.0 + rate / 100.0,379);
// display the year and the amount
cout << setw( 4 ) << 379 << setw( 30 ) << amount << endl;
} // end outer for
} // end main
我来回答
上一篇:
[Win32编程] 求推荐书籍 下一篇:
在MFC添加了新类却出现了问题