精華區beta NTUCH-HW 關於我們 聯絡資訊
#include <iostream> #include <iomanip> using namespace std; #include <string> #include <fstream> #include <cstdlib> class elevator { private: int maxfloor; int nowfloor; public: elevator(); elevator(int); void settop(int); void respond(int); }; elevator::elevator() { maxfloor=15; nowfloor=1; } elevator::elevator(int wantfloor) { maxfloor=15; nowfloor=wantfloor; } void elevator::settop(int max) { maxfloor=max; } void elevator::respond(int newfloor) { if(newfloor<1||newfloor>maxfloor||maxfloor==nowfloor) ; else if (newfloor>nowfloor) { cout<<"starting at floor "<<nowfloor<<endl; while(newfloor>nowfloor) { nowfloor++; cout<<"going up - now at floor "<<nowfloor<<endl; } cout<<"stop at floor "<<nowfloor<<endl; } else if (newfloor<nowfloor) { cout<<"starting at floor"<<nowfloor<<endl; while(newfloor<nowfloor) { nowfloor--; cout<<"going down - now at floor "<<nowfloor<<endl; } cout<<"stop at floor "<<nowfloor<<endl; } } #include <iostream> #include <iomanip> using namespace std; #include <string> #include <fstream> #include <cstdlib> #include "elevator.h" int main() { elevator a(3),b; a.respond(8); system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59