用c++书写程序车票管理系统:编程实现车票管理系统车票信息包括车次号、 发车时间、起始站、终点站、行车时间、载客量、已售票等如:班次号 发车时间 起始站 终点站 行车时间 载客量 已售票1 800 郫县 广汉 2 45 302 630 郫县 成都 05 40 403 700 郫县 成都 05 40 204 1000 郫县 成都 05 40 2 具体实现的管理功能如下:1录入车次信息文本文
以下是一个简单的用C++编写的车票管理系统的示例代码:
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_TICKETS = 1000;
class Ticket {
private:
int ticketNumber;
string departureTime;
string startStation;
string endStation;
double travelTime;
int passengerCapacity;
int soldTickets;
public:
Ticket() {}
Ticket(int num, string time, string start, string end, double travel, int capacity, int sold) {
ticketNumber = num;
departureTime = time;
startStation = start;
endStation = end;
travelTime = travel;
passengerCapacity = capacity;
soldTickets = sold;
}
int getTicketNumber() { return ticketNumber; }
string getDepartureTime() { return departureTime; }
string getStartStation() { return startStation; }
string getEndStation() { return endStation; }
double getTravelTime() { return travelTime; }
int getPassengerCapacity() { return passengerCapacity; }
int getSoldTickets() { return soldTickets; }
void setSoldTickets(int sold) { soldTickets = sold; }
};
class TicketManagementSystem {
private:
Ticket tickets[MAX_TICKETS];
int numTickets;
public:
TicketManagementSystem() {
numTickets = 0;
}
void readTicketsFromFile() {
ifstream inputFile("tickets.txt");
if (inputFile.is_open()) {
int num;
string time, start, end;
double travel;
int capacity, sold;
while (inputFile >> num >> time >> start >> end >> travel >> capacity >> sold) {
Ticket ticket(num, time, start, end, travel, capacity, sold);
tickets[numTickets++] = ticket;
}
inputFile.close();
}
}
void writeTicketsToFile() {
ofstream outputFile("tickets.txt");
if (outputFile.is_open()) {
for (int i = 0; i < numTickets; i++) {
outputFile << tickets[i].getTicketNumber() << " "
<< tickets[i].getDepartureTime() << " "
<< tickets[i].getStartStation() << " "
<< tickets[i].getEndStation() << " "
<< tickets[i].getTravelTime() << " "
<< tickets[i].getPassengerCapacity() << " "
<< tickets[i].getSoldTickets() << endl;
}
outputFile.close();
}
}
void addTicket() {
int num;
string time, start, end;
double travel;
int capacity, sold;
cout << "Enter ticket number: ";
cin >> num;
cout << "Enter departure time: ";
cin >> time;
cout << "Enter start station: ";
cin >> start;
cout << "Enter end station: ";
cin >> end;
cout << "Enter travel time: ";
cin >> travel;
cout << "Enter passenger capacity: ";
cin >> capacity;
cout << "Enter number of sold tickets: ";
cin >> sold;
Ticket ticket(num, time, start, end, travel, capacity, sold);
tickets[numTickets++] = ticket;
cout << "Ticket added successfully!" << endl;
}
void browseTickets() {
time_t now = time(0);
tm* currentTime = localtime(&now);
for (int i = 0; i < numTickets; i++) {
cout << "Ticket Number: " << tickets[i].getTicketNumber() << endl;
cout << "Departure Time: " << tickets[i].getDepartureTime() << endl;
cout << "Start Station: " << tickets[i].getStartStation() << endl;
cout << "End Station: " << tickets[i].getEndStation() << endl;
cout << "Travel Time: " << tickets[i].getTravelTime() << endl;
cout << "Passenger Capacity: " << tickets[i].getPassengerCapacity() << endl;
cout << "Sold Tickets: " << tickets[i].getSoldTickets() << endl;
if (currentTime->tm_hour > stoi(tickets[i].getDepartureTime().substr(0, 2))) {
cout << "This ticket has already departed." << endl;
}
cout << "-----------------------" << endl;
}
}
void queryTicketByNumber() {
int num;
cout << "Enter ticket number to query: ";
cin >> num;
for (int i = 0; i < numTickets; i++) {
if (tickets[i].getTicketNumber() == num) {
cout << "Ticket Number: " << tickets[i].getTicketNumber() << endl;
cout << "Departure Time: " << tickets[i].getDepartureTime() << endl;
cout << "Start Station: " << tickets[i].getStartStation() << endl;
cout << "End Station: " << tickets[i].getEndStation() << endl;
cout << "Travel Time: " << tickets[i].getTravelTime() << endl;
cout << "Passenger Capacity: " << tickets[i].getPassengerCapacity() << endl;
cout << "Sold Tickets: " << tickets[i].getSoldTickets() << endl;
cout << "-----------------------" << endl;
return;
}
}
cout << "Ticket not found." << endl;
}
void queryTicketByEndStation() {
string station;
cout << "Enter end station to query: ";
cin >> station;
for (int i = 0; i < numTickets; i++) {
if (tickets[i].getEndStation() == station) {
cout << "Ticket Number: " << tickets[i].getTicketNumber() << endl;
cout << "Departure Time: " << tickets[i].getDepartureTime() << endl;
cout << "Start Station: " << tickets[i].getStartStation() << endl;
cout << "End Station: " << tickets[i].getEndStation() << endl;
cout << "Travel Time: " << tickets[i].getTravelTime() << endl;
cout << "Passenger Capacity: " << tickets[i].getPassengerCapacity() << endl;
cout << "Sold Tickets: " << tickets[i].getSoldTickets() << endl;
cout << "-----------------------" << endl;
}
}
}
void sellTicket() {
int num;
cout << "Enter ticket number to sell: ";
cin >> num;
for (int i = 0; i < numTickets; i++) {
if (tickets[i].getTicketNumber() == num) {
if (tickets[i].getSoldTickets() < tickets[i].getPassengerCapacity() &&
stoi(tickets[i].getDepartureTime().substr(0, 2)) > time(0) / 3600) {
tickets[i].setSoldTickets(tickets[i].getSoldTickets() + 1);
writeTicketsToFile();
cout << "Ticket sold successfully!" << endl;
} else {
cout << "Ticket cannot be sold." << endl;
}
return;
}
}
cout << "Ticket not found." << endl;
}
void returnTicket() {
int num;
cout << "Enter ticket number to return: ";
cin >> num;
for (int i = 0; i < numTickets; i++) {
if (tickets[i].getTicketNumber() == num) {
if (stoi(tickets[i].getDepartureTime().substr(0, 2)) > time(0) / 3600) {
tickets[i].setSoldTickets(tickets[i].getSoldTickets() - 1);
writeTicketsToFile();
cout << "Ticket returned successfully!" << endl;
} else {
cout << "Ticket cannot be returned." << endl;
}
return;
}
}
cout << "Ticket not found." << endl;
}
};
int main() {
TicketManagementSystem tms;
tms.readTicketsFromFile();
int choice;
while (true) {
cout << "~~~~~欢迎使用车票管理系统!~~~~~" << endl;
cout << "=============================" << endl;
cout << "1. 录入车票信息" << endl;
cout << "2. 浏览车票信息" << endl;
cout << "3. 查询车票信息" << endl;
cout << "4. 售票" << endl;
cout << "5. 退票" << endl;
cout << "6. 退出系统" << endl;
cout << "请选择:";
cin >> choice;
cout << endl;
switch (choice) {
case 1:
tms.addTicket();
break;
case 2:
tms.browseTickets();
break;
case 3:
int queryChoice;
cout << "1. 按车次号查询" << endl;
cout << "2. 按终点站查询" << endl;
cout << "请选择查询方式:";
cin >> queryChoice;
cout << endl;
if (queryChoice == 1) {
tms.queryTicketByNumber();
} else if (queryChoice == 2) {
tms.queryTicketByEndStation();
} else {
cout << "Invalid choice." << endl;
}
break;
case 4:
tms.sellTicket();
break;
case 5:
tms.returnTicket();
break;
case 6:
tms.writeTicketsToFile();
cout << "退出系统成功!" << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
cout << endl;
}
return 0;
}
希望对你有所帮助
原文地址: https://www.cveoy.top/t/topic/iEHm 著作权归作者所有。请勿转载和采集!