Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include "DallarAmount.h"
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.    DollarAmount d1(123, 45); // $123.45
  10.    DollarAmount d2{1576}; // $15.76
  11.  
  12.    cout << "After adding d2 (" << d2.toString() << ") into d1 ("
  13.       << d1.toString() << "), d1 = ";
  14.    d1.add(d2); // modifies object d1
  15.    cout << d1.toString() << "\n";
  16.  
  17.    cout << "After subtracting d2 (" << d2.toString() << ") from d1 ("
  18.       << d1.toString() << "), d1 = ";
  19.    d1.subtract(d2); // modifies object d1
  20.    cout << d1.toString() << "\n";
  21.  
  22.    cout << "After subtracting d1 (" << d1.toString() << ") from d2 ("
  23.       << d2.toString() << "), d2 = ";
  24.    d2.subtract(d1); // modifies object d2
  25.    cout << d2.toString() << "\n\n";
  26.  
  27.    cout << "Enter integer interest rate and divisor. For example:\n"
  28.       << "for     2%, enter:    2 100\n"
  29.       << "for   2.3%, enter:   23 1000\n"
  30.       << "for  2.37%, enter:  237 10000\n"
  31.       << "for 2.375%, enter: 2375 100000\n> ";
  32.    int rate; // whole-number interest rate
  33.    int divisor; // divisor for rate
  34.    cin >> rate >> divisor;
  35.  
  36.    DollarAmount balance{100000}; // initial principal amount in pennies
  37.    cout << "\nInitial balance: " << balance.toString() << endl;
  38.  
  39.    cout << "\nYear" << setw(20) << "Amount on deposit" << endl;
  40.  
  41.    for (unsigned int year{1}; year <= 10; year++) {
  42.       balance.addInterest(rate, divisor);
  43.  
  44.       cout << setw(4) << year << setw(20) << balance.toString() << endl;
  45.    }
  46. }
  47.  
  48.  
  49.  
  50. #include <string>
  51. #include <cmath>
  52.  
  53. class DollarAmount {
  54. public:
  55.    explicit DollarAmount(int64_t value) : amount{value} { }
  56.  
  57.    explicit DollarAmount(int64_t dollars, int64_t cents){
  58.      amount = ( dollars * 100 + cents ) / 100;
  59.    }
  60.  
  61.    void add(DollarAmount right) {
  62.       amount += right.amount;
  63.    }
  64.  
  65.    void subtract(DollarAmount right) {
  66.       amount -= right.amount;
  67.    }
  68.  
  69.    void addInterest(int rate, int divisor) {
  70.       DollarAmount interest{
  71.          (amount * rate + divisor / 2) / divisor
  72.       };
  73.  
  74.       add(interest);
  75.    }
  76.  
  77.    std::string toString() const {
  78.       std::string dollars{std::to_string(amount / 100)};
  79.       std::string cents{std::to_string(std::abs(amount % 100))};
  80.       return dollars + "." + (cents.size() == 1 ? "0" : "") + cents;
  81.    }
  82. private:
  83.    int64_t amount{0};
  84. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement