zhe ge ti de wan zheng ban ben jiu shi zhege!!!!
Task
You have to write a program that will act as a ticket booking system for trains. The user will enter the origin and destination train stations then the system should work out if there is a direct train between the two stations. If there is, it should check to see if there are enough free seats left on that train. If there are, the seats should be reserved for that booking and should display the cost of the tickets. The program can also show the remaining tickets for each available route. The program will be driven by a simple menu system.
The stations and available trains are shown in the following table. The origin station is on the left and the destination station is on the top. If there is a number in a cell, that is the distance in km that the train has to travel. If there is no number then that means there is no train running on that route.
Ningbo Hangzhou Suzhou Changzhou Shanghai Taizhou Wenzhou Jinhua Nanjing
Ningbo 155 380
Hangzhou 155 210 180 180 280
Suzhou 95 90
Changzhou 210 95 130
Shanghai 180 90
Taizhou 380 610
Wenzhou 610 235
Jinhua 180 235
Nanjing 280 130
Stations are numbered, from 1, in the order they appear in the rows of the table.
When the program starts, the user should be shown a menu of the available actions and prompted to enter one:
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option:
If the user enters option 1, the program should print a list of all the station numbers and their names, then print a prompt asking the user to enter the origin station number, then a prompt asking the user to enter the destination station number. The station list should have a header with Num and Station column names, separated with a tab. The rows should be the station number and then the full station name, separated with a tab. See the examples below for the exact formatting.
After prompting the user for the origin station, you should check the input is a number that appears in the list. If not, you should print the error message "Invalid station number." and go back to the main menu. The same check should happen after the destination station number has been entered. If there is no train from the origin to the destination then the program should display "No train between X and Y." where X is the origin station name and Y is the destination station name, and return to the main menu.
Next, the program should ask the user for the number of tickets required. You should check the input is greater than zero, otherwise display the error message "Invalid number of tickets." and go back to the main menu. After the required number of tickets has been validated, the program should check if at least that number of tickets is still available for that journey. When the program starts, all train routes have 10 tickets available each (your program should not make any attempt to remember how many tickets are available between runs; always start with 10 when the program runs). If there are not enough tickets left display the error message "Not enough tickets available (X requested, Y reminaing)." where X is the number of tickets the user entered and Y is the number of tickets actually available.
Finally, if there are enough tickets available the program should reduce the number of available tickets by the amount the user requested and print a message containing the cost of the tickets. The cost is calculated in the following manner — the cost of a single ticket is 300 RMB for a journey over 500km, 200 RMB for a journey over 250km, and 100 RMB otherwise; the total cost is the single ticket price multiplied by the number of tickets. The message displayed should be "Tickets booked, total cost is X RMB." where X is the total cost calculated above. After this the program should return to the main menu.
If the user enters option 2 at the main menu then the program should print a table showing the number of tickets remaining for each train route. The table should have the names of the stations along the top row, then each row showing the origin station at the beginning of the row and the appropriate number of remaining tickets of each destination station at each cell across the row. Cells should be tab-separated. If no route exists between two stations then nothing should be shown in the cell. In order to maintain the layout of the table, the station names are limited to 6 characters. If the station name is longer then it should be truncated to 6 characters. See the examples for a demonstration of the exact layout. After the table has been printed the program should return to the main menu.
If the user enters option 3 at the main menu then the program should exit with no further output.
If the user enters a different number at the main menu then the program should print "Invalid option!" and return to the main menu.
See the examples below for the rest of the specification of the required text and layout of the prompts and messages. Your program should look exactly the same as the examples for the values entered in them, including spacing, capitalisation, and punctuation.
Example input/output
Example 1:
[zlizpd3@cs-linux ex2]$ ./trainbooking
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 10 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 3
Destination station number: 5
Number of tickets: 4
Tickets booked, total cost is 400 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 10 10
Hangzh 10 10 10 10 10
Suzhou 10 6
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 3
[zlizpd3@cs-linux ex2]$
Example 2:
[zlizpd3@cs-linux ex2]$ ./trainbooking
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 5
Invalid option!
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 10 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 1
Destination station number: 1
No train between Ningbo and Ningbo.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 1
Destination station number: 2
Number of tickets: 12
Not enough tickets available (12 requested, 10 remaining).
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 1
Destination station number: 2
Number of tickets: 4
Tickets booked, total cost is 400 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 6 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 5
Destination station number: 3
Number of tickets: 1
Tickets booked, total cost is 100 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 6 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 9
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 3
[zlizpd3@cs-linux ex2]$
Task
You have to write a program that will act as a ticket booking system for trains. The user will enter the origin and destination train stations then the system should work out if there is a direct train between the two stations. If there is, it should check to see if there are enough free seats left on that train. If there are, the seats should be reserved for that booking and should display the cost of the tickets. The program can also show the remaining tickets for each available route. The program will be driven by a simple menu system.
The stations and available trains are shown in the following table. The origin station is on the left and the destination station is on the top. If there is a number in a cell, that is the distance in km that the train has to travel. If there is no number then that means there is no train running on that route.
Ningbo Hangzhou Suzhou Changzhou Shanghai Taizhou Wenzhou Jinhua Nanjing
Ningbo 155 380
Hangzhou 155 210 180 180 280
Suzhou 95 90
Changzhou 210 95 130
Shanghai 180 90
Taizhou 380 610
Wenzhou 610 235
Jinhua 180 235
Nanjing 280 130
Stations are numbered, from 1, in the order they appear in the rows of the table.
When the program starts, the user should be shown a menu of the available actions and prompted to enter one:
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option:
If the user enters option 1, the program should print a list of all the station numbers and their names, then print a prompt asking the user to enter the origin station number, then a prompt asking the user to enter the destination station number. The station list should have a header with Num and Station column names, separated with a tab. The rows should be the station number and then the full station name, separated with a tab. See the examples below for the exact formatting.
After prompting the user for the origin station, you should check the input is a number that appears in the list. If not, you should print the error message "Invalid station number." and go back to the main menu. The same check should happen after the destination station number has been entered. If there is no train from the origin to the destination then the program should display "No train between X and Y." where X is the origin station name and Y is the destination station name, and return to the main menu.
Next, the program should ask the user for the number of tickets required. You should check the input is greater than zero, otherwise display the error message "Invalid number of tickets." and go back to the main menu. After the required number of tickets has been validated, the program should check if at least that number of tickets is still available for that journey. When the program starts, all train routes have 10 tickets available each (your program should not make any attempt to remember how many tickets are available between runs; always start with 10 when the program runs). If there are not enough tickets left display the error message "Not enough tickets available (X requested, Y reminaing)." where X is the number of tickets the user entered and Y is the number of tickets actually available.
Finally, if there are enough tickets available the program should reduce the number of available tickets by the amount the user requested and print a message containing the cost of the tickets. The cost is calculated in the following manner — the cost of a single ticket is 300 RMB for a journey over 500km, 200 RMB for a journey over 250km, and 100 RMB otherwise; the total cost is the single ticket price multiplied by the number of tickets. The message displayed should be "Tickets booked, total cost is X RMB." where X is the total cost calculated above. After this the program should return to the main menu.
If the user enters option 2 at the main menu then the program should print a table showing the number of tickets remaining for each train route. The table should have the names of the stations along the top row, then each row showing the origin station at the beginning of the row and the appropriate number of remaining tickets of each destination station at each cell across the row. Cells should be tab-separated. If no route exists between two stations then nothing should be shown in the cell. In order to maintain the layout of the table, the station names are limited to 6 characters. If the station name is longer then it should be truncated to 6 characters. See the examples for a demonstration of the exact layout. After the table has been printed the program should return to the main menu.
If the user enters option 3 at the main menu then the program should exit with no further output.
If the user enters a different number at the main menu then the program should print "Invalid option!" and return to the main menu.
See the examples below for the rest of the specification of the required text and layout of the prompts and messages. Your program should look exactly the same as the examples for the values entered in them, including spacing, capitalisation, and punctuation.
Example input/output
Example 1:
[zlizpd3@cs-linux ex2]$ ./trainbooking
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 10 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 3
Destination station number: 5
Number of tickets: 4
Tickets booked, total cost is 400 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 10 10
Hangzh 10 10 10 10 10
Suzhou 10 6
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 3
[zlizpd3@cs-linux ex2]$
Example 2:
[zlizpd3@cs-linux ex2]$ ./trainbooking
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 5
Invalid option!
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 10 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 1
Destination station number: 1
No train between Ningbo and Ningbo.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 1
Destination station number: 2
Number of tickets: 12
Not enough tickets available (12 requested, 10 remaining).
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 1
Destination station number: 2
Number of tickets: 4
Tickets booked, total cost is 400 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 6 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 10
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num Station
1 Ningbo
2 Hangzhou
3 Suzhou
4 Changzhou
5 Shanghai
6 Taizhou
7 Wenzhou
8 Jinhua
9 Nanjing
Origin station number: 5
Destination station number: 3
Number of tickets: 1
Tickets booked, total cost is 100 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
Ningbo Hangzh Suzhou Changz Shangh Taizho Wenzho Jinhua Nanjin
Ningbo 6 10
Hangzh 10 10 10 10 10
Suzhou 10 10
Changz 10 10 10
Shangh 10 9
Taizho 10 10
Wenzho 10 10
Jinhua 10 10
Nanjin 10 10
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 3
[zlizpd3@cs-linux ex2]$