火車車次查詢系統——時間最短,價格最優(非空雙向循環鏈表)C語言Linux實訓作業

huoche.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "huoche.h"
#define MAX 10001000

LLIST *p_checi=NULL;

char from[40];
char arrive[40];
int between;
int between_;
char wx[40];
char er[40];
char mingzi[40];

int checi_cmp(void *key, void *record)
{
	char *checi_name=key;
	struct checi *p=record;
	return strcmp(checi_name,p->checi_name);
}

int zhanming_cmp(void *key, void *record)
{
	char *zhanming=key;
	struct chezhan *p=record;
	return strcmp(zhanming,p->zhanming);
}

void create_checi_list()
{
	p_checi=llist_create(sizeof(struct checi));
}

void add_checi()
{
	struct checi cc;
	char checi_name[10];
	printf("==============\n");
	printf("請輸入車次:");
	scanf("%s", checi_name);
	strcpy(cc.checi_name, checi_name);
	cc.p_chezhan=llist_create(sizeof(struct chezhan));
	while(1)
	{
		struct chezhan cz;
		printf("請輸入車站名稱(輸入-1結束):");
		char zm[40];
		scanf("%s",zm);
		if(strcmp(zm, "-1")==0)
		{
			break;
		}
		strcpy(cz.zhanming,zm);
		printf("請輸入本站價格:");
		scanf("%d", &cz.jiage);
		printf("請輸入入站時間:");
		scanf("%d %d %d", &cz.h.hour, &cz.h.minute, &cz.h.day);
		printf("請輸入出站時間:");
		scanf("%d %d %d", &cz.x.hour, &cz.x.minute, &cz.x.day);
		llist_insert(cc.p_chezhan,&cz,2);
	}
		llist_insert(p_checi,&cc,2);
}

void delete()
{
	char checi_name[10];
	char zhanming[40];
	struct checi *train;
	struct chezhan *station;
	int x;
	printf("==============\n");
	printf("刪除車次輸入1\n");
	printf("刪除車站輸入2\n");
	scanf("%d", &x);
	if(x==1)
	{
		printf("------------------------------------------------------\n");
		printf("請輸入需要刪除的車次:");
		scanf("%s", checi_name);
		printf("------------------------------------------------------\n");
		train=llist_find(p_checi,checi_name,checi_cmp);
		if(train!=NULL)
		{
			llist_delete(p_checi,checi_name,checi_cmp);
			printf("刪除成功!\n");
			printf("------------------------------------------------------\n");
		}
		else
		{
			printf("!!!未找到該車次!!!\n");
		}
	}
	else
	{
		printf("------------------------------------------------------\n");
		struct checi *train;
		printf("請輸入需要刪除的車次:");
		scanf("%s", checi_name);
		printf("------------------------------------------------------\n");
		train=llist_find(p_checi,checi_name,checi_cmp);
		if(train!=NULL)
		{
			printf("請輸入需要刪除的車站:");
			scanf("%s", zhanming);
			printf("------------------------------------------------------\n");
			station=llist_find(train->p_chezhan,zhanming,zhanming_cmp);
			if(station!=NULL)
			{
				llist_delete(train->p_chezhan,zhanming,zhanming_cmp);
				printf("刪除成功!\n");
				printf("------------------------------------------------------\n");
			}
			else
			{
				printf("!!!未找到該車站!!!\n");
			}
		}
		else
		{
			printf("!!!未找到該車次!!!\n");
		}
	}
}

void revise()
{
	struct checi *train;
	struct chezhan *station;
	char checi_name[10];
	char zhanming[40];
	int x;
	printf("==============\n");
	printf("修改車次請輸入1\n");
	printf("修改車站請輸入2\n");
	scanf("%d", &x);
	if(x==1)
	{
		printf("------------------------------------------------------\n");
		printf("請輸入需要修改的車次名稱:");
		scanf("%s", checi_name);
		train=llist_find(p_checi,checi_name,checi_cmp);
		if(train!=NULL)
		{
			printf("請輸入修改之後的車次名稱:");
			scanf("%s", checi_name);
			printf("------------------------------------------------------\n");
			strcpy(train->checi_name, checi_name);
			print_checi(train);
		}
		else
		{
			printf("------------------------------------------------------\n");
			printf("!!!輸入錯誤!!!\n");
		}
	}
	else if(x==2)
	{
		printf("------------------------------------------------------\n");
		printf("請輸入該車站所在的車次名稱:");
		scanf("%s", checi_name);
		train=llist_find(p_checi,checi_name,checi_cmp);
		if(train!=NULL)
		{
			printf("請輸入需要修改的車站名稱:");
			scanf("%s", zhanming);
			station=llist_find(train->p_chezhan,zhanming,zhanming_cmp);
			if(station!=NULL)
			{
				printf("請輸入修改之後的車站名稱:");
				scanf("%s", zhanming);
				strcpy(station->zhanming, zhanming);
				printf("請輸入修改之後的車站價格:");
				scanf("%d", &station->jiage);
				printf("請輸入修改之後的入站時間:");
				scanf("%d %d %d", &station->h.hour, &station->h.minute, &station->h.day);
				printf("請輸入修改之後的出站時間:");
				scanf("%d %d %d", &station->x.hour, &station->x.minute, &station->x.day);
				printf("------------------------------------------------------\n");
				print_chezhan(station);
				printf("------------------------------------------------------\n");
			}
			else
			{
				printf("------------------------------------------------------\n");
				printf("!!!輸入錯誤!!!\n");
			}
		}
		else
		{
			printf("------------------------------------------------------\n");
			printf("!!!輸入錯誤!!!\n");
		}
	}
	else 
	{
		printf("------------------------------------------------------\n");
		printf("!!!輸入錯誤!!!\n");
	}
}

void find()
{
	int x;
	struct checi *train;
	struct checi *station;	
	char checi_name[10];
	char chezhan_name[40];
	printf("==============\n");
	printf("查詢車次輸入1\n");
	printf("查詢車站輸入2\n");
	scanf("%d", &x);
	if(x==1)
	{	
		printf("------------------------------------------------------\n");
		printf("請輸入需要查詢的車次名稱:");
		scanf("%s", checi_name);
		printf("------------------------------------------------------\n");
		train = llist_find(p_checi,checi_name,checi_cmp);
		if(train!=NULL)
		{
			print_checi(train);
			printf("\n");
		}
		else
		{
			printf("------------------------------------------------------\n");
			printf("抱歉,未查詢到該車次!\n\n");
		}
	}
	else if(x==2)
	{
		printf("------------------------------------------------------\n");
		printf("請輸入需要查詢的車次名稱:");
		scanf("%s", checi_name);
		train = llist_find(p_checi,checi_name,checi_cmp);
		if(train!=NULL)
		{
			printf("請輸入需要查詢的車站名稱:");
			scanf("%s", chezhan_name);
			printf("------------------------------------------------------\n");
			station = llist_find(train->p_chezhan,chezhan_name,zhanming_cmp);
			if(station!=NULL)
			{
				print_chezhan(station);
				printf("\n");
			}
			else
			{
				printf("------------------------------------------------------\n");
				printf("抱歉,未查詢到該車站!\n\n");
			}	
		}
		else
		{
			printf("------------------------------------------------------\n");
			printf("抱歉,未查詢到該車次!\n\n");
		}
		
	}
	else 
	{
		printf("------------------------------------------------------\n");
		printf("!!!輸入錯誤!!!\n");
	}
}

void travel_checi()
{
	llist_travle(p_checi,print_checi);
}

void print_chezhan(void *data)
{
	struct chezhan *cz=data;
	printf("站名:%s\t", cz->zhanming);
	printf("價格:%d\n", cz->jiage);
	printf("入站時間:%02d:%02d+%d\t", cz->h.hour, cz->h.minute, cz->h.day);
	printf("到站時間:%02d:%02d+%d\n", cz->x.hour, cz->x.minute, cz->x.day);
}

void print_checi(void *data)
{
	struct checi *cc=data;
	printf("車次:%s\n", cc->checi_name);
	llist_travle(cc->p_chezhan,print_chezhan);
}

void print__(void *data)
{
    struct checi *cc=data;
    struct chezhan *sj1;
    sj1=llist_find(cc->p_chezhan,wx,zhanming_cmp);
    struct chezhan *sj2;
    sj2=llist_find(cc->p_chezhan,er,zhanming_cmp);
    int price=sj2->jiage-sj1->jiage;
    if (between>price)
    {
        between=price;
        strcpy(from,sj1->zhanming);
        strcpy(arrive,sj2->zhanming);
        strcpy(mingzi,cc->checi_name);
    }
}

void print_to_(void *data)
{
    struct checi *cc=data;
    struct chezhan *sj1;
    sj1=llist_find(cc->p_chezhan,wx,zhanming_cmp);
    struct chezhan *sj2;
    sj2=llist_find(cc->p_chezhan,er,zhanming_cmp);
    int time=(sj2->h.day-sj1->x.day)*24*60+(sj2->h.hour-sj1->x.hour)*60+(sj2->h.minute-sj1->x.minute);
    if(between_>time)
    {
        between_=time;
        strcpy(from,sj1->zhanming);
        strcpy(arrive,sj2->zhanming);
        strcpy(mingzi,cc->checi_name);
    }
}

void search_min_price(LLIST *ptr)
{
	printf("==============\n");
    printf("請輸入要查詢的起始車站名字:");
    scanf("%s", wx);
    printf("請輸入要查詢的到達車站名字:");
    scanf("%s", er);
    between=MAX;
    llist_travle(p_checi,print__);
	printf("------------------------------------------------------\n");
    printf("從%s到%s的價格最優車次爲:%s\n",from,arrive,mingzi);
    printf("最優價格爲:%d元\n",between);
	printf("------------------------------------------------------\n");
}

void search_min_time(LLIST *ptr)
{
	printf("==============\n");
    printf("請輸入要查詢的起始車站名字:");
    scanf("%s", wx);
    printf("請輸入要查詢的到達車站名字:");
    scanf("%s", er);
    between_=MAX;
    llist_travle(p_checi,print_to_);
	printf("------------------------------------------------------\n");
    printf("從%s到%s的時間最優車次爲:%s\n",from,arrive,mingzi);
    printf("最優時間爲:%d分鐘\n",between_);
	printf("------------------------------------------------------\n");
}

void menu()
{
	create_checi_list();
	int x=0;
	while(1)
	{
		printf("\n>>>>>>>>車次管理系統<<<<<<<<\n\n");
		printf("1、輸入車次和車站信息\n");
		printf("2、刪除車次或車站\n");
		printf("3、修改車次或車站\n");
		printf("4、查詢車次或車站\n");
		printf("5、顯示全部車次信息\n");
		printf("6、查找最優價格\n");
		printf("7、查找最優時間\n");
		printf("8、退出\n");
		printf("==============\n");
		printf("請輸入操作指令:");
		scanf("%d", &x);
		if(x==1)
		{
			add_checi();
		}
		else if(x==2)
		{
			delete();
		}
		else if(x==3)
		{
			revise();
		}
		else if(x==4)
		{
			find();
		}
		else if(x==5)
		{
			printf("==============\n");
			travel_checi();
		}
		else if(x==6)
		{
			search_min_price(p_checi);
		}
		else if (x==7)
		{
			search_min_time(p_checi);
		}
		else if(x==8)
		{
			printf("==============\n");
			printf("\n\t歡迎再次使用!\n\n");
			break;
		}
		else
		{
			printf("!!!輸入錯誤!!!\n");
		}
	}
}

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

huoche.h

#ifndef _HUOCHE_
#define _HUOCHE_
#include "llist.h"
struct shijian
{
	int minute;
	int hour;
	int day;
};
struct chezhan
{
    char zhanming[40];
    int jiage;
    struct shijian h, x;
};
struct checi
{
    char checi_name[10];
    LLIST *p_chezhan;
};
extern LLIST *p_checi;
int cmp(void *key, void *record);
void create_checi_list();
void add_checi();
void travel_checi();
void delete_checi();
void alter_chezhan();
void find_chezhan();
void print_chezhan(void *data);
void print_checi(void *data);
void print__(void *data);
void print_to_(void *data);
void search_min_price(LLIST *ptr);
void search_min_time(LLIST *ptr);
void menu();
#endif

list.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "llist.h"
#if 0
struct llist_node_st
{
	struct llist_node_st *prev;
	struct llist_node_st *next;
	char data[0];
};
typedef struct
{
	int size;
	struct llist_node_st head;
}LLIST;
#endif
LLIST *llist_create(int size)
{
	LLIST *new;
	new=malloc(sizeof(*new));
	if(new==NULL)
	{
		return NULL;
	}
	new->size=size;
	new->head.prev=new->head.next=&new->head;
	return new;
}

int llist_insert(LLIST *ptr,const void *data,int mode)
{
	struct llist_node_st *newnode;
	newnode=malloc(sizeof(*newnode)+ptr->size);
	if(newnode==NULL)
	{
		return -1;
	}
	memcpy(newnode->data,data,ptr->size);
	if(mode==LLIST_FORWARD)
	{
		newnode->next=ptr->head.next;
		newnode->prev=&ptr->head;
	}
	else if(mode==LLIST_BACKWARD)
	{
		newnode->next=&ptr->head;
		newnode->prev=ptr->head.prev;
	}
	newnode->next->prev=newnode;
	newnode->prev->next=newnode;
	return 0;
}
void llist_travle(LLIST *ptr,void (*op)(void *))
{
	struct llist_node_st *cur;
	for(cur=ptr->head.next;cur!=&ptr->head;cur=cur->next)
	{
		op(cur->data);
	}
}
struct llist_node_st *find__(LLIST *ptr, void *key,int (*cmp)( void *, void *))
{
	struct llist_node_st *cur;
	for(cur=ptr->head.next;cur!=&ptr->head;cur=cur->next)
	{
		if(cmp(key,cur->data)==0)
		{
			break;
		}
	}
	return cur;	
}

void * llist_find(LLIST *ptr, void *key,int (*cmp)( void *, void *))
{
	struct llist_node_st *node;
	node=find__(ptr,key,cmp);
	if(node==&ptr->head)
	{
		return NULL;
	}
	return node->data;
}

void llist_delete(LLIST *ptr,void *key,int (*cmp)( void *, void *))
{
	struct llist_node_st *node;
	node=find__(ptr,key,cmp);
	if(node==&ptr->head)
	{
		return;
	}
	node->next->prev=node->prev;
	node->prev->next=node->next;
	free(node);
}
int llist_fetch(LLIST *ptr,void *data,void *key,int (*cmp)( void *, void *))
{
	struct llist_node_st *node;
	node=find__(ptr,key,cmp);
	if(node==&ptr->head)
	{
		return -1;
	}
	memcpy(data,node->data,ptr->size);
	node->next->prev=node->prev;
	node->prev->next=node->next;
	free(node);
	return 0;
}

void llist_destroy(LLIST *ptr)
{
	struct llist_node_st *cur,*save;
	for(cur=ptr->head.next;cur!=&ptr->head;cur=save)
	{
		save=cur->next;
		free(cur);
	}
	free(ptr);
}

list.h

#ifndef LLIST_H
#define LLIST_H
#define LLIST_FORWARD 1
#define LLIST_BACKWARD 2
struct llist_node_st
{
	struct llist_node_st *prev;
	struct llist_node_st *next;
	char data[0];
};
typedef struct
{
	int size;
	struct llist_node_st head;
}LLIST;
LLIST *llist_create(int size);
int llist_insert(LLIST *ptr,const void *data,int mode);
void llist_travle(LLIST *ptr,void (*op)(void *));
void * llist_find(LLIST *ptr, void *key,int (*cmp)( void *, void *));
void llist_delete(LLIST *ptr,void *key,int (*cmp)( void *, void *));
int llist_fetch(LLIST *ptr,void *data,void *key,int (*cmp)( void *, void *));
void llist_destroy(LLIST *ptr);
#endif

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章