home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / advfun9.cpp < prev    next >
C/C++ Source or Header  |  1993-05-17  |  3KB  |  114 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. double StraightLine(double origVal, double scrapVal, 
  10.                     unsigned serviceLife, unsigned year)
  11. // straight line depreciation method
  12. {
  13.   double deprRate = (origVal - scrapVal) / serviceLife;
  14.   
  15.   return origVal - year * deprRate;
  16. }
  17.  
  18. double DeclineBalance(double origVal, double scrapVal, 
  19.                       unsigned serviceLife, unsigned year)
  20. // declining balance depreciation method                      
  21. {
  22.   double deprCoef = 1 - pow(scrapVal / origVal, 
  23.                             1.0/serviceLife);
  24.   
  25.   return origVal * pow((1 - deprCoef), year);
  26. }
  27.  
  28.  
  29. double SumOfDigits(double origVal, double scrapVal, 
  30.                    unsigned serviceLife, unsigned year)
  31. // sum of digits depreciation method                      
  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. void CalcDeprTable(double origVal, double scrapVal, 
  43.                    unsigned serviceLife,
  44.                    double (*f)(double, double, unsigned, unsigned))
  45. // calculate the depreciation table
  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. // promtp for and obtain double-type number
  56. {
  57.   do {
  58.     cout << prompt;
  59.     cin >> x;
  60.     cout << "\n";
  61.   } while (x <= 0);
  62. }
  63.  
  64. void GetUnsigned(const char* prompt, unsigned& n)
  65. // prompt for and obtain unsigned integer
  66. {
  67.   do {
  68.     cout << prompt;
  69.     cin >> n;
  70.     cout << "\n";
  71.   } while (n < 1);
  72.  
  73. unsigned SelectDeprFunction()
  74. // prompt for and obtain the depreciation function
  75. {
  76.   int n;
  77.   
  78.   cout << "Select a depreciation method:\n"
  79.        << "1) Straight line\n"
  80.        << "2) Declining balance\n"
  81.        << "3) Sum of digits\n\n";
  82.   cout << "Enter Choice by number: ";
  83.   cin >> n;
  84.   cout << "\n\n";
  85.   return (n >= 1 && n <= 3) ? n : 1;
  86. }
  87.                    
  88. main()
  89. {                  
  90.   double OrigVal;
  91.   double ScrapVal;
  92.   unsigned ServiceLife;
  93.   double (*DeprFun[3])(double, double, unsigned, unsigned);
  94.   unsigned Choice;
  95.   char Answer;
  96.  
  97.   DeprFun[0] = StraightLine;
  98.   DeprFun[1] = DeclineBalance;
  99.   DeprFun[2] = SumOfDigits;
  100.  
  101.   do {                                    
  102.     GetDouble("Enter original value: ", OrigVal);
  103.     GetDouble("Enter scrap value: ", ScrapVal);
  104.     GetUnsigned("Enter service life: ", ServiceLife);
  105.     Choice = SelectDeprFunction() - 1;
  106.     CalcDeprTable(OrigVal, ScrapVal, ServiceLife, DeprFun[Choice]);
  107.     cout << "More calculations ? (Y/N) ";
  108.     cin >> Answer;
  109.     cout << "\n";
  110.   } while (Answer == 'Y' || Answer == 'y');
  111.  
  112.   return 0;
  113. }