Advertisement
jacknpoe

Increase <==> Interest Rate REDUX

Jan 31st, 2016 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.56 KB | None | 0 0
  1. #include <ctgmath>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <locale>
  6.  
  7. namespace jacknpoe {
  8. //--------------------------- Interest class
  9.     class Interest {
  10.     protected:
  11.     protected:
  12.         short Quant;        // number of payments (all payments with 0 will be considered a valid payment in cash)
  13.         bool Compounded;        // compounded (InterestRate², InterestRate³)
  14.         short Period;       // period fot the InterestRate (like 30 for a 30 days interest rate)
  15.         short *Payments;        // payment days in periods like in days { 0, 30, 60, 90}
  16.         long double *Weights;       // payment weights (in any unit, value, apportionment...)
  17.         void init( short quant, bool compounded, short period);
  18.     public:
  19.         Interest( short quant = 0, bool compounded = false, short period = 30);
  20.         Interest( bool compounded, short period = 30);
  21.         ~Interest();
  22.         bool setQuant( short quant);
  23.         short getQuant( void);
  24.         void setCompounded( bool compounded);
  25.         bool getCompounded( void);
  26.         bool setPeriod( short period);
  27.         short getPeriod( void);
  28.         bool setPayment( short index, short value);
  29.         short getPayment( short index);
  30.         bool setWeight( short index, long double value);
  31.         long double getWeight( short index);
  32.         long double getTotalWeight( void);
  33.         long double InterestRateToIncrease( long double interestrate);
  34.         long double IncreaseToInterestRate( long double increase, char precision = 8,
  35.                                             short maxiterations = 100, long double maxinterestrate = 50,
  36.                                             bool increaseasoriginalvalue = false);
  37.     };
  38.  
  39.     void Interest::init( short quant, bool compounded, short period) {
  40.         Payments = NULL;  Weights = NULL;  Quant = 0;
  41.         setQuant( quant);  Compounded = compounded;  Period = period;
  42.     }
  43.  
  44.     Interest::Interest( short quant, bool compounded, short period) { init( quant, compounded, period); }
  45.     Interest::Interest( bool compounded, short period) { init( 0, compounded, period); }
  46.  
  47.     bool Interest::setQuant( short quant) {
  48.         if( quant < 0 ) return false; if( quant == Quant) return true;
  49.         Payments = (short *) std::realloc( Payments, sizeof( short) * quant);
  50.         if( quant !=0 and Payments == NULL) { Quant = 0; return false; }
  51.         Weights = (long double *) std::realloc( Weights, sizeof( long double) * quant);
  52.         if( quant !=0 and Weights == NULL) { std::free( Payments); Payments = NULL; Quant = 0; return false; }
  53.         for( short index = Quant; index < quant; index++) { Payments[ index] = 0; Weights[ index] = 1; }
  54.         Quant = quant; return true;
  55.     }
  56.     short Interest::getQuant( void) { return Quant; }
  57.  
  58.     void Interest::setCompounded( bool compounded) { Compounded = compounded; }
  59.     bool Interest::getCompounded( void) { return Compounded; }
  60.  
  61.     bool Interest::setPeriod( short period) {
  62.         if( period < 1 ) return false;
  63.         Period = period; return true;
  64.     }
  65.     short Interest::getPeriod( void) { return Period; }
  66.  
  67.     bool Interest::setPayment( short index, short value) {
  68.         if( index < 0 or index >= Quant or value < 0) return false;
  69.         Payments[ index] = value; return true;
  70.     }
  71.     short Interest::getPayment( short index) {
  72.         if( index < 0 or index >= Quant) return 0; else return Payments[ index];
  73.     }
  74.  
  75.     bool Interest::setWeight( short index, long double value) {
  76.         if( index < 0 or index >= Quant or value < 0) return false;
  77.         Weights[ index] = value; return true;
  78.     }
  79.     long double Interest::getWeight( short index) {
  80.         if( index < 0 or index >= Quant) return 0; else return Weights[ index];
  81.     }
  82.  
  83.     long double Interest::getTotalWeight( void) {
  84.         long double accumulator = 0;
  85.         for( short index = 0; index < Quant; index++) accumulator += Weights[ index];
  86.         return accumulator;
  87.     }
  88.  
  89.     long double Interest::InterestRateToIncrease( long double interestrate) {
  90.         if( interestrate <= 0 or Quant == 0 ) return 0;   long double total = getTotalWeight();
  91.         if( total == 0) return 0;   if( Period <= 0) return 0;
  92.         long double accumulator = 0;   bool onlyzero = true;
  93.  
  94.         for( short index = 0; index < Quant; index++) {
  95.             if( Payments[ index] > 0 and Weights[ index] > 0) onlyzero = false;
  96.             if( Compounded) accumulator += Weights[ index] / pow( 1 + interestrate / 100, Payments[ index] / Period);  // compounded interest
  97.                 else accumulator += Weights[ index] / (( 1 + interestrate / 100) * Payments[ index] / Period);  // simple interest
  98.         }
  99.         if( onlyzero) return 0;
  100.         return ( total / accumulator - 1 ) * 100;
  101.     }
  102.  
  103.     long double Interest::IncreaseToInterestRate( long double increase, char precision, short maxiterations, long double maxinterestrate, bool increaseasoriginalvalue)
  104.     {      
  105.         long double mininterestrate = 0, medinterestrate, min_diff;
  106.         if( maxiterations < 1 or Quant == 0) return 0;   if( precision < 1) return 0;
  107.         long double total = getTotalWeight();   if( total == 0) return 0;
  108.         if( Period <= 0) return 0;  if( increase <= 0) return 0;
  109.         if( increaseasoriginalvalue) {
  110.             increase = 100 * ( total / increase - 1 );    if( increase <= 0) return 0;
  111.         }
  112.         min_diff = pow( 0.1, precision);
  113.         for( short index = 0; index < maxiterations; index++) {
  114.             medinterestrate = ( mininterestrate + maxinterestrate) / 2;
  115.             if( ( maxinterestrate - mininterestrate ) < min_diff) break;        // the desired precision was reached
  116.             if( InterestRateToIncrease( medinterestrate ) <= increase )
  117.                 mininterestrate = medinterestrate; else maxinterestrate = medinterestrate;
  118.         }
  119.         return medinterestrate;
  120.     }
  121.  
  122.     Interest::~Interest() { std::free( Payments); std::free( Weights); }
  123. }
  124.  
  125. using namespace jacknpoe;   // this code is awkward because, in reality, this file is a concatenation of a .h and a .cpp (from a library) and the main
  126. int main() {
  127.     Interest interest;
  128.  
  129.     setlocale( LC_ALL, "");     // equal caracters in prompt
  130.     std::cout.precision(9);
  131.  
  132.     short payments, temp, period, option;
  133.     long double increase, interestrate, total, temp_ld;
  134.  
  135.    
  136.     while( true) {
  137.         std::cout << "——————————————————————————————————\n";
  138.         std::cout << "| Choose an option               |\n";
  139.         std::cout << "——————————————————————————————————\n\n";
  140.         std::cout << "     1. Interest Rate to Increase.\n\n";
  141.         std::cout << "     2. Increase to Interest Rate.\n\n";
  142.         std::cout << "Others. Exit.\n\n\n";
  143.  
  144.         std::cin >> option;
  145.         std::cout << "\n";
  146.         if( option != 1 and option != 2) {
  147.             break;
  148.         }
  149.  
  150.         std::cout << "\n";
  151.        
  152.         std::cout << "Number of payments: ";
  153.         std::cin >> payments;
  154.    
  155.         if( payments < 1) {
  156.             std::cout << "You must provide at least one payment.\n\n";
  157.             system( "PAUSE");
  158.             std::cout << "\n\n";
  159.             continue;
  160.         }
  161.         interest.setQuant( payments);
  162.    
  163.         std::cout << "Interest rate relating to (example: 30 days, 1 month): ";
  164.         std::cin >> period;
  165.    
  166.         if( period < 1) {
  167.             std::cout << "Interest rate must relating to at least one period.\n\n";
  168.             system( "PAUSE");
  169.             std::cout << "\n\n";
  170.             continue;
  171.         }
  172.         interest.setPeriod( period);
  173.    
  174.         for( short index = 0; index < payments; index++) {
  175.             std::cout << "Payment " << index + 1 << ": ";
  176.             std::cin >> temp;
  177.             interest.setPayment( index, temp);
  178.  
  179.             std::cout << "Weight " << index + 1 << ": ";
  180.             std::cin >> temp_ld;
  181.             interest.setWeight( index, temp_ld);
  182.         }
  183.    
  184.         if( option == 1)
  185.         {
  186.             std::cout << "\nFull cash value: ";
  187.             std::cin >> total;
  188.  
  189.             std::cout << "Interest rate: ";
  190.             std::cin >> interestrate;
  191.        
  192.             interest.setCompounded( false); // simple interest
  193.             increase = interest.InterestRateToIncrease( interestrate);
  194.             std::cout << "\n\nSIMPLE INTEREST";
  195.             std::cout << "\nIncrease: " << increase << "%, payment value: " << total * ( 1 + increase / 100) / payments;
  196.             std::cout << ", total paid: " << total * ( 1 + increase / 100);
  197.        
  198.             interest.setCompounded( true);  // compound interest
  199.             increase = interest.InterestRateToIncrease( interestrate);
  200.             std::cout << "\n\nCOMPOUND INTEREST";
  201.             std::cout << "\nIncrease: " << increase << "%, payment value: " << total * ( 1 + increase / 100) / payments;
  202.             std::cout << ", total paid: " << total * ( 1 + increase / 100);
  203.         }
  204.             else
  205.         {
  206.             std::cout << "\nIncrease: ";
  207.             std::cin >> increase;
  208.  
  209.             interest.setCompounded( false);
  210.             interestrate = interest.IncreaseToInterestRate( increase);
  211.             std::cout << "\n\nSIMPLE INTEREST: " << interestrate;
  212.        
  213.             interest.setCompounded( true);
  214.             interestrate = interest.IncreaseToInterestRate( increase);
  215.             std::cout << "\n\nCOMPOUND INTEREST: " << interestrate;
  216.         }
  217.  
  218.         std::cout << "\n\n";
  219.         system( "PAUSE");
  220.         std::cout << "\n\n";
  221.     }
  222.     std::cout << "\n\n"; exit( 0);
  223. }
  224.  
  225. //
  226. //----------------------------------
  227. //| Choose an option               |
  228. //----------------------------------
  229. //     1. Interest Rate to Increase.
  230. //     2. Increase to Interest Rate.
  231. //Others. Exit.
  232.  
  233. //2
  234.  
  235. //Number of payments: 3
  236. //Interest rate relating to (example: 30 days, 1 month): 30
  237. //Payment 1: 30
  238. //Weight 1: 1
  239. //Payment 2: 60
  240. //Weight 2: 1
  241. //Payment 3: 90
  242. //Weight 3: 1
  243.  
  244. //Increase: 20
  245.  
  246. //SIMPLE INTEREST: 10.2936087
  247. //COMPOUND INTEREST: 9.70102574
  248.  
  249. //----------------------------------
  250. //| Choose an option               |
  251. //----------------------------------
  252. //     1. Interest Rate to Increase.
  253. //     2. Increase to Interest Rate.
  254. //Others. Exit.
  255.  
  256. //1
  257.  
  258. //Number of payments: 2
  259. //Interest rate relating to (example: 30 days, 1 month): 1
  260. //Payment 1: 1
  261. //Weight 1: 1
  262. //Payment 2: 2
  263. //Weight 2: 1
  264.  
  265. //Full cash value: 1000
  266. //Interest rate: 5
  267.  
  268. //SIMPLE INTEREST
  269. //Increase: 7.44186047%, payment value: 537.209302, total paid: 1074.4186
  270.  
  271. //COMPOUND INTEREST
  272. //Increase: 7.56097561%, payment value: 537.804878, total paid: 1075.60976
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement