|
用C++写代码:定义一个高铁类,具有高铁编号,高铁承载人数,高铁路线节点以及不同节点的到达和停留时 间,高铁拥有座位 定义一个乘客类,具有乘客编号,乘客车票,乘客姓名,乘客身份证号,乘客联系方式 定义一个车票类,具有车票编号,车票所属车次,上车节点时间,下车节点时间,座位编号,车 票所属用户 定义一个座位类,具有所属车次,该节点或时间的乘客 注意: 高铁路线用图表示 该乘客是否可以上车 该乘客座位是否有人 该乘客上车地点时间是否与车票对应 显示不同时刻不同车次分别的用户以及行驶的节点位置
以下是使用C++编写的代码示例:
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class HighSpeedRail {
- private:
- string trainNumber;
- int capacity;
- vector<string> routeNodes;
- vector<int> arrivalTimes;
- vector<int> stopTimes;
- vector<int> seatAvailability;
- public:
- HighSpeedRail(string number, int cap, vector<string> nodes, vector<int> arrival, vector<int> stop)
- : trainNumber(number), capacity(cap), routeNodes(nodes), arrivalTimes(arrival), stopTimes(stop) {
- seatAvailability.resize(routeNodes.size(), 0);
- }
- string getTrainNumber() {
- return trainNumber;
- }
- int getCapacity() {
- return capacity;
- }
- vector<string> getRouteNodes() {
- return routeNodes;
- }
- vector<int> getArrivalTimes() {
- return arrivalTimes;
- }
- vector<int> getStopTimes() {
- return stopTimes;
- }
- bool isSeatAvailable(int nodeIndex) {
- return seatAvailability[nodeIndex] == 0;
- }
- void bookSeat(int nodeIndex) {
- seatAvailability[nodeIndex] = 1;
- }
- };
- class Passenger {
- private:
- string passengerID;
- string ticket;
- string name;
- string IDNumber;
- string contact;
- public:
- Passenger(string id, string t, string n, string idNum, string con)
- : passengerID(id), ticket(t), name(n), IDNumber(idNum), contact(con) {}
- string getPassengerID() {
- return passengerID;
- }
- string getTicket() {
- return ticket;
- }
- string getName() {
- return name;
- }
- string getIDNumber() {
- return IDNumber;
- }
- string getContact() {
- return contact;
- }
- };
- class Ticket {
- private:
- string ticketNumber;
- string trainNumber;
- int boardingNodeTime;
- int alightingNodeTime;
- string seatNumber;
- Passenger* passenger;
- public:
- Ticket(string number, string trainNum, int boardingTime, int alightingTime, string seatNum, Passenger* pass)
- : ticketNumber(number), trainNumber(trainNum), boardingNodeTime(boardingTime),
- alightingNodeTime(alightingTime), seatNumber(seatNum), passenger(pass) {}
- string getTicketNumber() {
- return ticketNumber;
- }
- string getTrainNumber() {
- return trainNumber;
- }
- int getBoardingNodeTime() {
- return boardingNodeTime;
- }
- int getAlightingNodeTime() {
- return alightingNodeTime;
- }
- string getSeatNumber() {
- return seatNumber;
- }
- Passenger* getPassenger() {
- return passenger;
- }
- };
- class Seat {
- private:
- string trainNumber;
- int nodeTime;
- Passenger* passenger;
- public:
- Seat(string trainNum, int time, Passenger* pass)
- : trainNumber(trainNum), nodeTime(time), passenger(pass) {}
- string getTrainNumber() {
- return trainNumber;
- }
- int getNodeTime() {
- return nodeTime;
- }
- Passenger* getPassenger() {
- return passenger;
- }
- };
- int main() {
- // Create HighSpeedRail objects
- HighSpeedRail train1("G123", 100, {"Beijing", "Shanghai", "Guangzhou"},
- {0, 2, 4}, {1, 2, 3});
- HighSpeedRail train2("D456", 80, {"Shanghai", "Beijing", "Xian"},
- {0, 2, 4}, {1, 2, 3});
- // Create Passenger objects
- Passenger passenger1("P001", "T001", "John", "1234567890", "123-456-7890");
- Passenger passenger2("P002", "T002", "Alice", "0987654321", "987-654-3210");
- // Create Ticket objects
- Ticket ticket1("T001", "G123", 0, 2, "A01", &passenger1);
- Ticket ticket2("T002", "D456", 0, 2, "B02", &passenger2);
- // Create Seat objects
- Seat seat1("G123", 2, &passenger1);
- Seat seat2("D456", 2, &passenger2);
- // Display information
- cout << "Train: " << train1.getTrainNumber() << endl;
- cout << "Route Nodes: ";
- for (string node : train1.getRouteNodes()) {
- cout << node << " ";
- }
- cout << endl;
- cout << "Train: " << train2.getTrainNumber() << endl;
- cout << "Route Nodes: ";
- for (string node : train2.getRouteNodes()) {
- cout << node << " ";
- }
- cout << endl;
- cout << "Passenger: " << passenger1.getName() << endl;
- cout << "Ticket: " << passenger1.getTicket() << endl;
- cout << "Passenger: " << passenger2.getName() << endl;
- cout << "Ticket: " << passenger2.getTicket() << endl;
- cout << "Seat: " << seat1.getTrainNumber() << " - " << seat1.getNodeTime() << endl;
- cout << "Seat: " << seat2.getTrainNumber() << " - " << seat2.getNodeTime() << endl;
- return 0;
- }
复制代码 这段代码定义了高铁类( HighSpeedRail ),乘客类( Passenger ),车票类( Ticket ),以及座位类( Seat )。每个类都有相应的成员变量和方法来存储和访问相关信息。 在主函数中,我们创建了两个高铁对象( train1 和 train2 ),两个乘客对象( passenger1 和 passenger2 ),两个车票对象( ticket1 和 ticket2 ),以及两个座位对象( seat1 和 seat2 )。然后,我们使用相应的成员函数来获取和显示对象的信息。 请注意,这只是一个简单的示例,仅用于说明如何定义和使用这些类。根据实际需求,你可能需要添加更多的成员变量和方法,并进行适当的扩展和修改。
|
|