Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Car
- {
- public:
- void init(double, double);
- double getEff() { return eff; }
- double checkgas(){ return gas; }
- double go(double);
- private:
- double gas;
- double eff;
- };
- void Car::init(double G, double E)
- {
- gas = G;
- eff = E;
- }
- double Car::go(double kilo)
- {
- if (gas >= (kilo / eff))
- {
- gas -= (kilo / eff);
- cout << "油箱還剩下" << gas << "公升的油" << endl;
- if (gas == 0)
- cout << "沒油了!!" << endl;
- }
- else
- {
- cout << "油不夠了,現在的油只夠跑" << (kilo = gas*eff) << "公里" << endl;
- gas = 0;
- }
- return kilo;
- }
- int main()
- {
- Car super;
- super.init(20, 30);
- cout << "超級省油車1公升的油可跑" << super.getEff()<<"公里"<< endl;
- cout << "油箱還有" << super.checkgas()<<"公升的油" << endl;
- while (super.checkgas() > 0)
- {
- double kilo;
- cout << "現在要開幾公里?" << endl;
- cin >> kilo;
- super.go(kilo);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement