[ create a new paste ] login | about

Link: http://codepad.org/raD6B47i    [ raw code | fork ]

C++, pasted on Jul 30:
// this programm is meant for basic analysis of investment.
// it takes cash flows as input and outputs such parameters as:
// NPV - total present value of the project.
// i_profit - index of profit of the project
// p_profit = period before project starts to give profit
// ea - equivalent annuitet
// irr = inner return rate. Estimation of such percent rate at which project generate enough money to payback itself, but doesn't give any profit.
// irr is the percent rate at which npv equals zero
// mirr = modified inner return rate. modification of irr, due to irr being sometimes too optimistic.
// TODO:0.	make the programm accept some cmdline variables.
//		1.	clean the code.
// 		2.	move the length and percent rate to a class too.
// 		3.	write module for comparing 2 (for beginning) or unlimited number of projects.
// 		4.	make the programm accept files with data, not only data from standart input.
// 		5.	it should be able to create datafiles too, not only read them (probably design an xml data "standart" for such thing).
//		6.	come up with a nice name for this project
//		7.	write a GUI for it using Qt (and probably kdelibs)

#include <iostream>
#include <vector>
#include <cmath>
#include "basic_invest.h"
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;

/*-----------------------------------------------------*/

// This function is used for data input, also it initializes the global vectors. Arguments are self descriptive.
void data_input ( int &length, long double &discont )
{
  // Read length of project.
  bool error = false; // variable for checking input
  do
  {
	error = false;
	cout << "Input the length of project" << endl;
	cin >> length;

	// Length can't be less then or equal zero
	if ( length <= 0 ) {
	  cerr << "Wrong length of project" << endl;
	  cerr << "The length of project must be greater then 0" << endl;
	  error = true;
	}
  }
  while (error == true);

  // Read the discount rate of the project.
  do
  {
	cout << "Input the percent rate of discont" << endl;
	cin >> discont;
	error = false;

	// Now convert from percents to 0.x
	discont /= 100;

	// The discont rate can't be less then zero, and doubtfull will ever be more then 1.
	if ( discont <= 0 || discont >= 1 ) {
	  cerr << "Wrong rate of discont" << endl;
	  cerr << "Rate of discont must be between 0 and 100 percents" << endl;
	  error = true;
	}
  }
  while (error == true);
}

/*-----------------------------------------------------*/
int main ()
{
  char again = 'N';
  bool error = false;
  do
  {
	long double percent = 0;
	int period = 0;


	data_input(period, percent);// reading the length of project and percent rate
	koefficients k_disc(period); k_disc.discont(percent);// creating koefficients of discount
	koefficients k_infl(period); k_infl.inflate(percent);// creating koefficients of inflation
	cout << "Input the investment cash flow per year" << endl
	<< "Remember, that investment on the 0th age can't be zero, as it doesn't make sense." << endl;
	cash_flow invest(period); // creating investment cash flow
	cout << "Input the revenue cash flow per year" << endl;
	cash_flow revenue(period);// creating revenue cash flow
	invest.d_cash_flow(k_disc); revenue.d_cash_flow(k_disc); // discounting cash flows
	cf_calc other_flows; other_flows.calc(period, invest, revenue); // creating and calculating other flows.
	results calculations; // initialazing the results.

	// The actuall calculations and output.
	cout << "NPV of this project = " << calculations.npv (period, other_flows) << endl
	<< "Index of profit of this project = " << calculations.profit_index (period, invest, revenue) << endl
	<< "Period for getting profit = " << calculations.profit_period (period, other_flows) << endl
	<< "Equivalent annuitet = " << calculations.ea_calc (period, percent, other_flows, k_disc) << endl
	<< "MIRR of this project = " << calculations.mirr_calc (period, invest, revenue, k_disc, k_infl) << endl
	<< "IRR of this project = "<< calculations.irr_calc (period, percent, invest, revenue) << endl;

	// We probably need to analyze another project. Let's ask user about it.
	do
	{
	  cout << "Do you want to analyse another project? [Y/N]" << endl;
	  cin >> again;
	  switch (again) {
		case 'Y' :
		  error = false;
		  break;
		case 'N' :
		  error = false;
		  break;
		default :
		  error = true;
		  cout << "Sorry, I didn't understand the answer, please answer Y or N" << endl;
		  break;
	  }
	}
	while ( error == true );
  }
  while ( again == 'Y' );
  return 0;

}
/*-----------------------------------------------------*/


Create a new paste based on this one


Comments: