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 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 printList(struct list * l);

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

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

struct list * addTainInform(struct list * l);

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

struct list * createAllTrainInform();

int login();

int userUsage(struct list * l);

int administratorUsage(struct list * l);

int userMenu(struct list * l);

int administratorMenu(struct list * l);


.c文件

#include "train优化.h"

int main()
{
	login();
	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){//打印节点信息
	if(train!=NULL){
		printf("列车id: %d\n",train->train_inform->id);
		printf("列车名称:%s\n",train->train_inform->name);
		printf("剩余票数:%d\n",train->train_inform->remainTickets);
	}
	else{
		printf("系统内无此列车信息!\n");
	}
	return 0;
}

int printList(struct list * l){
	struct node * p;
	p = l->listhead->nd->next;
	while(p!=NULL){
		printNode(p);
		p=p->next;
	}
	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;
}

struct list * addTainInform(struct list * l){
	int id;
	char name[50];
	int remainTickets;
	struct node * p, * last , * check;
	last = l->listhead->nd;
	while(last->next!=NULL){
		last=last->next;
	}
	printf("依次输入:列车id号,名称,剩余票数!\n");
	scanf("%d%s%d",&id,&name,&remainTickets);
	check = l->listhead->nd;
	while(check!=last){
		check=check->next;
		if(id==check->train_inform->id||strcmp(name,check->train_inform->name)==0){
			printf("添加失败!id号或列车名称重复!\n");
			return l;
		}
	}
	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;
	printf("%s列车信息已添加!\n",p->train_inform->name);
	return l;
}

struct list * deleteTainInform(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;
	}
	if(l->listhead->nd->next==NULL){
		printf("列车信息为空!删除失败!\n");
	}
	else{
		p = l->listhead->nd->next;
		l->listhead->nd->next=p->next;
		printf("%s列车信息被删除!\n",p->train_inform->name);
		free(p);
	}
	return l;
}

struct list * createAllTrainInform(){
	struct list * l;
	l=initList();
	printf("请输入列车原始信息!\n");
	l = createList(l);
	return l;
}

int login(){
	int x;
	struct list * l;
	l = createAllTrainInform();
	printf("请登录!\n1.管理员登录 2.用户登录\n");
	scanf("%d",&x);
	switch(x){
	case 1:
		administratorUsage(l);break;
	case 2:
		userUsage(l);break;
	default:return 0;
	}
	return 0;
}

int userUsage(struct list * l){//打印用户提示界面
	printf("请按提示输入完成操作!\n");
	printf("1.查询车次信息\n");
	printf("2.订票\n");
	printf("3.退票\n");
	printf("4.退出系统\n");
	userMenu(l);
	return 0;
}

int administratorUsage(struct list * l){//打印管理员提示界面
	int pass;
loop:	printf("请输入管理员密码:");
	scanf("%d",&pass);
	if(pass==123456){
		printf("请按提示输入完成操作!\n");
		printf("1.查询车次信息\n");
		printf("2.增加车次信息\n");
		printf("3.删除车次信息\n");
		printf("4.打印所有车次信息\n");
		printf("5.退出系统\n");
		administratorMenu(l);
	}
	else{
		printf("密码错误!请重新输入!\n");
		goto loop;
	}
	return 0;
}

int userMenu(struct list * l){
	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);
			if(p==NULL){
				printf("//\n");
				break;
			}
			printNode(p);
			printf("//\n");
			break;
		case 2:
			printf("输入所要订票的列车的id号:");
			scanf("%d",&id);
			bookTrain(l,id);
			printf("//\n");
			break;
		case 3:
			printf("输入所要订票的列车的id号:");
			scanf("%d",&id);
			bookTrain(l,id);
			printf("//\n");
			break;
		case 4:
			k=0;
			printf("已退出系统……\n");
			printf("//\n");
			break;
		default:return 0;
		}
	}
	return 0;
}

int administratorMenu(struct list * l){
	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);
			printf("//\n");
			break;
		case 2:
			printf("添加列车信息!\n");
			l=addTainInform(l);
			printf("//\n");
			break;
		case 3:
			printf("输入所要删除的列车的id号:");
			scanf("%d",&id);
			l=deleteTainInform(l,id);
			printf("//\n");
			break;
		case 4:
			printList(l);
			printf("//\n");
			break;
		case 5:
			k=0;
			printf("已退出系统……\n");
			printf("//\n");
			break;
		default:return 0;
		}
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值