bài giảng ký thuật lập trình các cấu trúc dữ liệu cơ bản đại học bách khoa hà nội

2 490 0
bài giảng ký thuật lập trình các cấu trúc dữ liệu cơ bản đại học bách khoa hà nội

Đang tải... (xem toàn văn)

Thông tin tài liệu

1 Case Study: Mô phỏng sân bay Nhậndạng các lớp •Lớp Plane, các phương thức? –Khởitạo –Hạ cánh –Cất cánh •LớpRunway: –Chứa thành phầnthể hiện hàng đợi củacác máy bay đang đợicất cánh hoặchạ cánh. •Lớp Random: –trạng thái ngẫu nhiên củaviệccất cánh và hạ cánh củamáybay Cài đặt const int END_TIME = 30; // time to run simulation const int QUEUE_LIMIT = 60; // size of Runway queues const double ARRIVAL_RATE = 0.5, DEPARTURE_RATE = 0.5; int main( ) // Airport simulation program /* Pre: The user must supply the number of time intervals the simulation is to run, the expected number of planes arriving, the expected number of planes departing per time interval, and the maximum allowed size for runway queues. Post: The program performs a random simulation of the airport, showing the status of the runway at each time interval, and prints out a summary of airport operation at the conclusion. Uses: Classes Runway, Plane, Random and functions run_idle, initialize . * / 1. { 2. int flight_number = 0; 3. Random random; 4. Runway small_airport(queue_limit); 5. for (int current_time = 0; current_time < END_TIME; current_time++) 6. { 7. // loop over time intervals 8. int number_arrivals = random.poisson(ARRIVAL_RATE); 9. // current arrival requests 10. for (int i = 0; i < number_arrivals; i++) { 11. Plane current_plane(flight_number++, current_time, arriving); 12. if (small_airport.can_land(current_plane) != success) 13 current plane refuse () ; 18. int number_departures = random.poisson(DEPARTURE_RATE); 19. // current departure requests 20. for (int j = 0; j < number_departures; j++) { 21. Plane current_plane(flight_number++, current_time, departing); 22. if (small_airport.can_depart(current_plane) != success) 23. current_plane.refuse( ); 24. } 25. Plane moving_plane; 26. switch (small_airport.activity(current_time, moving_plane)) { 27. // Let at most one Plane onto the Runway at current_time. 28. case land: 29. moving_plane.land(current_time); 30. break; 31. case takeoff: 32. moving_plane.fly(current_time); 33. break; 34. case idle: 35. run_idle(current_time); 36 } // end of switch Class Runway •Cầnquản lý hai hàng đợi –Cất cánh và hạ cánh • Ưu tiên máy bay đợihạ cánh •Yêucầuthống kê: –Số máy bay đượcxử lý –Thờigianđợi trung bình –Số máy bay bị từ chối 2 1. enum Runway_activity {idle, land, takeoff}; 2. class Runway { 3. public: 4. Runway(int limit); 5. Error_code can_land(const Plane &current); 6. Error_code can_depart(const Plane &current); 7. Runway_activity activity(int time, Plane &moving); 8. void shut_down(int time) const; 9. private: 10. Queue landing; 11. Queue takeoff; 12. int queue_limit; 13. int num_land_requests; // number of planes asking to land 14. int num_takeoff_requests; // number of planes asking to take off 15. int num_landings; // number of planes that have landed 16. int num_takeoffs; // number of planes that have taken off 17. int num_land_accepted; // number of planes queued to land 18. int num_takeoff_accepted; // number of planes queued to take off 19. int num_land_refused; // number of landing planes refused 20. int num_takeoff_refused; // number of departing planes refused 21 int land wait ; // total time of planes waiting to land Định nghĩalớpPlane 1. enum Plane_status {null, arriving, departing}; 2. class Plane { 3. public: 4. Plane( ); 5. Plane(int flt, int time, Plane_status status); 6. void refuse( ) const; 7. void land(int time) const; 8. void fly(int time) const; 9. int started( ) const; 10.private: 11. int flt_num; 12. int clock_start; 13. Plane_status state; 14.}; [...]...Thiết kế chương trình • • • • Tổ chức tốt từng phần Mã lệnh phải viết rõ ràng, dễ hiểu Lựa chọn cấu trúc dữ liệu phù hợp Phân tích thuật toán 1 2 Phong cách lập trình Đặt tên • Mục đích của tên: – Lớp và biến: đại diện cho cái gì – Hàm: thực hiện công việc gì – Giải thích chi tiết Một số chú ý khi đặt tên 1 Lựa chọn cẩn thận tên hàm, lớp, hằng, biến toàn cục 2 Tên biến cục... instructions( ), user_says_yes( ) */ Định dạng chương trình • Canh lề giúp chương trình dễ hiểu Thời gian đọc chương trình lớn hơn rất nhiều so với viết chương trình Làm chương trình dễ đọc nhất có thể Tinh chỉnh và module hóa • Tinh chỉnh top-down: – Bắt đầu bằng viết chương trình chính – Quyết định việc chia các công việc (cho lớp, hàm, CTDL) – Tiếp tục với các hàm, lớp, CTDL Tinh chỉnh Top-down – Hướng dẫn... chương trình cung cấp dữ liệu cho hàm và đánh giá kết quả int main ( ) // driver for neighbor_count( ) { Life iConfig; iConfig.initialize( ); for (row = 1; row . 1 Case Study: Mô phỏng sân bay Nhậndạng các lớp •Lớp Plane, các phương thức? –Khởitạo –Hạ cánh –Cất cánh •LớpRunway: –Chứa thành phầnthể hiện hàng đợi củacác máy bay đang đợicất cánh hoặchạ cánh. •Lớp. break; 34. case idle: 35. run_idle(current_time); 36 } // end of switch Class Runway •Cầnquản lý hai hàng đợi –Cất cánh và hạ cánh • Ưu tiên máy bay đợihạ cánh •Yêucầuthống kê: –Số máy bay đượcxử lý –Thờigianđợi

Ngày đăng: 19/10/2014, 18:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan