?? 3.cpp
字號(hào):
/* Program extracts from Chapter 3 of "Data Structures and Program Design in C++" by Robert L. Kruse and Alexander J. Ryba Copyright (C) 1999 by Prentice-Hall, Inc. All rights reserved. Extracts from this file may be used in the construction of other programs, but this code will not compile or execute as given here. */// Section 3.1:class Queue {public: Queue(); bool empty() const; Error_code append(const Queue_entry &x); Error_code serve(); Error_code retrieve(Queue_entry &x) const;// Additional members will represent queue data.};class Extended_queue: public Queue {public: bool full() const; int size() const; void clear(); Error_code serve_and_retrieve(Queue_entry &item);};Queue q;q.append('a');q.serve();q.append('b');q.serve();q.append('c');q.append('d');q.serve();Queue q;q.append('a');q.append('b');q.retrieve(x);q.serve();q.append('c');q.append(x);q.serve();q.serve();Queue q;q.append('a');x = 'b';q.append('x');q.retrieve(y);q.serve();q.append(x);q.serve();q.append(y);// Section 3.3:const int maxqueue = 10; // small value for testingclass Queue {public: Queue(); bool empty() const; Error_code serve(); Error_code append(const Queue_entry &item); Error_code retrieve(Queue_entry &item) const;protected: int count; int front, rear; Queue_entry entry[maxqueue];};Queue::Queue()/*Post: The Queue is initialized to be empty.*/{ count = 0; rear = maxqueue - 1; front = 0;}bool Queue::empty() const/*Post: Return true if the Queue is empty, otherwise return false.*/{ return count == 0;}Error_code Queue::append(const Queue_entry &item)/*Post: item is added to the rear of the Queue. If the Queue is fullreturn an Error_code of overflow and leave the Queue unchanged.*/{ if (count >= maxqueue) return overflow; count++; rear = ((rear + 1) == maxqueue) ? 0 : (rear + 1); entry[rear] = item; return success;}Error_code Queue::serve()/*Post: The front of the Queue is removed. If the Queueis empty return an Error_code of underflow.*/{ if (count <= 0) return underflow; count--; front = ((front + 1) == maxqueue) ? 0 : (front + 1); return success;}Error_code Queue::retrieve(Queue_entry &item) const/*Post: The front of the Queue retrieved to the output parameter item. If the Queue is empty return an Error_code of underflow.*/{ if (count <= 0) return underflow; item = entry[front]; return success;}int Extended_queue::size() const/*Post: Return the number of entries in the Extended_queue.*/{ return count;}// Section 3.4:int main()/*Post: Accepts commands from user as a menu-driven demonstration program for the class Extended_queue.Uses: The class Extended_queue and the functions introduction, get_command, and do_command.*/{ Extended_queue test_queue; introduction(); while (do_command(get_command(), test_queue));}void help()/*Post: A help screen for the program is printed, giving the meaning of each command that the user may enter.*/{ cout << endl << "This program allows the user to enter one command" << endl << "(but only one) on each input line." << endl << "For example, if the command S is entered, then" << endl << "the program will serve the front of the queue." << endl << endl << " The valid commands are:" << endl << "A - Append the next input character to the extended queue" << endl << "S - Serve the front of the extended queue" << endl << "R - Retrieve and print the front entry." << endl << "# - The current size of the extended queue" << endl << "C - Clear the extended queue (same as delete)" << endl << "P - Print the extended queue" << endl << "H - This help screen" << endl << "Q - Quit" << endl << "Press <Enter> to continue." << flush; char c; do { cin.get(c); } while (c != '\n');}bool do_command(char c, Extended_queue &test_queue)/*Pre: c represents a valid command.Post: Performs the given command c on the Extended_queue test_queue. Returns false if c == 'q', otherwise returns true.Uses: The class Extended_queue.*/{ bool continue_input = true; Queue_entry x; switch (c) { case 'r': if (test_queue.retrieve(x) == underflow) cout << "Queue is empty." << endl; else cout << endl << "The first entry is: " << x << endl; break; case 'q': cout << "Extended queue demonstration finished." << endl; continue_input = false; break; // Additional cases will cover other commands. } return continue_input;}// Section 3.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.*/{ int end_time; // time to run simulation int queue_limit; // size of Runway queues int flight_number = 0; double arrival_rate, departure_rate; initialize(end_time, queue_limit, arrival_rate, departure_rate); Random variable; Runway small_airport(queue_limit); for (int current_time = 0; current_time < end_time; current_time++) { // loop over time intervals int number_arrivals = variable.poisson(arrival_rate); // current arrival requests for (int i = 0; i < number_arrivals; i++) { Plane current_plane(flight_number++, current_time, arriving); if (small_airport.can_land(current_plane) != success) current_plane.refuse(); } int number_departures= variable.poisson(departure_rate); // current departure requests for (int j = 0; j < number_departures; j++) { Plane current_plane(flight_number++, current_time, departing); if (small_airport.can_depart(current_plane) != success) current_plane.refuse(); } Plane moving_plane; switch (small_airport.activity(current_time, moving_plane)) { // Let at most one Plane onto the Runway at current_time. case land: moving_plane.land(current_time); break; case takeoff: moving_plane.fly(current_time); break; case idle: run_idle(current_time); } } small_airport.shut_down(end_time);}enum Runway_activity {idle, land, takeoff};class Runway {public: Runway(int limit); Error_code can_land(const Plane ¤t); Error_code can_depart(const Plane ¤t); Runway_activity activity(int time, Plane &moving); void shut_down(int time) const;private: Extended_queue landing; Extended_queue takeoff; int queue_limit; int num_land_requests; // number of planes asking to land int num_takeoff_requests; // number of planes asking to take off int num_landings; // number of planes that have landed int num_takeoffs; // number of planes that have taken off
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -