application2-3.cpp

来自「对于特定长度的高速公路」· C++ 代码 · 共 47 行

CPP
47
字号
#include <iostream> 
#include <cmath> 
#include <iomanip> 
#include <locale> 
 
using namespace std; 
   
// Compute the initial investment necessary for  
// a specified future value. 
double initval(double targetValue, double rateOfRet, 
               int numYears, int compPerYear) {  
  double b, e;  
 
  rateOfRet /= 100.0; // convert percentage to fraction  
 
  b = (1 + rateOfRet/compPerYear);  
  e = compPerYear * numYears;  
  
  return targetValue / pow(b, e);    
}     
 
int main() { 
  double p, r; 
  int y, cpy; 
 
  // Set locale to English.  Adjust as necessary 
  // for your language and/or egion. 
  cout.imbue(locale("english")); 
 
  cout << "Enter desired future value: "; 
  cin >> p; 
 
  cout << "Enter rate of return: "; 
  cin >> r; 
 
  cout << "Enter number years: "; 
  cin >> y; 
 
  cout << "Enter number of compoundings per year: "; 
  cin >> cpy; 
 
  cout << "\nInitial investment required: "  
       << fixed << setprecision(2) 
       << initval(p, r, y, cpy) << endl; 
 
  return 0; 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?