fork download
  1. #include <iostream>
  2.  
  3. struct Double
  4. {
  5. int characteristic;
  6. char decimal;
  7. int mantissa;
  8.  
  9. friend std::ostream &operator <<(std::ostream &stm, Double const &rhs)
  10. {
  11. return stm << rhs.characteristic << rhs.decimal << rhs.mantissa;
  12. }
  13.  
  14. friend std::istream &operator >>(std::istream &stm, Double &rhs)
  15. {
  16. return stm >> rhs.characteristic >> rhs.decimal >> rhs.mantissa;
  17. }
  18.  
  19. Double operator-(Double const &rhs) const
  20. {
  21. Double result = *this;
  22. if(rhs.mantissa > result.mantissa)
  23. {
  24. --result.characteristic;
  25. result.mantissa += 100;
  26. }
  27.  
  28. result.characteristic -= rhs.characteristic;
  29. result.mantissa -= rhs.mantissa;
  30.  
  31. return result;
  32. }
  33. };
  34.  
  35. int main()
  36. {
  37. Double itemCost;
  38. std::cout << "Please enter the item cost: ";
  39. std::cin >> itemCost;
  40.  
  41. Double amountPaid;
  42. std::cout << "Please enter the amount paid: ";
  43. std::cin >> amountPaid;
  44.  
  45. Double change = amountPaid - itemCost;
  46. std::cout << "Change due $" << change << std::endl;
  47. //now you can get the exact change easy
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 3348KB
stdin
15.55
20.0
stdout
Please enter the item cost: Please enter the amount paid: Change due $4.45