用C加加写一个火车票售票系统
时间: 2023-08-24 07:07:16 浏览: 152
C语言编写火车售票系统
4星 · 用户满意度95%
以下是一个简单的火车票售票系统的实现,包括车票信息的输入和查询功能:
```cpp
#include <iostream>
#include <string>
using namespace std;
struct Ticket {
string trainNumber; // 车次
string startStation; // 起点站
string endStation; // 终点站
string departureTime; // 出发时间
int seatCount; // 座位数
int soldCount; // 已售座位数
};
int main() {
Ticket tickets[100]; // 最多存储100个车票信息
int ticketCount = 0; // 当前车票数量
int choice; // 用户选择的操作
while (true) {
cout << "请选择操作:\n";
cout << "1. 添加车票信息\n";
cout << "2. 查询车票信息\n";
cout << "3. 退出程序\n";
cout << "请选择:";
cin >> choice;
if (choice == 1) { // 添加车票信息
if (ticketCount >= 100) {
cout << "车票信息已满,无法继续添加!\n";
} else {
Ticket ticket;
cout << "请输入车次:";
cin >> ticket.trainNumber;
cout << "请输入起点站:";
cin >> ticket.startStation;
cout << "请输入终点站:";
cin >> ticket.endStation;
cout << "请输入出发时间:";
cin >> ticket.departureTime;
cout << "请输入座位数:";
cin >> ticket.seatCount;
ticket.soldCount = 0;
tickets[ticketCount] = ticket;
ticketCount++;
cout << "车票信息添加成功!\n";
}
} else if (choice == 2) { // 查询车票信息
string trainNumber;
cout << "请输入要查询的车次:";
cin >> trainNumber;
bool found = false;
for (int i = 0; i < ticketCount; i++) {
if (tickets[i].trainNumber == trainNumber) {
cout << "车次:" << tickets[i].trainNumber << endl;
cout << "起点站:" << tickets[i].startStation << endl;
cout << "终点站:" << tickets[i].endStation << endl;
cout << "出发时间:" << tickets[i].departureTime << endl;
cout << "座位数:" << tickets[i].seatCount << endl;
cout << "已售座位数:" << tickets[i].soldCount << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到车次为" << trainNumber << "的车票信息!\n";
}
} else if (choice == 3) { // 退出程序
cout << "程序已退出!\n";
break;
} else {
cout << "无效的选择,请重新输入!\n";
}
}
return 0;
}
```
以上代码仅作为示例,实际的火车票售票系统需要考虑更多的功能和细节,如票价计算、座位选择、支付等功能。
阅读全文