Advertisement
jacknpoe

Increase to Interest Rate and Interest Rate to Increase

Nov 7th, 2013 (edited)
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.68 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;
  91.         long double total = getTotalWeight();
  92.         if( total == 0) return 0;
  93.         if( Period <= 0) return 0;
  94.         long double accumulator = 0; bool onlyzero = true;
  95.  
  96.         for( short index = 0; index < Quant; index++) {
  97.             if( Payments[ index] > 0 and Weights[ index] > 0) onlyzero = false;
  98.             if( Compounded)     // compounded interest
  99.                 accumulator += Weights[ index] / pow( 1 + interestrate / 100, Payments[ index] / Period);
  100.             else        // simple interest
  101.                 accumulator += Weights[ index] / (( 1 + interestrate / 100) * Payments[ index] / Period);
  102.         }
  103.         if( onlyzero) return 0;
  104.         return ( total / accumulator - 1 ) * 100;
  105.     }
  106.  
  107.     long double Interest::IncreaseToInterestRate( long double increase, char precision,
  108.                                                   short maxiterations, long double maxinterestrate,
  109.                                                   bool increaseasoriginalvalue)
  110.     {      
  111.         long double mininterestrate = 0, medinterestrate, min_diff;
  112.         if( maxiterations < 1 or Quant == 0) return 0;
  113.         if( precision < 1) return 0;
  114.         long double total = getTotalWeight();
  115.         if( total == 0) return 0;
  116.         if( Period <= 0) return 0;
  117.         if( increase <= 0) return 0;
  118.         if( increaseasoriginalvalue) {
  119.             increase = 100 * ( total / increase - 1 );
  120.             if( increase <= 0) return 0;
  121.         }
  122.        
  123.         min_diff = pow( 0.1, precision);
  124.         for( short index = 0; index < maxiterations; index++) {
  125.             medinterestrate = ( mininterestrate + maxinterestrate) / 2;
  126.             if( ( maxinterestrate - mininterestrate ) < min_diff) break;        // the desired precision was reached
  127.             if( InterestRateToIncrease( medinterestrate ) <= increase )
  128.                 mininterestrate = medinterestrate;
  129.             else
  130.                 maxinterestrate = medinterestrate;
  131.         }
  132.         return medinterestrate;
  133.     }
  134.  
  135.     Interest::~Interest() { std::free( Payments); std::free( Weights); }
  136. }
  137.  
  138. 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
  139. int main() {
  140.     Interest interest;
  141.  
  142.     setlocale( LC_ALL, "");     // equal caracters in prompt
  143.     std::cout.precision(9);
  144.  
  145.     short payments, temp, period, option;
  146.     long double increase, interestrate, total, temp_ld;
  147.  
  148.    
  149.     while( true) {
  150.         std::cout << "——————————————————————————————————\n";
  151.         std::cout << "| Choose an option               |\n";
  152.         std::cout << "——————————————————————————————————\n\n";
  153.         std::cout << "     1. Interest Rate to Increase.\n\n";
  154.         std::cout << "     2. Increase to Interest Rate.\n\n";
  155.         std::cout << "Others. Exit.\n\n\n";
  156.  
  157.         std::cin >> option;
  158.         std::cout << "\n";
  159.         if( option != 1 and option != 2) {
  160.             break;
  161.         }
  162.  
  163.         std::cout << "\n";
  164.        
  165.         std::cout << "Number of payments: ";
  166.         std::cin >> payments;
  167.    
  168.         if( payments < 1) {
  169.             std::cout << "You must provide at least one payment.\n\n";
  170.             system( "PAUSE");
  171.             std::cout << "\n\n";
  172.             continue;
  173.         }
  174.         interest.setQuant( payments);
  175.    
  176.         std::cout << "Interest rate relating to (example: 30 days, 1 month): ";
  177.         std::cin >> period;
  178.    
  179.         if( period < 1) {
  180.             std::cout << "Interest rate must relating to at least one period.\n\n";
  181.             system( "PAUSE");
  182.             std::cout << "\n\n";
  183.             continue;
  184.         }
  185.         interest.setPeriod( period);
  186.    
  187.         for( short index = 0; index < payments; index++) {
  188.             std::cout << "Payment " << index + 1 << ": ";
  189.             std::cin >> temp;
  190.             interest.setPayment( index, temp);
  191.  
  192.             std::cout << "Weight " << index + 1 << ": ";
  193.             std::cin >> temp_ld;
  194.             interest.setWeight( index, temp_ld);
  195.         }
  196.    
  197.         if( option == 1)
  198.         {
  199.             std::cout << "\nFull cash value: ";
  200.             std::cin >> total;
  201.  
  202.             std::cout << "Interest rate: ";
  203.             std::cin >> interestrate;
  204.        
  205.             interest.setCompounded( false); // simple interest
  206.             increase = interest.InterestRateToIncrease( interestrate);
  207.             std::cout << "\n\nSIMPLE INTEREST";
  208.             std::cout << "\nIncrease: " << increase << "%, payment value: " << total * ( 1 + increase / 100) / payments;
  209.             std::cout << ", total paid: " << total * ( 1 + increase / 100);
  210.        
  211.             interest.setCompounded( true);  // compound interest
  212.             increase = interest.InterestRateToIncrease( interestrate);
  213.             std::cout << "\n\nCOMPOUND INTEREST";
  214.             std::cout << "\nIncrease: " << increase << "%, payment value: " << total * ( 1 + increase / 100) / payments;
  215.             std::cout << ", total paid: " << total * ( 1 + increase / 100);
  216.         }
  217.             else
  218.         {
  219.             std::cout << "\nIncrease: ";
  220.             std::cin >> increase;
  221.  
  222.             interest.setCompounded( false);
  223.             interestrate = interest.IncreaseToInterestRate( increase);
  224.             std::cout << "\n\nSIMPLE INTEREST: " << interestrate;
  225.        
  226.             interest.setCompounded( true);
  227.             interestrate = interest.IncreaseToInterestRate( increase);
  228.             std::cout << "\n\nCOMPOUND INTEREST: " << interestrate;
  229.         }
  230.  
  231.         std::cout << "\n\n";
  232.         system( "PAUSE");
  233.         std::cout << "\n\n";
  234.     }
  235.     std::cout << "\n\n"; exit( 0);
  236. }
  237.  
  238. //
  239. //----------------------------------
  240. //| Choose an option               |
  241. //----------------------------------
  242. //     1. Interest Rate to Increase.
  243. //     2. Increase to Interest Rate.
  244. //Others. Exit.
  245.  
  246. //2
  247.  
  248. //Number of payments: 3
  249. //Interest rate relating to (example: 30 days, 1 month): 30
  250. //Payment 1: 30
  251. //Weight 1: 1
  252. //Payment 2: 60
  253. //Weight 2: 1
  254. //Payment 3: 90
  255. //Weight 3: 1
  256.  
  257. //Increase: 20
  258.  
  259. //SIMPLE INTEREST: 10.2936087
  260. //COMPOUND INTEREST: 9.70102574
  261.  
  262. //----------------------------------
  263. //| Choose an option               |
  264. //----------------------------------
  265. //     1. Interest Rate to Increase.
  266. //     2. Increase to Interest Rate.
  267. //Others. Exit.
  268.  
  269. //1
  270.  
  271. //Number of payments: 2
  272. //Interest rate relating to (example: 30 days, 1 month): 1
  273. //Payment 1: 1
  274. //Weight 1: 1
  275. //Payment 2: 2
  276. //Weight 2: 1
  277.  
  278. //Full cash value: 1000
  279. //Interest rate: 5
  280.  
  281. //SIMPLE INTEREST
  282. //Increase: 7.44186047%, payment value: 537.209302, total paid: 1074.4186
  283.  
  284. //COMPOUND INTEREST
  285. //Increase: 7.56097561%, payment value: 537.804878, total paid: 1075.60976
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement