第八周
本周在学校进行c++训练;
练习车票管理系统以及银行个人账户管理系统
以下为一些代码:
点击查看代码
#ifndef PERSON_H
#define PERSON_H
#include <string>
using namespace std;
class Car {
public:
int hour;//发车时间
int minute;//发车时间
string start_station;//起始站
string end_station;//终点站
double runtime;//行驶时间
int busload;//载客量
int ys;//售票数
};
class Sys {
private:
Car car;
public:
void input();//录入
void output();//浏览
void search_train();//按车次查询
void search_station();//按终点站查询
void sp();//售票
void tp();//退票
void display(int a);//显示a车次信息
friend void save();//保存数据
friend void read();//读取文件数据
};
#endif
点击查看代码
#include "CarSys.h"
#include <iostream>
#include<fstream>
#include<iomanip>
#include<ctime>
using namespace std;
time_t nowtime;//获取当前时间秒数
static int n = 0;
Sys ticket[100];
//存储车次信息到.txt中
void save() {
ofstream file("数据.txt", ios_base::out);
int i;
for (i = 0; i < n; i++) {
file << i + 1 << " ";
file << ticket[i].car.hour << " ";
file << ticket[i].car.minute << " ";
file << ticket[i].car.start_station << " ";
file << ticket[i].car.end_station << " ";
file << ticket[i].car.runtime << " ";
file << ticket[i].car.busload << " ";
file << ticket[i].car.ys << endl;
}
file.close();
cout << "信息存入文件中" << endl;
}
//录入
void Sys::input() {
cin >> ticket[n].car.hour >> ticket[n].car.minute;
cin >> ticket[n].car.start_station >> ticket[n].car.end_station;
cin >> ticket[n].car.runtime >> ticket[n].car.busload >> ticket[n].car.ys;
n++;
//存入文件
save();
}
//浏览车次信息
void Sys::output() {
time(&nowtime);
tm p;//转化为本地时间的tm结构
localtime_s(&p, &nowtime);
cout << "当前时间:" << p.tm_hour << ":" << p.tm_min << endl;
cout << "车次信息:" << endl;
cout << "班次号\t" << "发车时间 " << "起始站 " << "终点站 " << "行车时间 " << "载客量 " << "售票数 " << "状态"<<endl;
for (int j = 0; j < n; j++) {
ticket[j].display(j + 1);
cout << " ";
//判断是否发车
if (ticket[j].car.hour < p.tm_hour) {
cout << "此班已出发" << endl;
}
else {
if (ticket[j].car.minute <= p.tm_min && ticket[j].car.hour == p.tm_hour) {
cout << "此班已出发" << endl;
}
else {
cout << "此班未出发" << endl;
}
}
}
}
//显示a车次信息
void Sys::display(int a) {
cout << a << "\t" << car.hour << ":";
if (car.minute == 0) {
cout << "00";
}
else {
cout << car.minute;
}
cout << "\t" << " " << car.start_station << " " << car.end_station << " " << car.runtime <<"\t" << " " << car.busload << " " << car.ys;
}
//按车次查询
void Sys::search_train() {
int s;
cout << "输入要查询的车次:";
cin >> s;
cout << " 班次号\t" << "发车时间 " << "起始站 " << "终点站 " << "行车时间 " << "载客量 " << "售票数 " << endl;
cout << s << "\t" << ticket[s - 1].car.hour << ":" << ticket[s - 1].car.minute << "\t" << ticket[s - 1].car.start_station << "\t" << ticket[s - 1].car.end_station << "\t" << ticket[s - 1].car.runtime << "\t" << ticket[s - 1].car.busload << "\t" << ticket[s - 1].car.ys << endl;
}
//按终点查询
void Sys::search_station() {
string s;
cout << "输入要查询的终点站:";
cin >> s;
cout << " 班次号\t" << "发车时间 " << "起始站 " << "终点站 " << "行车时间 " << "载客量 " << "售票数 " << endl;
for (int j = 0; j < n; j++) {
if (s==ticket[j].car.end_station) {
cout << j+1 << "\t" << ticket[j].car.hour << ":" << ticket[j].car.minute << "\t" << ticket[j].car.start_station << "\t" << ticket[j].car.end_station << "\t" << ticket[j].car.runtime << "\t" << ticket[j].car.busload << "\t" << ticket[j].car.ys << endl;
}
}
}
//读取存储数据
void read() {
ifstream file("数据.txt", ios_base::in);
while (!file.eof()) {
file >> ticket[n].car.hour;
file >> ticket[n].car.minute;
file >> ticket[n].car.start_station;
file >> ticket[n].car.end_station;
file >> ticket[n].car.runtime;
file >> ticket[n].car.busload;
file >> ticket[n].car.ys;
n++;
}
file.close();
}
//售票
void Sys::sp() {
int a;
cout << "输入售票班次号:";
cin >> a;
time(&nowtime);
tm p;//转化为本地时间的tm结构
localtime_s(&p, &nowtime);
int flag = 0;
if (ticket[a - 1].car.ys < ticket[a - 1].car.busload) {
if (ticket[a - 1].car.hour > p.tm_hour) {
flag = 1;
}
}
else {
if (ticket[a - 1].car.hour == p.tm_hour && ticket[a - 1].car.minute > p.tm_min) {
flag = 1;
}
}
if (flag == 1) {
cout << "售票成功" << endl;
ticket[a - 1].car.ys++;
}
else {
cout << "票已售完" << endl;
}
}
//退票
void Sys::tp() {
int a;
cout << "退票车次:";
cin >> a;
time(&nowtime);
tm p;//转化为本地时间的tm结构
localtime_s(&p, &nowtime);
int flag = 0;
if (ticket[a - 1].car.hour > p.tm_hour) {
flag = 1;
}
else {
if (ticket[a - 1].car.hour == p.tm_hour && ticket[a - 1].car.minute > p.tm_min) {
flag = 1;
}
}
if (flag == 1) {
cout << "退票成功" << endl;
ticket[a - 1].car.ys--;
}
else {
cout << "退票失败" << endl;
}
}
点击查看代码
#include "CarSys.h"
#include <iostream>
#include <ctime>
void menu() {
cout << "~~~~~~~~~~~~~~~~欢迎使用车票管理系统!~~~~~~~~~~~~~~~~" << endl;
cout << " =========================" << endl;
cout << " 1.录入车票信息 " << endl;
cout << " 2.浏览车票信息 " << endl;
cout << " 3.查询车票信息 " << endl;
cout << " 4.售票 " << endl;
cout << " 5.退票 " << endl;
cout << " 6.退出系统 " << endl;
}
int main() {
menu();
Sys p;
int m;
int f;
int flag = 1;
while (flag) {
cout << "\n请选择:";
cin >> m;
switch (m)
{
case 1:p.input(); break;//录入车票信息
case 2:p.output(); break;//浏览车票信息
case 4:p.sp(); break;//售票
case 5:p.tp(); break;//退票
case 6:flag=0; break;
case 3:
cout << "查询方式(按车次查询输入1,按终点站查询输入2:";
cin >> f;
if (f == 1) {
p.search_train();
}
if (f == 2) {
p.search_station();
}
}
}
return 0;
}