火车订票系统---课程设计(C语言简易版)

目录

        流程图: 

        源代码:

        代码运行视频见相关上传视频资源:live.csdn.net/v/307500 

         火车订票系统(简易版)课程设计可参考(耗时一下午,相对简单易懂),话不多说直接上思路以及源代码!!!(代码较长, 可通过目录跳转)

        流程图: 

 

 

        源代码:

         

#include <iostream> 
#include <fstream>   //文件操作函数
#include <string.h>  //引入字符串类型
#include <time.h>   //时间函数
#include <windows.h>  //调用系统函数的头文件
using namespace std;
void welcome();                          //主菜单函数
//子函数
void banciluru();                        //班次录入子函数
void liulanbancixinxi();                 //浏览班次信息子函数
void shoupiao();                         //售票子函数
void tuipiao();                          //退票子函数
void chaxun();                           //查询子函数
int loading();                           //文件导入子函数
void adminLogin();						 //管理员登录判断函数
void userLogin();                        //用户登录判断函数
void userRegister();                     //用户注册函数
void adminMenu();                        //管理员菜单函数
void userMenu();                         //用户菜单函数
void userChoose();                       //用户选择函数
void adminChoose();                      //管理员选择函数
void Numbersearch();                     //按班次查找函数
void Finalsearch();                      //按终点站查找函数
int userloading();                       //文件导入用户信息并返回用户人数
struct ticket {           //车票
	string Number;	      //车次
	char Qidian[20];      //起点站				
	char Zhongdian[20];   //终点站
	string Lasttime;      //行车时间
	int Fixnumber;        //额定载客量
	int Fixednumber;      //已订票人数
}car[50];
struct User {             //用户
	string name;          //用户名
	string password;      //用户密码
}user[100];
//主函数
int main() {
	welcome();    //调用主菜单函数
	return 0;
}
//欢迎子函数
void welcome() {
	system("title 火车订票系统");         //设置cmd窗口标题(可省略)
	//system("color 0A");  改变文字颜色及背景颜色
	cout << "_____________________________________________________________" << endl;
	cout << "| 欢 * 迎 * 使 * 用 * 火 * 车 * 订 * 票 * 管 * 理 * 系 * 统 |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|                    本订票系统有以下功能:                  |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|         1 管理员登入    2 用户登录    3 用户注册          |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|___________________________________________________________|" << endl;
	cout << "请选择身份:";
	string choice;       //设置为字符串类型方便异常处理
	cin >> choice;
	if (choice == "1") {
		adminLogin();
	}
	if (choice == "2") {
		userLogin();
	}
	if (choice == "3") {
		userRegister();
	}
	try {  //异常处理
		if (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "5") {
			throw runtime_error("");    //抛出异常
		}
	}
	catch (runtime_error) {     // 捕获异常,并输出错误信息
		cout << "错误信息:输入错误!正在重新进入系统......" << endl;
		Sleep(1000);    //休眠效果
	}
	int choose;   //判断是否退出
	cout << "是否退出本系统(1代表是,0代表否),请输入:";
	cin >> choose;
	if (choose == 0) {
		system("cls"); //清屏效果
		welcome();     //自调用
	}
	else {
		cout << "已退出,欢迎下次使用本系统,谢谢!!!" << endl;
	}
}


void adminLogin() {   //用户登录判断函数
	string name;
	string pwd;
	cout << "请输入用户名:";
	cin >> name;
	cout << "请输入用户密码:";
	cin >> pwd;
	int i, n = userloading();   //调用文件导入函数并返回已录入信息的用户数量,便于加快以下循环
	int loop = 0;      //判断用户输入信息是否存在
	for (i = 0; i < n; i++) {   
		if (user[i].name == name && user[i].password == pwd) {
			loop = 1;    //存在
			break;
		}
	}
	if (loop == 1) {
		adminMenu();
	}
	else {
		cout << "用户名或密码错误" << endl;
		adminLogin();
	}
}
void adminMenu() {    //管理员菜单函数
	system("cls");  //清屏
	cout << "_____________________________________________________________" << endl;
	cout << "| 欢 * 迎 * 使 * 用 * 火 * 车 * 订 * 票 * 管 * 理 * 系 * 统 |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|                  本管理系统有以下功能:                    |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|     1 班次录入      2 浏览班次信息    3 查询火车信息      |" << endl;
	cout << "|     4 购买火车票    5 退订火车票      6 退出系统          |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|___________________________________________________________|" << endl;
	cout << "请输入所要执行功能前的数字: ";
	loading();   //文件导入航班信息
	adminChoose();
}
void adminChoose() {   //管理员选择函数
	string i;
	cin >> i;  
	if (i == "1")
		banciluru();
	else if (i == "2")
		liulanbancixinxi();
	else if (i == "3")
		chaxun();
	else if (i == "4")
		shoupiao();
	else if (i == "5")
		tuipiao();
	else if (i == "6")
		cout<<endl;
	else {
		cout << "输入有误!请在数字1-6之间重新您的输入选择!" << endl;
		adminChoose();
	}
}
void banciluru() {
	int t = 1;        //浏览班次子函数
	int i = loading();
	ofstream outfile("航班.txt",ios::out|ios::app);   //打开文件覆盖写模式
	for (; t == 1; i++) {
		cout << "请输入需要添加的火车班次:" << endl;
		cin >> car[i].Number;
		cout << "请输入出发站:" << endl;
		cin >> car[i].Qidian;
		cout << "请输入终点站:" << endl;
		cin >> car[i].Zhongdian;
		cout << "请输入行车时间:" << endl;
		cin >> car[i].Lasttime;
		cout << "请输入额定载客量:" << endl;
		cin >> car[i].Fixnumber;
		cout << "请输入已订票人数:" << endl;
		cin >> car[i].Fixednumber;
		outfile << '\n'<<car[i].Number << " " << car[i].Qidian << " " << car[i].Zhongdian << " " << car[i].Lasttime << " " << car[i].Fixnumber << " " << car[i].Fixednumber << endl;
		cout << "是否继续录入?(是请输1,否请输任意键返回主菜单)" << endl;
		cin >> t;
		if (t != 1) {
			outfile.close();
			adminMenu();
		}
	}
}
void chaxun() {
	string t;
	cout << "_____________________________________________________________" << endl;
	cout << "|                请输入您想选择的查询方法:                  |" << endl;
	cout << "|             1.按班次查询         2.按终点站查询           |" << endl;
	cout << "|___________________________________________________________|" << endl;
	cin >> t;
	if (t == "1")   
		Numbersearch();
	else if (t == "2")   
		Finalsearch();
	else if (t != "1" || t != "2") {
		cout << "输入有误,请重新输入" << endl;
		chaxun();
	}
}
int loading() {
	int i;
	ifstream infile("航班.txt");   //打开文件
	for (i = 0; !infile.eof(); i++) {  //循环判断文件是否还有内容
		infile >> car[i].Number >> car[i].Qidian >> car[i].Zhongdian >> car[i].Lasttime >> car[i].Fixnumber >> car[i].Fixednumber;
	}
	infile.close();
	return i;   //返回航班数目
}
void Numbersearch() {    //班次查询子函数
	loading();
	string SearchNumber;
	cout << "请输入您需要查找的班次号:" << endl;
	cin >> SearchNumber;
	cout << "班次" << " " << "起点站" << " " << "终点站" << " " << "行车时间" << " " << "额定载客量" << " " << "已订票人数" << endl;
	for (int len = 1; len < (sizeof(car) / sizeof(car[0])); len++) {
		if (car[len - 1].Number == SearchNumber) {
			cout << car[len - 1].Number  << "     " << car[len - 1].Qidian << "   " << car[len - 1].Zhongdian << "     " << car[len - 1].Lasttime << "     " << car[len - 1].Fixnumber << "        " << car[len - 1].Fixednumber << endl;
		}
	}
}
void Finalsearch() {//终点站查询子函数
	int s;
	s = loading();
	int i;
	char Address[20];
	cout << "请输入终点站名称:" << endl;
	cin >> Address;
	cout << "班次" << " " << "起点站" << " " << "终点站" << " " << "行车时间" << " " << "额定载客量" << " " << "已订票人数" << endl;
	for (i = 0; i < s; i++) {
		if ((strcmp(Address, car[i].Zhongdian)) == 0)
			cout << car[i].Number << "    " << car[i].Qidian << "    " << car[i].Zhongdian << "     " << car[i].Lasttime << "      " << car[i].Fixnumber << "          " << car[i].Fixednumber << endl;
	}
}
void shoupiao() {
	int s;
	s = loading();
	int i = 0, k;
	int loop = 0;
	string SearchNumber;
	cout << "请输入要订购的车票的班次:";
	cin >> SearchNumber;
	for (; i < s; i++) {
		if (car[i].Number == SearchNumber) {
			loop = 1;
			k = i;
			break;
		}
	}
	if (loop == 1) {
		cout << "你的订票成功,即将打印您的购票信息......" << endl;
		Sleep(2000);
		cout << "***********************************************" << endl;
		cout << "*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*" << endl;
		cout << "*                                             *" << endl;
		cout << "班次:" << car[k].Number << endl;
		cout << "                                               " << endl;
		cout << "      " << "起点站:" << car[k].Qidian << "---->" << "终点站:" << car[k].Zhongdian << endl;
		cout << "      " << "发车时间:" << car[k].Lasttime << endl;
		cout << "*                                             *" << endl;
		cout << "*         请提前候车,祝您旅途愉快!!!      *" << endl;
		cout << "***********************************************" << endl;
	}
	else 
		cout << "对不起,今天的这趟车的票已卖完,请明天再来,谢谢合作" << endl;
}   
void tuipiao() {     //退订火车票子函数
	int s;
	s = loading();
	int i = 0;
	string SearchNumber;
	cout << "请输入要退购的车票的班次:" << endl;
	cin >> SearchNumber;
	for (int len = 1; len < (sizeof(car) / sizeof(car[0]))-1; len++) {
		if (car[len - 1].Fixednumber >= 1) {
			car[len - 1].Fixednumber -= 1;
			ofstream outfile("航班.txt");
			for (i = 0; i < s; i++) {
				outfile << car[i].Number << " " << car[i].Qidian << " " << car[i].Zhongdian << " " << car[i].Lasttime << " " << car[i].Fixnumber << " " << car[i].Fixednumber << endl;
			}
			outfile.close();
		}
	}
	cout << "退订车票成功,谢谢使用!" << endl;
}

void userLogin() {
	string username;
	string password;
	cout << "请输入用户名:";
	cin >> username;
	cout << "请输入密码:";
	cin >> password;
	int i, n = userloading(), loop = 0;
	for (i = 0; i < n; i++) {
		if (user[i].name == username && user[i].password == password) {
			loop = 1;
			break;
		}
	}
	if (loop == 1) {
		userMenu();
	}
	else {
		cout << "用户名或密码错误";
		userLogin();
	}
}
void userMenu() {
	system("cls");
	cout << "_____________________________________________________________" << endl;
	cout << "| 欢 * 迎 * 使 * 用 * 火 * 车 * 订 * 票 * 管 * 理 * 系 * 统 |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|                  本管理系统有以下功能:                    |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|             1 浏览班次信息   2 查询火车信息               |" << endl;
	cout << "|             3 购买火车票     4 退订火车票                 |" << endl;
	cout << "|             5 退出系统                                    |" << endl;
	cout << "|                                                           |" << endl;
	cout << "|___________________________________________________________|" << endl;
	cout << "请输入所要执行功能前的数字: ";
	userChoose();
}
void userChoose() {
	string i;
	cin >> i;
	if (i == "1")
		liulanbancixinxi();
	else if (i == "2")
		chaxun();
	else if (i == "3")
		shoupiao();
	else if (i == "4")
		tuipiao();
	else if (i == "5")
		cout << endl;
	else {
		cout << "输入有误!请在数字1-5之间重新您的输入选择!" << endl;
		userChoose();
	}
}
void liulanbancixinxi() {
	int c;
	c = loading();
	cout << "班次" << " " << "起点站" << " " << "终点站" << " " << "行车时间" << " " << "额定载客量" << " " << "已订票人数" << endl;
	for (int i = 0; i < c-1; i++) {
		cout << car[i].Number << "     " << car[i].Qidian << "    " << car[i].Zhongdian << "      " << car[i].Lasttime << "      " << car[i].Fixnumber << "      " << car[i].Fixednumber << endl;
	}
}  

int userloading() {
	int i = 0;
	ifstream in("user.txt");
	while (!in.eof()) {
		in >> user[i].name >> user[i].password;
		i++;
	}
	in.close();
	return i;
}
void userRegister() {
	int n = userloading();
	int k;
	int i = n;
	ofstream out("user.txt", ios::out | ios::app);
	while (1) {
		cout << "请输入新用户的用户名:";
		cin >> user[i].name;
		cout << "请输入新用户的密码:";
		cin >> user[i].password;
		out << "\n" << user[i].name << ' ' << user[i].password;
		i++;
		cout << "登记完毕!!!是否继续登记?(0/1):" ;
		cin >> k;
		if (k == 0) {
			out.close();
			break;
		}
	}
}

        代码运行视频见相关上传视频资源:live.csdn.net/v/307500 

        总的来说,本项目相对容易实现对于信手来说算一个小demo。感兴趣可以复制代码并升级精修代码,从而实现更多功能及编写更复杂的代码。[抱拳][抱拳][抱拳]

  • 2
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,下面是一个简单的C语言火车票订票系统代码示例,仅供参考。 ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> // 火车票信息结构体 typedef struct TrainTicket { int id; // 票号 char name[20]; // 乘客姓名 char from[20]; // 出发地 char to[20]; // 目的地 char date[20]; // 日期 bool isSold; // 是否售出 } TrainTicket; // 火车票数组 TrainTicket tickets[100]; // 剩余票数 int remain = 100; // 打印火车票信息 void printTicket(TrainTicket ticket) { printf("Ticket ID: %d\n", ticket.id); printf("Name: %s\n", ticket.name); printf("From: %s\n", ticket.from); printf("To: %s\n", ticket.to); printf("Date: %s\n", ticket.date); printf("Sold: %s\n", ticket.isSold ? "Yes" : "No"); } // 查找未售出的票 void findAvailableTickets() { printf("Available Tickets:\n"); for (int i = 0; i < 100; i++) { if (!tickets[i].isSold) { printTicket(tickets[i]); } } } // 购买火车票 void buyTicket() { if (remain == 0) { printf("No ticket available!\n"); return; } TrainTicket ticket; printf("Please enter your name: "); scanf("%s", ticket.name); printf("Please enter the from station: "); scanf("%s", ticket.from); printf("Please enter the to station: "); scanf("%s", ticket.to); printf("Please enter the date (MM/DD/YYYY): "); scanf("%s", ticket.date); ticket.id = 100 - remain + 1; ticket.isSold = true; tickets[100 - remain] = ticket; remain--; printf("Ticket purchased successfully!\n"); } int main() { int choice; while (true) { printf("Please select an option:\n"); printf("1. Find available tickets\n"); printf("2. Buy a ticket\n"); printf("3. Exit\n"); scanf("%d", &choice); switch (choice) { case 1: findAvailableTickets(); break; case 2: buyTicket(); break; case 3: exit(0); default: printf("Invalid choice!\n"); } } return 0; } ``` 这个代码示例实现了一个简单的火车票订票系统,包括查找未售出的票和购买火车票两个功能。当用户购买票时,系统会要求输入乘客姓名、出发地、目的地和日期,然后生成一个唯一的票号,并将票信息存储到票数组中,并将剩余票数减1。当用户查找未售出的票时,系统会遍历票数组,查找未售出的票,并打印输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想我记得写信

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值