《2022年用单链表编制一个简单的火车票销售系统程序代码 .pdf》由会员分享,可在线阅读,更多相关《2022年用单链表编制一个简单的火车票销售系统程序代码 .pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、用单链表编制一个简单的火车票销售系统,可完成售票、退票、车票剩余情况查询等功能。每张车票包含车次、座位等信息。要求:在售票、退票、查询剩余票等环节中,都必须显示出车票的信息, 即车次、 座位情况。 为简单起见, 在此假设所有出售的车票均为同一车次的车票。退票时,必须是车站售出的列车票才能退(这里以同一车次的为例),否则视为无效票,不能办理退票业务。#include #include #define ShowCommand(x) cout (x) endl usingnamespace std; typedef struct tagTicket int nCheCi; / 车次int nNumb
2、er; / 座号Ticket; typedef struct tagTicketNode Ticket ticket; tagTicketNode* pNextTicket; TicketNode; typedef TicketNode * TicketList; TicketList gTicketList; void ShowAllTickets(); bool InitTicketsList(); void BuyTicket(); int GetTicketCount(); int main() gTicketList = new TicketNode; / initiall Tick
3、ets list if (!InitTicketsList() cout tickets initialize failed endl; return0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - cout Please input command iCommand; while (iCommand != 0) ShowCommand(iCommand); switch (iCommand) case 0: / break
4、; case 1: / Show All Tickets; ShowAllTickets(); break; case 2: / buy a ticket BuyTicket(); break; case 3: cout current tickets count is =GetTicketCount() endl; default : /assert(false); break; cout Please input command 0: exit , 1: buy, 2: iCommand; return0; void ShowAllTickets() TicketNode* tmpTick
5、Node = gTicketList; for ( int i =1; i 5; i+) cout ticket.nNumber pNextTicket; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - bool InitTicketsList() gTicketList-ticket.nCheCi = 100; gTicketList-ticket.nNumber = 0; TicketNode* nextTicket= ne
6、w TicketNode; gTicketList-pNextTicket = nextTicket; for ( int i =1; iticket.nCheCi = 100; nextTicket-ticket.nNumber = i; nextTicket-pNextTicket = new TicketNode; nextTicket = nextTicket-pNextTicket; nextTicket-pNextTicket = NULL; returntrue ; void BuyTicket() gTicketList = gTicketList-pNextTicket; r
7、eturn ; int GetTicketCount() int nCount = 1; TicketNode* tmpNode = gTicketList; while (tmpNode) nCount+; if (tmpNode-pNextTicket) tmpNode = tmpNode-pNextTicket; return nCount; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - 设有 n 个学生,每个学生包含学
8、号、姓名、分数三部分数据,其中学号是关键字,依次输入这些学生数据来建立一棵二叉排序树,然后再输入一个学号进行查找,查找成功则输出该学生的数据;查找失败输出找不到信息。#include #include #include /字符串操作#include using namespace std; #define N 50 /学生数#define M 10 /课程数struct student char name20; int number; int score8; stu60; string kechengmingM; void changesort(struct student a,int n,i
9、nt j) int flag=1,i; struct student temp; while(flag) flag=0; for(i=1;in-1;i+=2) if(ai.scorejai+1.scorej) /*奇数项比较 */ temp=ai; ai=ai+1; ai+1=temp; flag=1; for(i=0;in-1;i+=2) if(ai.scorejai+1.scorej) /*偶数项比较 */ temp=ai; ai=ai+1; ai+1=temp; flag=1; void print(struct student a,int n,int j) int i,k; coutk
10、echengmingj前 5 名数据如下: endl; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - coutsetw(8)名次setw(8) 学号setw(8) 姓名setw(8)kechengmingjendl; k=1; for(i=0;k=5 & i0 & ai.scorej!=ai-1.scorej) k+; coutsetw(8)k; coutsetw(8)ai.number; coutsetw(8)ai.name
11、; coutsetw(8)ai.scorej; coutendl; int main() int i,j,k,n,m; struct student temp; cout 请输入学生数 ( 最多为 Nn; cout 请输入课程数 ( 最多为 M-2m; kechengmingm=sum;kechengmingm+1=avg; for(i=0;im;i+) cout请输入第 i+1kechengmingi; /输入课程名 for(i=0;in;i+) cout请输入第 i+1stui.name; cout请输入第 i+1stui.number; for(j=0;jm;j+) cout请输入 ke
12、chengmingjstui.scorej; for(i=0;in;i+) stui.scorem=0; for(j=0;jm;j+) stui.scorem+=stui.scorej; stui.scorem+1=stui.scorem/m; changesort(stu,n,m); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - cout 学生成绩如下: endl; coutsetw(6)名次; coutsetw(6)姓名;
13、 coutsetw(6)学号; for(i=0;im+2;i+) coutsetw(6)kechengmingi; coutendl; k=1; for(i=0;i0 & stui.scorem!=stui-1.scorem) k+; coutsetw(6)k; coutsetw(6)stui.name; coutsetw(6)stui.number; for(j=0;jm+2;j+) coutsetw(6)stui.scorej; coutendl; j=0; cout 请输入您要对第几个成绩进行排序(1mi; while(i=0 & j=0) if(i0 & im+1) changesort(stu,n,i-1); print(stu,n,i-1); cout请输入您要对第几个成绩进行排序:(1mi; else if(i=0) j=1; else couti; return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -