home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / advfun8.cpp < prev    next >
C/C++ Source or Header  |  1993-05-17  |  3KB  |  130 lines

  1. /*
  2.    C++ program that uses pointers to functions to implement a
  3.    a program that calculates the depreciation of an asset
  4. */
  5.  
  6. #include <iostream.h>
  7. #include <math.h>
  8.  
  9. enum DeprMethod { StraightLineMethod, DeclineBalanceMethod, 
  10.                   SumOfDigitsMethod };
  11.  
  12. double StraightLine(double origVal, double scrapVal, 
  13.                     unsigned serviceLife, unsigned year)
  14. {
  15.   double deprRate = (origVal - scrapVal) / serviceLife;
  16.   
  17.   return origVal - year * deprRate;
  18. }
  19.  
  20. double DeclineBalance(double origVal, double scrapVal, 
  21.                       unsigned serviceLife, unsigned year)
  22. {
  23.   double deprCoef = 1 - pow(scrapVal / origVal, 
  24.                             1.0/serviceLife);
  25.   
  26.   return origVal * pow((1 - deprCoef), year);
  27. }
  28.  
  29.  
  30. double SumOfDigits(double origVal, double scrapVal, 
  31.                    unsigned serviceLife, unsigned year)
  32. {
  33.   long sumYears = serviceLife * (serviceLife + 1) / 2;
  34.   double totalDeprVal = origVal - scrapVal;
  35.   double sumDepr = 0;
  36.   
  37.   for (unsigned i = 1; i <= year; i++)
  38.     sumDepr += totalDeprVal * (serviceLife + 1 - i) / sumYears;
  39.   return origVal - sumDepr;
  40. }
  41.                    
  42.  
  43. void CalcDeprTable(double origVal, double scrapVal, 
  44.                    unsigned serviceLife,
  45.                    double (*f)(double, double, unsigned, unsigned))
  46. {
  47.   for (unsigned year = 1; year <= serviceLife; year++) 
  48.     cout << "At year " << year << " value is $"
  49.          << (*f)(origVal, scrapVal, serviceLife, year)
  50.          << "\n";
  51.   cout << "\n\n\n";
  52. }               
  53.  
  54. void GetDouble(const char* prompt, double& x)
  55. {
  56.   do {
  57.     cout << prompt;
  58.     cin >> x;
  59.     cout << "\n";
  60.   } while (x <= 0);
  61. }
  62.  
  63. void GetUnsigned(const char* prompt, unsigned& n)
  64. {
  65.   do {
  66.     cout << prompt;
  67.     cin >> n;
  68.     cout << "\n";
  69.   } while (n < 1);
  70.  
  71. DeprMethod SelectDeprFunction()
  72. {
  73.   int n;
  74.   
  75.   cout << "Select a depreciation method:\n"
  76.        << "1) Straight line\n"
  77.        << "2) Declining balance\n"
  78.        << "3) Sum of digits\n\n";
  79.   cout << "Enter Choice by number: ";
  80.   cin >> n;
  81.   cout << "\n\n";
  82.   switch (n) {
  83.     case 1:
  84.       return StraightLineMethod;
  85.       
  86.     case 2:
  87.       return DeclineBalanceMethod;
  88.       
  89.     case 3:
  90.       return SumOfDigitsMethod;
  91.       
  92.     default:
  93.       return StraightLineMethod;
  94.   }
  95. }
  96.                    
  97. main()
  98. {                  
  99.   double OrigVal;
  100.   double ScrapVal;
  101.   unsigned ServiceLife;
  102.   double (*DeprFun)(double, double, unsigned, unsigned);
  103.   DeprMethod Choice;
  104.   char Answer;
  105.  
  106.   do {                                    
  107.     GetDouble("Enter original value: ", OrigVal);
  108.     GetDouble("Enter scrap value: ", ScrapVal);
  109.     GetUnsigned("Enter service life: ", ServiceLife);
  110.     Choice = SelectDeprFunction();
  111.     switch (Choice) {
  112.       case StraightLineMethod:
  113.         DeprFun = StraightLine;
  114.         break;
  115.       case DeclineBalanceMethod:
  116.         DeprFun = DeclineBalance;
  117.         break;
  118.       case SumOfDigitsMethod:
  119.         DeprFun = SumOfDigits;
  120.         break;
  121.     }
  122.     CalcDeprTable(OrigVal, ScrapVal, ServiceLife, DeprFun);
  123.     cout << "More calculations ? (Y/N) ";
  124.     cin >> Answer;
  125.     cout << "\n";
  126.   } while (Answer == 'Y' || Answer == 'y');
  127.  
  128.   return 0;
  129. }