finance.cpp

来自「C/C++程序设计导论(第二版)》程序源文件」· C++ 代码 · 共 39 行

CPP
39
字号
// FILE:  finance.cpp

#include <math.h>
// RateExpr()  Return an interest sub-expression
// IN:		rate is the periodic interest rate
//		k is a payment number

float RateExpr (float rate, float k)
{	float temp;
	temp = 1.0 + rate;
	return (pow (temp, -k));
}
// Payment()  Return the payment on a loan given the prin-
//  ciple, rate, and number of payments
// IN:  		rate is the periodic interest rate
//		principal is the original loan value
//		num_payments is the number of equally-spaced payments

float Payment (float principal, float rate, float num_pay)
{	float paymnt;
	paymnt = principal * (rate / (1.0 - RateExpr (rate, num_pay)));
	return (paymnt);
}
// Amortize()  Update the accumulated interest and balance
//  of a loan given principal, rate, payment, and payment number
// IN:		rate is the periodic interest rate
//		prin is the original loan value
//		num is the current payment number
// OUT:  bal is the current loan balance after the current payment
// 		accum is the accumulated interest after payment

void Amortize (float& accum, float& bal, float rate, float pmnt, 
					float prin, float num)
{	float temp;
	temp = RateExpr (rate, num);
	bal = (1.0 / temp) * ((pmnt * (temp-1.0) / rate) + prin);
	accum = bal + num * pmnt - prin;
}

⌨️ 快捷键说明

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