preface
Last week, I helped my classmates do a project assignment. See the following figure for specific requirements. The implementation language is c + +.
Today, I decided to write this program again. The specific and complete code is implemented at the end of the article
Requirements analysis -- programming ideas
Structure, class object - seat
According to the description of the topic, we have at least four attributes for a seat class, so our implementation ideas are as follows:
1. The seat list is displayed in alphabetical order (number) according to c) in the menu, so we can use character form to store the number;
2. Use a bool value to judge whether the seat is reserved;
3. Two strings are used to represent the sex and name of the predetermined person respectively.
Then, since there are 12 seats in the title, we might as well use a seat array. The author used the method of class nesting in the code. At that time, he wanted to access through the class object pointer of the seat table to save the incoming space. At the same time, it would be easier to use when counting the number of seats in the back.
initialization
When creating a seat table object, you may wish to initialize it and assign a value to the seat number attribute of the corresponding object of the seat table according to A-L in a cycle.
Function implementation - menu
1. The menu display is similar to the previous address book. You only need to encapsulate a function and call it in a loop. At the same time, for a certain visual effect, press any key to continue + clear the screen once in a loop.
2. Call structure. First, as mentioned above, loop, and then use another branch structure switch. Each option corresponds to a function call.
3. Function realization logic of deleting seats and reserved seats:
For deletion, pay attention to judge whether the seat matches (i.e. whether the seat is empty and matches the customer's name)
For reservation, you need to judge whether the input meets the requirements (whether the seat number is empty)
code
#include<iostream> #include<string> using namespace std; #define Max_size 12 // Seat class class Seat { public: char number; // Seat number A - L bool is_empty; // Judge whether it is empty false full, true empty string name; // Name of the subscriber string lastname; // Last name of the intended person }; // Seating table - Seating array class seats_table { public: // Seating table Seat Seats[Max_size]; // Number of actual seats in the seat table int m_size; }; // Menu display void show_menu() { cout << "To choose a functions, enter its letter label:" << endl << "a) Show number of empty seats" << endl << "b) Show list of empty seats" << endl << "c) Show alphabetical list of seats" << endl << "d) Assign a customer to seat assignment" << endl << "e) Delete a seat assignment" << endl << "f) Quit" << endl; } // Displays the number of empty seats void show_num_of_empty(seats_table* Seats) { cout << "Numer of empty seats: " << Max_size - Seats->m_size << endl; system("pause"); // Any key to continue system("cls"); // Screen clearing operation } // The empty seat list displays the seat number void show_Listofempty(seats_table* S) { cout << "List of empty seats:" << endl; for (int i = 0; i < Max_size; ++i) { if (S->Seats[i].is_empty) { cout << S->Seats[i].number << "\t"; } } cout << endl; /*system("pause"); system("cls");*/ } // Output the seat list in alphabetical order (i.e. output the information of all (12) seats) void show_ListofSeats(seats_table* St) { cout << "List of Seats:" << endl; for (int i = 0; i < Max_size; ++i) { cout << St->Seats[i].number << "\t"; if (St->Seats[i].is_empty) { cout << "Empty" << endl; } else { cout << "Not empty" << "\t" << St->Seats[i].name << "\t" << St->Seats[i].lastname << endl; } } system("pause"); system("cls"); } // Assign a customer to a seat assignment void assign_seats(seats_table* St) { // Call the function to display the empty seat list show_Listofempty(St); char judge; do { // Output prompt information cout << "Please choose the num of seat you want choose:" << endl; char sel; cin >> sel; // First judge whether it is the correct input if (sel < 'A' || sel > 'L') { cout << "Input Error!" << endl; } else { // Then judge whether the seat is empty if (!St->Seats[sel - 'A'].is_empty) { // Non empty cout << "The seat is not empty!" << endl; } else { // According to the letter number, the number of people in the corresponding seat position (i.e. non empty, set no), and the total number of actual seats (non empty seats) is + 1 St->Seats[(int)(sel - 'A')].is_empty = false; St->m_size++; // Output prompt information (remind user to enter name) cout << "Please input your name:" << endl; string na; cin >> na; St->Seats[(int)(sel - 'A')].name = na; cout << "Please input your lastname:" << endl; string ln; cin >> ln; St->Seats[(int)(sel - 'A')].lastname = ln; // Output the prompt of successful Booking cout << "Successful reservation!" << endl; } } cout << "Continue? (Y/N)" << endl; cin >> judge; } while (judge == 'Y'); system("pause"); system("cls"); } // delete a seat assignment // To be honest, there is a problem with this mechanism. It should not be operated by customers, but by staff void d_seat(seats_table* St) { char num; char judge; do { // Output prompt information (seat number to be deleted) cout << "Please input the number(A-L) of seat you want delete:" << endl; cin >> num; // First judge whether it is the correct input if (num < 'A' || num > 'L') { cout << "Input Error!" << endl; } else { // Judge whether it is empty // Empty seat if (St->Seats[num - 'A'].is_empty) { cout << "The seat is empty." << endl; } else { cout << "Please input your name and lastname:" << endl; string na, lna; cin >> na >> lna; // Judge whether the first name and last name of the seat match if (St->Seats[num - 'A'].name == na && St->Seats[num - 'A'].lastname == lna) { St->Seats[num - 'A'].is_empty = true; St->m_size--; St->Seats[num - 'A'].name = ""; St->Seats[num - 'A'].lastname = ""; cout << "Seat withdrawal succeeded!" << endl; } else { cout << "The name or lastname is not matchied!" << endl; } } } cout << "Continue? (Y/N)" << endl; cin >> judge; } while (judge == 'Y'); system("pause"); system("cls"); } int main() { seats_table S; // Initialization, the actual seat is 0 S.m_size = 0; // Cycle, initialize the seat number (A - L) and empty it all for (int i = 0; i < Max_size; i++) { // 97--'A' S.Seats[i].number = (char)(i + 65); S.Seats[i].is_empty = true; } // Declare selection variables char select = 'a'; // Cycle through the display menu while (1) { // Menu call show_menu(); bool b = false; cin >> select; switch (select) { case 'a': show_num_of_empty(&S); break; case 'b': show_Listofempty(&S); system("pause"); system("cls"); break; case 'c': show_ListofSeats(&S); break; case 'd': assign_seats(&S); break; case 'e': d_seat(&S); break; // Abort the operation and exit the loop (while loop) case 'f': b = true; break; // Other situations default: cout << "Input error!" << endl; system("pause"); system("cls"); } if (b) { break; } } system("pause"); return 0; }