~cpp #include "bus_and_man.h" #include <iostream> using namespace std; void Set_memory(); void Call_act(); road *cau_road; void main() { cout << "시스템을 초기화 합니다.\n"; Set_memory(); Call_act(); } void Set_memory() { cau_road=new road(3000); cau_road->Build("정문",5,0,5); cau_road->Build("issac",10,700,5); cau_road->Build("시장",3,1000,5); cau_road->Build("중문",10,1320,5); cau_road->Build("곰두리",10,1700,5); cau_road->Build("공대건물",2,2100,5); cau_road->Build("주차장",30,2750,5); cau_road->Build("후문",10,2999,5); } void Call_act() { for (register int i=1; i<20; ++i) { for (register int j=0; j<5; ++j) { cau_road->Act(); cout << "\n"; system("PAUSE"); cout << "\n"; } cau_road->Start_car(i,20,0,80); } delete cau_road; }
~cpp //버스 위치. //승객 //하는 일 #include <iostream> #include <stdlib.h> #include <time.h> #include "bus_and_man.h" using namespace std; const int MOVE=1; const int STOP=0; man::man(int input_from, int input_to) { from=input_from; to=input_to; } man::~man() { } int man::where_go() { return to; } station::station(int input_station_number, char *input_name, int input_percent, int input_size ) { strcpy(name,input_name); percent=input_percent; station_size=input_size; number_man=0; station_number=input_station_number; humans=new man*[station_size]; for (register int i=0; i<station_size; ++i) humans[i]=0; cout << name << "역이 생성되었습니다.\n"; } station::~station() { for (register int i=0; i<station_size; ++i) { if (0!=humans[i]) delete humans[i]; } delete humans; cout << name << "역이 파괴되었습니다.\n"; } void station::make_people(int numbers_station) { if (station_size!=number_man && numbers_station!=station_number+1) { if (0==rand()%percent) { humans[number_man]=new man(station_number,station_number+rand()%(numbers_station-station_number-1)+1); ++number_man; cout << name << "역에 사람이 한명 생성되었습니다.\n"; } } } man* station::out_people() { if (0==number_man) return 0; man *temp_man=humans[0]; humans[0]=0; for (register int i=1; i<number_man; ++i) { humans[i-1]=humans[i]; humans[i]=0; } --number_man; return temp_man; } int station::where_here() { return station_number; } void station::act(int numbers_station) { make_people(numbers_station); } void bus::move(road *in_road) { int in_station=0; int temp_where=where; temp_where=in_road->car_move(&in_station, speed, where); if (1==in_station) state=STOP; if (-1==temp_where) in_road->car_stop(where); else { where=temp_where; cout << number << "번 버스가 " << where << "로 이동하였습니다.\n"; } } void bus::stop(station *in_station, road* in_road) { man*in_people=in_station->out_people(); if (0!=in_people) { humans[number_man]=in_people; ++number_man; cout << number << "번 버스가 승객을 승차하였습니다.\n"; } int suched=0; for (register int i=0; i<number_man; ++i) { if (in_station->where_here()==humans[i]->where_go()) { delete humans[i]; humans[i]=0; for (register int j=i+1; j<number_man; ++j) { humans[j-1]=humans[j]; humans[j]=0; } --number_man; suched=1; cout << number << "번 버스가 승객을 하차하였습니다.\n"; } } if (0==suched && 0==in_people) { state=MOVE; move(in_road); } } bus::bus(int input_number, int input_size, int input_where, int input_speed) { number=input_number; bus_size=input_size; humans=new man*[bus_size]; state=STOP; where=input_where; number_man=0; speed=input_speed; for (register int i=0; i<bus_size; ++i) humans[i]=0; cout << number << "호차가 생성되었습니다.\n"; } bus::~bus() { for (register int i=0; i<bus_size; ++i) { if (0!=humans[i]) delete humans[i]; } delete humans; cout << number << "호차가 파괴되었습니다.\n"; } void bus::act(station* in_station, road* in_road) { if (STOP==state) stop(in_station, in_road); else move(in_road); } road::road (int input_long) { srand(time(NULL)); road_long=input_long; number_station=0; cars=new bus*[road_long]; buildings=new station*[road_long]; for (register int i=0; i<road_long; ++i) { cars[i]=0; buildings[i]=0; } cout << "도로1개가 생성되었습니다.\n"; } road::~road() { for (register int i=0; i<road_long; ++i) { if (0!=buildings[i]) delete buildings[i]; if (0!=cars[i]) delete cars[i]; } delete cars; delete buildings; cout << "도로1개가 파괴되었습니다.\n"; } int road::Build(char *input_name, int input_percent, int where, int input_size) { if (0!=buildings[where]) { cout << "역을 생성하는데 실패하였습니다.\n"; return -1; } buildings[where]=new station(number_station,input_name,input_percent,input_size); ++number_station; return 0; } int road::Start_car(int input_number, int input_size, int input_where, int input_speed) { if (0!=cars[input_where]) { cout << "차를 생성하는데 실패하였습니다.\n"; return -1; } cars[input_where]=new bus(input_number,input_size,input_where, input_speed); return 0; } void road::Act() { for (register int i=road_long-1; i>=0; --i) { if (0!=buildings[i]) buildings[i]->act(number_station); if (0!=cars[i]) cars[i]->act(buildings[i],this); } } int road::car_move(int *state, int speed, int where) { register int i; for (i=where+1; i<=where+speed; ++i) { if (i>=road_long) return -1; if (0!=cars[i]) break; if (0!=buildings[i]) { *state=1; ++i; break; } } if (i-1!=where) { cars[i-1]=cars[where]; cars[where]=0; } return i-1; } void road::car_stop(int where) { delete cars[where]; cars[where]=0; }
~cpp class man; class station; class bus; class road; class man{ private: int from; int to; public: man(int input_from, int input_to); ~man(); int where_go(); }; class station{ private: char name[256]; int station_number; int percent; int station_size; int number_man; man **humans; void make_people(int); public: station(int, char*, int, int); ~station(); man* out_people(); int where_here(); void act(int); }; class bus{ private: int number; int bus_size; int state; int where; man **humans; int number_man; int speed; void move(road*); void stop(station*, road*); public: bus(int , int , int , int ); ~bus(); void act(station* , road* ); }; class road{ private: int road_long; int number_station; bus **cars; station **buildings; public: road (int ); ~road(); int Build(char*, int , int, int ); int Start_car(int , int , int , int ); void Act(); int car_move(int*, int , int ); void car_stop(int); };
~cpp #include "bus_and_man.h" #include <iostream> using namespace std; void Set_memory(); void Call_act(); road *cau_road; void main() { cout << "시스템을 초기화 합니다.\n"; Set_memory(); Call_act(); } void Set_memory() { cau_road=new road(3000); cau_road->Build("정문",5,0,5); cau_road->Build("issac",10,700,5); cau_road->Build("시장",3,1000,5); cau_road->Build("중문",10,1320,5); cau_road->Build("곰두리",10,1700,5); cau_road->Build("공대건물",2,2100,5); cau_road->Build("주차장",30,2750,5); cau_road->Build("후문",10,2999,5); } void Call_act() { for (register int i=1; i<20; ++i) { for (register int j=0; j<5; ++j) { cau_road->Act(); cout << "\n"; system("PAUSE"); cout << "\n"; } cau_road->Start_car(i,20,0,80); } delete cau_road; }
~cpp //버스 위치. //승객 //하는 일 #include <iostream> #include <stdlib.h> #include <time.h> #include "bus_and_man.h" using namespace std; const int MOVE=1; const int STOP=0; man::man(int input_from, int input_to) { from=input_from; to=input_to; } man::~man() { } int man::where_go() { return to; } station::station(int input_station_number, char *input_name, int input_percent, int input_size, int input_where) { strcpy(name,input_name); percent=input_percent; station_size=input_size; number_man=0; station_number=input_station_number; where=input_where; humans=new man*[station_size]; for (register int i=0; i<station_size; ++i) humans[i]=0; cout << name << "역이 생성되었습니다.\n"; } station::~station() { for (register int i=0; i<station_size; ++i) { if (0!=humans[i]) delete humans[i]; } delete humans; cout << name << "역이 파괴되었습니다.\n"; } void station::make_people(int numbers_station) { if (station_size!=number_man && numbers_station!=station_number+1) { if (0==rand()%percent) { humans[number_man]=new man(station_number,station_number+rand()%(numbers_station-station_number-1)+1); ++number_man; cout << name << "역에 사람이 한명 생성되었습니다.\n"; } } } man* station::out_people() { if (0==number_man) return 0; man *temp_man=humans[0]; humans[0]=0; for (register int i=1; i<number_man; ++i) { humans[i-1]=humans[i]; humans[i]=0; } --number_man; return temp_man; } int station::where_here() { return station_number; } void station::act(int numbers_station) { make_people(numbers_station); } int station::where_am_i() { return where; } void bus::move(road *in_road) { int in_station=0; int temp_where=where; temp_where=in_road->car_move(&in_station, speed, where); if (1==in_station) state=STOP; if (-1==temp_where) in_road->car_stop(number); else { where=temp_where; cout << number << "번 버스가 " << where << "로 이동하였습니다.\n"; } } void bus::stop(station *in_station, road* in_road) { man*in_people=0; if (number_man<bus_size) { in_people=in_station->out_people(); if (0!=in_people) { humans[number_man]=in_people; ++number_man; cout << number << "번 버스가 승객을 승차하였습니다.\n"; } } int suched=0; for (register int i=0; i<number_man; ++i) { if (in_station->where_here()==humans[i]->where_go()) { delete humans[i]; humans[i]=0; for (register int j=i+1; j<number_man; ++j) { humans[j-1]=humans[j]; humans[j]=0; } --number_man; suched=1; cout << number << "번 버스가 승객을 하차하였습니다. (잔여승객수 : " << number_man << " ) \n"; break; } } if (0==suched && 0==in_people) { state=MOVE; move(in_road); } } bus::bus(int input_number, int input_size, int input_where, int input_speed) { number=input_number; bus_size=input_size; humans=new man*[bus_size]; state=STOP; where=input_where; number_man=0; speed=input_speed; for (register int i=0; i<bus_size; ++i) humans[i]=0; cout << number << "호차가 생성되었습니다.\n"; } bus::~bus() { for (register int i=0; i<bus_size; ++i) { if (0!=humans[i]) delete humans[i]; } delete humans; cout << number << "호차가 파괴되었습니다.\n"; } void bus::act(station* in_station, road* in_road) { if (STOP==state) stop(in_station, in_road); else move(in_road); } int bus::where_am_i() { return where; } int bus::what_number() { return number; } road::road (int input_long) { srand(time(NULL)); road_long=input_long; number_station=0; number_bus=0; cars=NULL; buildings=NULL; cout << "도로1개가 생성되었습니다.\n"; } road::~road() { for (register int i=0; i<number_bus; ++i) delete cars[i]; for (register int i=0; i<number_station; ++i) delete buildings[i]; if (cars!=NULL) delete cars; if (buildings!=NULL) delete buildings; cout << "도로1개가 파괴되었습니다.\n"; } int road::Build(char *input_name, int input_percent, int where, int input_size) { if (0!=number_station) { for (register int i=0; i<number_station; ++i) { if (buildings[i]->where_am_i()==where) { cout << "역을 생성하는데 실패하였습니다.\n"; return -1; } } station** temp=new station*[number_station+1]; for (register int i=0; i<number_station; ++i) { temp[i]=buildings[i]; } delete buildings; buildings=temp; buildings[i]=new station(number_station,input_name,input_percent,input_size,where); } else { buildings=new station*; buildings[0]=new station(number_station,input_name,input_percent,input_size,where); } ++number_station; return 0; } int road::Start_car( int input_number, int input_size, int input_where, int input_speed) { if (0!=number_bus) { for (register int i=0; i<number_bus; ++i) { if (cars[i]->where_am_i()==input_where) { cout << "차를 생성하는데 실패하였습니다.\n"; return -1; } } bus** temp=new bus*[number_bus+1]; for (register int i=0; i<number_bus; ++i) { temp[i]=cars[i]; } delete cars; cars=temp; cars[number_bus]=new bus(input_number,input_size,input_where, input_speed); } else { cars=new bus*; cars[0]=new bus(input_number,input_size,input_where, input_speed); } ++number_bus; return 0; } void road::Act() { for (register int i=0; i<number_station; ++i) buildings[i]->act(number_station); for (register int i=0; i<number_bus; ++i) { for (register int j=0; j<number_station; ++j) { if (buildings[j]->where_am_i()==cars[i]->where_am_i()) { cars[i]->act(buildings[j],this); break; } } if (j==number_station) cars[i]->act(0,this); } } int road::car_move(int *state, int speed, int where) { int temp_where=where+speed; for (register int i=0; i<number_station; ++i) { if (where<buildings[i]->where_am_i() && temp_where>=buildings[i]->where_am_i()) { temp_where=buildings[i]->where_am_i(); *state=1; } } for (register int i=0; i<number_bus; ++i) { if (where<cars[i]->where_am_i() && temp_where>=cars[i]->where_am_i()) { temp_where=cars[i]->where_am_i(); *state=0; } } if (temp_where>=road_long) return -1; if (*state==0) return temp_where-1; else return temp_where; } void road::car_stop(int number) { bus** temp=new bus*[number_bus-1]; int swit=0; for (register int i=0; i<number_bus; ++i) { if (0==swit) { if (cars[i]->what_number()==number) { delete cars[i]; swit=1; } else temp[i]=cars[i]; } else temp[i-1]=cars[i]; } delete cars; cars=temp; --number_bus; }
~cpp class man; class station; class bus; class road; class man{ private: int from; int to; public: man(int input_from, int input_to); ~man(); int where_go(); }; class station{ private: char name[256]; int station_number; int percent; int station_size; int number_man; int where; man **humans; void make_people(int); public: station(int, char*, int, int, int); ~station(); man* out_people(); int where_here(); void act(int); int where_am_i(); }; class bus{ private: int number; int bus_size; int state; int where; man **humans; int number_man; int speed; void move(road*); void stop(station*, road*); public: bus(int , int , int , int ); ~bus(); void act(station* , road* ); int where_am_i(); int what_number(); }; class road{ private: int road_long; int number_station; int number_bus; bus **cars; station **buildings; public: road (int ); ~road(); int Build(char*, int , int, int ); int Start_car( int, int , int , int ); void Act(); int car_move(int*, int , int ); void car_stop(int); };