Advertisement
wendy890711

類別

Apr 8th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. class Car
  7. {
  8. public:
  9. void init(double, double);
  10. double getEff() { return eff; }
  11. double checkgas(){ return gas; }
  12. double go(double);
  13. private:
  14. double gas;
  15. double eff;
  16. };
  17.  
  18.  
  19. void Car::init(double G, double E)
  20. {
  21. gas = G;
  22. eff = E;
  23. }
  24.  
  25. double Car::go(double kilo)
  26. {
  27. if (gas >= (kilo / eff))
  28. {
  29. gas -= (kilo / eff);
  30. cout << "油箱還剩下" << gas << "公升的油" << endl;
  31. if (gas == 0)
  32. cout << "沒油了!!" << endl;
  33. }
  34. else
  35. {
  36. cout << "油不夠了,現在的油只夠跑" << (kilo = gas*eff) << "公里" << endl;
  37. gas = 0;
  38. }
  39. return kilo;
  40. }
  41. int main()
  42. {
  43.  
  44. Car super;
  45. super.init(20, 30);
  46.  
  47. cout << "超級省油車1公升的油可跑" << super.getEff()<<"公里"<< endl;
  48. cout << "油箱還有" << super.checkgas()<<"公升的油" << endl;
  49.  
  50. while (super.checkgas() > 0)
  51. {
  52. double kilo;
  53. cout << "現在要開幾公里?" << endl;
  54. cin >> kilo;
  55. super.go(kilo);
  56. }
  57.  
  58.  
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement