Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- class Car
- {
- public:
- void init(double, double);
- double re_gas(){ return gas; };
- double re_eff(){ return eff; };
- void go(double);
- private:
- double gas, eff;
- };
- void Car::init(double E, double G)
- {
- gas = G;
- eff = E;
- }
- void Car::go(double kilo)
- {
- if (gas >= kilo/eff)
- {
- gas -= kilo / eff;
- if (gas == 0)
- {
- cout << "油剛好用完了!請加油!\n";
- }
- else
- {
- cout << "跑完了 " << kilo << " 公里,油還剩下 " << gas << " 公升\n";
- }
- }
- else
- {
- cout << "油量不夠!現在的油只夠跑 " << gas*eff << " 公里\n";
- }
- }
- void main()
- {
- Car superone;
- superone.init(20, 30);
- cout << "一台超級車一公升可以跑" << superone.re_eff() << "公里\n"<<"現在有"<<superone.re_gas()<<"公升汽油";
- float length;
- while (superone.re_gas() > 0)
- {
- cout << "請輸入要跑多遠:";
- cin >> length;
- superone.go(length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement