首页 > 其他 > 详细

火车票订票系统(链表结构化设计)

时间:2014-04-12 04:51:49      阅读:839      评论:0      收藏:0      [点我收藏+]
train.h文件

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct train {//车次的属性
	int id;
	char name[50];
	int remainTickets;
};

struct node {//普通节点的属性
	struct node *next;
	struct train * train_inform;	
};

struct head {//头节点属性
	struct node * nd;
	int fg;
};

struct tail {//尾节点属性
	struct node * nd;
	int fg;
};

struct list{//链表的属性
	struct head * listhead;
	int len;
};

struct list * initList();

struct list * createList(struct list * l);

struct node * searchTrain(struct list * l,int id);

int printNode(struct node * train);

int bookTrain(struct list * l,int id);

int returnTrain(struct list * l,int id);

int menu(struct list * l);

int usage(void);

/////////////////////////////////////////////////////////

train.c文件


#include "train.h"

int main()
{
	usage();
	return 0;
}

struct list * initList(){
	struct list * l;
	l = (struct list *)malloc(sizeof(struct list));
	l->listhead = (struct head *)malloc(sizeof(struct head));
	l->listhead->nd = (struct node *)malloc(sizeof(struct node));
	l->listhead->nd->train_inform = (struct train *)malloc(sizeof(struct train));
	l->listhead->nd->next=NULL;
	l->len=0;
	return l;
}

struct list * createList(struct list * l){
	int id;
	char name[50];
	int remainTickets;
	struct node * p, * last;
	last=l->listhead->nd;
	printf("依次输入:列车id号,名称,剩余票数(id==-1终止!)\n");
	scanf("%d%s%d",&id,&name,&remainTickets);
	while(id!=-1){
		p=(struct node *)malloc(sizeof(struct node));
		p->train_inform=(struct train *)malloc(sizeof(struct train));
		p->train_inform->id=id;
		strcpy(p->train_inform->name,name);
		p->train_inform->remainTickets=remainTickets;
		l->len++;
		last->next=p;  
		last=p;  
		p->next=NULL;
		scanf("%d%s%d",&id,&name,&remainTickets);
	}
	return l;
}

struct node * searchTrain(struct list * l,int id){//查询,返回节点信息
	struct node * p;
	p = l->listhead->nd;
	while(p!=NULL){
		if(p->train_inform->id==id){
			break;
		}
		p=p->next;
	}
	return p;
}

int printNode(struct node * train){//打印节点信息
	printf("列车id: %d\n",train->train_inform->id);
	printf("列车名称:%s\n",train->train_inform->name);
	printf("剩余票数:%d\n",train->train_inform->remainTickets);
	return 0;
}

int bookTrain(struct list * l,int id){//订票
	struct node * p;
	p = l->listhead->nd;
	while(p!=NULL){
		if(p->train_inform->id==id){
			p->train_inform->remainTickets--;
			break;
		}
		p=p->next;
	}
	printNode(p);
	return 0;
}
	
int returnTrain(struct list * l,int id){//退票
	struct node * p;
	p = l->listhead->nd;
	while(p!=NULL){
		if(p->train_inform->id==id){
			p->train_inform->remainTickets++;
			break;
		}
		p=p->next;
	}
	printNode(p);
	return 0;
}
int menu(struct list * l){//具体实现usage
	int x,id;
	int k=1;
	struct node * p;
	while(k){
		printf("请输入序列号:");
		scanf("%d",&x);
		switch(x){
		case 1:
			printf("输入所要查询的列车的id号:");
			scanf("%d",&id);
			p = searchTrain(l,id);
			printNode(p);
			break;
		case 2:
			printf("输入所要订票的列车的id号:");
			scanf("%d",&id);
			bookTrain(l,id);
			break;
		case 3:
			printf("输入所要订票的列车的id号:");
			scanf("%d",&id);
			bookTrain(l,id);
			break;
		case 4:k=0;break;
		default:return 0;
		}
	}
	return 0;
}

int usage(void){//打印提示界面
	struct list * l;
	l=initList();
	printf("请输入列车原始信息!\n");
	l = createList(l);
	printf("请按提示输入完成操作!\n");
	printf("1.查询车次信息\n");
	printf("2.订票\n");
	printf("3.退票\n");
	printf("4.退出系统\n");
	menu(l);
	return 0;
}

火车票订票系统(链表结构化设计),布布扣,bubuko.com

火车票订票系统(链表结构化设计)

原文:http://blog.csdn.net/u011700203/article/details/23478615

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!