今年春运首日火车票12月30日正式开售。每年春运,不少乘客为购得一张回家车票,“八仙过海、各显神通”,然而,总有部分乘客“神通失灵”。今年春运火车票如何安心购票?不如自己写一个火车订票系统吧。
这是好久之前写的一个火车售票系统, 写的非常粗糙, 后来也没改了, 希望遇见有缘人继续优化吧。
主要的功能是:设置车次,删除车次, 买票, 改签, 退票, 查询等。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<conio.h>
#define MAXNUM 10000
#define Num 100
typedef struct train
{
int Train_num;//火车编号
char Train_end[10];//终点站
int Train_price;//票价
int leftnum;//余票数量
}Train_Ticket;
typedef struct person{
int mark;//标记
int snum;//身份号码
char sex[10];//买票人性别
//char Train_start[8];
char name[10];//买票人姓名
}Person;
typedef struct node
{
Train_Ticket data[MAXNUM];
int len;
}SeqList;
Person pa1[Num],pa2[Num];
void init_Seqlist(SeqList*);
void Sale(SeqList*,FILE*);
void Withdraw(FILE*);
void Inquiry(SeqList*);
void Select(SeqList*,FILE*);
void Delete(SeqList*);
void Init(SeqList*,struct train t);
void Input(SeqList*);
void Exchange(SeqList*,FILE *fp);
int main()
{
SeqList*q;
FILE *fp;
fp=fopen("train.dat","wb");
q=(SeqList*)malloc(sizeof(SeqList));
int sel;
Input(q);
system("cls");
system("color 3f");//改变颜色
system("cls");
while(1){
printf("------------------------------------- ");
printf("-----Welcome to YaiTai trainstation-- ");
printf("1-- Set Tickets --");//初始化
printf(" 2-- Book Ticket --");//买票
printf(" 3-- Exchange Ticket --");//改签
printf(" 4-- Withdraw Ticket --");//退票
printf(" 5-- Delete Ticket --");//删除车次
printf(" 6-- Select Ticket --");//查询
printf(" 7-- Inquiry --");//
printf(" 8-- Exit --");//退出
printf(" -------------------------------------");
printf(" Please input your choice:");
scanf("%d",&sel);
getchar();
switch(sel)
{
case 1:
init_Seqlist(q); break;
case 2:
if(q->len==0)
{
printf("------------------ ");
printf(" No tickets! ");
printf("------------------ ");break;
}else{
Sale(q,fp); break;
}
case 3:
Exchange(q,fp); break;
case 4:
Withdraw(fp);break;
case 5:
Delete(q); break;
case 6:
Select(q,fp); break;
case 7:
Inquiry(q);break;
case 8:
printf("------------------------------ ");
printf("---Thank you for your use!--- ");
printf("------------------------------ ");
exit(1);
default : printf("Enter error!! ");
}
printf("请按ENTER返回主菜单 ");
getchar();
}
return 0;
}
void Input(SeqList*L)
{
int i;
struct train tra[5]={{206,"beijing",200,20},
{301,"chengdu",350,20},
{432,"shangha",400,20},
{783,"qingdao",175,20},
{312,"hangzho",480,20}
};
for(i=0;i<5;i++)
Init(L,tra[i]);
}
void Init(SeqList*L,struct train t)
{
int i=L->len;
L->data[i].Train_num=t.Train_num;
strcpy(L->data[i].Train_end,t.Train_end);
L->data[i].Train_price=t.Train_price;
L->data[i].leftnum=t.leftnum;
L->len++;
}
void init_Seqlist(SeqList*L)
{
int i,d,n;
d=L->len;
printf("-------------车次设置------------- ");
printf("请输入你想设置的车次数量: ");
scanf("%d",&n);
for(i=d;i<d+n;i++){
printf("请输入车次编号:");
scanf("%d",&L->data[i].Train_num);
printf("请输入车次终点站:");
scanf("%s",&L->data[i].Train_end);
printf("请输入设置的票价:");
scanf("%d",&L->data[i].Train_price);
printf("请输入余票的数量:");
scanf("%d",&L->data[i].leftnum);
L->len++;
if((L->len)>1&&i<(L->len-1))
{
printf("请输入下班车次信息 ");
}
}
printf("Initiation finish! ");
}
void Sale(SeqList*L,FILE *fp)
{
int i=0,k,a,j;
if((fp=fopen("train.dat","ab+"))==NULL)
{
printf("Cannot open file! ");
exit(1);
}
printf("------------买票服务------------ ");
printf("请输入你想购置车票数: ");
scanf("%d",&a);
if(a>1){
printf("请分别输入%d名乘客信息 ",a);
}
else if(a<=1)
{
printf("请输入该名乘客信息 ");
}
for(i=0;i<a;i++)
{
printf("请先输入你的姓名:");
scanf("%s",&pa1[i].name);
printf("请选择性别<男>F<女>M:");
scanf("%s",&pa1[i].sex);
do{
if(strcmp(pa1[i].sex,"F")!=0 && strcmp(pa1[i].sex,"M")!=0){
printf("性别输入错误!请重新输入! ");
scanf("%s",&pa1[i].sex);
}
}while(strcmp(pa1[i].sex,"F")!=0 && strcmp(pa1[i].sex,"M")!=0);
printf("请先输入你的身份证号码:");
scanf("%d",&pa1[i].snum);
}
Inquiry(L);
for(j=0;j<a;j++){
printf(" Please input the Train_number you want to book(1~%d):");
scanf("%d",&k);
char answer[10];
for(i=0;i<L->len;i++){
if(k==L->data[i].Train_num){
pa1[j].mark=i;//标记点位
printf("确定选择购买 %d 车次的列车吗(yes or no) ",L->data[i].Train_num);
scanf("%s",&answer);
do
{
if(strcmp(answer,"yes")!=0){
printf("你的输入有误,请重新输入! ");
scanf("%s",&answer);
}
}while(strcmp(answer,"yes")!=0);
if(strcmp(answer,"yes")==0){
printf("你已支付该车票的价格%d元 ",L->data[i].Train_price);
L->data[i].leftnum--;
}else if(strcmp(answer,"no")==0){
printf("请重新选择你要购买的车次编号 ");
}
}
}
printf("Tickets purchased success! ");
}
for(i=0;i<a;i++)
//printf("%d ",pa1[i].mark);
fwrite((void*)pa1,sizeof(Person),a,fp);
fclose(fp);
}
void Delete(SeqList*L)
{
int n,i,true=0;
SeqList*q;
q=(SeqList*)malloc(sizeof(SeqList));
printf(" --------------按照车次编号删除车次------------- ");
printf("请输入要删除的车次编号 ");
scanf("%d",&n);
for(i=0;i<L->len;i++){
if(L->data[i].Train_num==n)
{
true=1;
if(i<L->len-1)
{
L->data[i].Train_num=L->data[i+1].Train_num;
L->data[i].Train_price=L->data[i+1].Train_price;
L->data[i].leftnum=L->data[i+1].leftnum;
strcpy(L->data[i].Train_end,L->data[i+1].Train_end);L->len--;
printf("-------------------- ");
printf("--已删除该车次信息-- ");
printf("-------------------- ");
}
if(i==L->len-1)
{
L->len--;
printf("-------------------- ");
printf("--已删除该车次信息-- ");
printf("-------------------- ");
}
}
}
if(true==0)
{
printf("No this ticket numbers! ");
}
}
void Select(SeqList*L,FILE *fp)
{
SeqList*q;
q=(SeqList*)malloc(sizeof(SeqList));
int i,a,b,true=0;
printf("-------------------查询服务------------- ");
printf("---------------------------------------- ");
printf("1.按乘客的名称查询 2.按乘客证件号查询 ");
printf("--------------------------------------- ");
if((fp=fopen("train.dat","rb+"))==NULL)
{
printf("Canot open file ! ");
}
fseek(fp,0L,SEEK_END);
int d=ftell(fp)/sizeof(Person);
rewind(fp);
fread((void*)&pa2,sizeof(Person),d,fp);
scanf("%d",&a);
switch(a)
{
case 1:
{
char na[20];
printf("请输入该乘客的姓名: ");
scanf("%s",&na);
for(i=0;i<d;i++)
{
if(strcmp(pa2[i].name,na)==0){
b=pa2[i].mark;
true=1;
printf("-------------%s的车次信息------- ",pa2[i].name);
printf("始发站:Yantai 终点站:%s ",L->data[b].Train_end);
printf("第%d号列车 票价: %d ",L->data[b].Train_num,L->data[b].Train_price);
printf("乘客姓名:%s ",pa2[i].name);
printf("乘客证件信息:%d ",pa2[i].snum);
printf("--------------------------------- ");
}
}
}
break;
case 2:
{
int w;
printf("请输入你的证件号: ");
scanf("%d",&w);
for(i=0;i<Num;i++)
{
if(pa1[i].snum==w)
{
b=pa2[i].mark;
printf("-------------%s的车次信息------- ",pa2[i].name);
printf("始发站:Yantai 终点站:%s ",L->data[b].Train_end);
printf("第%d号列车 票价: %d ",L->data[b].Train_num,L->data[b].Train_price);
printf("乘客姓名:%s ",pa2[i].name);
printf("乘客证件信息:%d ",pa2[i].snum);
printf("--------------------------------- ");
}
}
}break;
default:
printf("------------------------------ ");
printf("--你的输入有误,请重新输入!-- ");
printf("------------------------------ ");
Select(q,fp);
break;
}
if(true==0)
{
printf("没有该乘客的信息! ");
}
fclose(fp);
//getchar();
}
void Exchange(SeqList*L,FILE *fp)
{
printf("------------改签服务----------- ");
printf("请输入你的姓名: ");
char ta[10];
if((fp=fopen("train.dat","rb+"))==NULL)
{
printf("Canot open file ! ");
}
Person p[Num];
fseek(fp,0L,SEEK_END);
int r=ftell(fp)/sizeof(Person);
rewind(fp);
fread((void*)&p,sizeof(Person),r,fp);
int m,i,d,j,y,h;
scanf("%s",&ta);
for(i=0;i<r;i++)
{
if(strcmp(p[i].name,ta)==0){
d=p[i].mark;
printf("你目前的目的地是%s站 ",L->data[d].Train_end);
L->data[d].leftnum++;
}
}
Inquiry(L);
printf("请重新输入你想去的目的地的车次编号:");
scanf("%d",&m);
for(j=0;j<L->len;j++)
{
if(m == L->data[j].Train_num)
{
L->data[j].leftnum--;
printf("------------------------- ");
printf("---你的改签服务已完成!-- ");
printf("------------------------- ");
d=j;
}
}
for(i=0;i<r;i++)
{
if(strcmp(p[i].name,ta)==0){
p[i].mark=d;
h=i;
}
}
fseek(fp,h*sizeof(Person),SEEK_SET);
fwrite((void*)&p,sizeof(Person),1,fp);
fclose(fp);
}
void Withdraw(FILE *fp)
{
printf("----------退票服务------- ");
char na[10];
printf("请输入你的姓名: ");
scanf("%s",&na);
Person *pe;
int i,j;
if((fp=fopen("train.dat","rb"))==NULL)
{
printf("The file cannot be opened! ");
}
fseek(fp,0L,SEEK_END);
int n=ftell(fp)/sizeof(Person);
if(n==1)
{
fclose(fp);
fp=fopen("train.dat","wb");
}
pe=(Person*)malloc(sizeof(Person)*(n-1));
rewind(fp);
for(j=i=0;i<n;i++)
{
fread((void*)(pe+j),sizeof(Person),1,fp);
if((strcmp((pe+j)->name,na))==0)
continue;
j++;
}
fclose(fp);
fp=fopen("train.dat","wb");
fwrite((void*)pe,sizeof(Person),n-1,fp);
printf("------------------------- ");
printf("---你的退票服务已完成!-- ");
printf("------------------------- ");
}
void Inquiry(SeqList*L)
{
int i;
if(L->len==0)
{
printf("No ticket left! ");
}
else
{
printf("----------------------------Tickets information---------------------- ");
printf("火车车次编号 车 次 路 线 车票售价 剩余票数 ");
for(i=0;i<L->len;i++){
printf(" %d Yantai--%s %d %d ",L->data[i].Train_num,L->data[i].Train_end,L->data[i].Train_price,L->data[i].leftnum);
printf("--------------------------------------------------------------------- ");
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。
如果你想更好的提升你的编程能力,学好C语言C++编程!弯道超车,快人一步!
【C语言C++学习企鹅圈子】,分享(源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
编程学习书籍:
编程学习视频:
文章来源:https://blog.csdn.net/chaseqrr/article/details/93521777