本文主要是介绍火车票订票系统(简化版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include "train简化.h"int main()
{usage();return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct train{int id;char name[50];int remainTickets;struct train * next;
};struct train * addTailNode(){struct train * L;L = (struct train *)malloc(sizeof(struct train));struct train * p,* last;last=L;int id;char name[50];int remainTickets;printf("请分别输入列车的id号,名称,剩余票数!输入终止id==-1\n");scanf("%d%s%d",&id,&name,&remainTickets);while(id!=-1){p = (struct train *)malloc(sizeof(struct train));p->id=id;strcpy(p->name,name);p->remainTickets=remainTickets;last->next=p;last=p;p->next=NULL;scanf("%d%s%d",&id,&name,&remainTickets);}return L;
}struct train * searchTrain(struct train * L,int id){struct train * p;p = L;while(p!=NULL){if(p->id==id){break;}p=p->next;}return p;
}int printNode(struct train * p){//打印节点信息printf("列车id: %d\n",p->id);printf("列车名称:%s\n",p->name);printf("剩余票数:%d\n",p->remainTickets);return 0;
}int bookTrain(struct train * L,int id){//订票int count=0;struct train * p;p = L;while(p!=NULL){if(p->id==id){p->remainTickets--;break;}p=p->next;}return 0;
}int returnTrain(struct train * L,int id){//订票struct train * p;p = L;while(p!=NULL){if(p->id==id){p->remainTickets++;break;}p=p->next;}return 0;
}int menu(){//具体实现usageprintf("请输入列车原始信息!\n");struct train * L;L = addTailNode();int x,id;int k=1;while(k){printf("请输入序列号:");scanf("%d",&x);switch(x){case 1:printf("输入所要查询列车的id号:");scanf("%d",&id);printNode(searchTrain(L,id));break;case 2:printf("输入所要订票列车的id号:");scanf("%d",&id);bookTrain(L,id);printNode(searchTrain(L,id));break;case 3:printf("输入所要订票列车的id号:");scanf("%d",&id);returnTrain(L,id);printNode(searchTrain(L,id));break;case 4:k=0;default:return 0;}}return 0;
}int usage(void){//打印提示界面printf("请按提示输入完成操作!\n");printf("1.查询车次信息\n");printf("2.订票\n");printf("3.退票\n");printf("4.退出系统\n\n");menu();return 0;
}
这篇关于火车票订票系统(简化版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!