⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hangkongdingpiao.c

📁 数据结构课程设计用C实现的航空订票系统
💻 C
📖 第 1 页 / 共 3 页
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define MAX 13
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -2
#define OPSETSIZE 7
#define STACK_INIF_SIZE 50
#define STACKINCREMENT 10
#define PR printf
int number[2][4];     
typedef int status;
typedef struct airline{
	char line_num[8];//航班号
	char plane_num[8];//飞机号
	char end_place[20];//目的地
	int total;//座位总数
	int left;//剩余座位
	struct airline *next;//下一个结点
}airline;

typedef struct customer{
	char name[9];//顾客名
	char line_num[8];//航班号
	int seat_num;//座位号
	struct customer *next;//下一个结点
}customer;

airline *init_airline(){//初始化链表
	airline *l;
	l=(airline*)malloc(sizeof(airline));
	if(l==NULL){
		exit(0);
		}
	l->next=NULL;
	return l;
}

customer * init_customer(){//初始化链表
	customer *l;
	l=(customer*)malloc(sizeof(customer));
	if(l==NULL){
		exit(0);
		}
	l->next=NULL;
	return l;
}

status insert_airline(airline **p,char *line_num,char *plane_num,char *end_place,int total,int left){//airline链表插入操作
	airline *q;
	q=(airline*)malloc(sizeof(airline));
	/*{
		PR("内存分配失败\n");
		return OVERFLOW;
	}*/
	strcpy(q->line_num , line_num);
	strcpy(q->plane_num , plane_num);
	strcpy(q->end_place , end_place);
	q->total  =total;
	q->left =left;
	q->next=NULL;
	(*p)->next=q;
	(*p)=(*p)->next;
 //   PR("insert %d	,%dis succssed!\n",e,bl);
	return OK;
	}

status insert_customer(customer **p,char *name,char *line_num,int seat){//customer链表插入操作
	customer *q;
	q=(customer*)malloc(sizeof(customer));
/*	{
		PR("内存分配失败\n");
		return OVERFLOW;
	}*/
	strcpy(q->name , name);
	strcpy(q->line_num , line_num);
	q->seat_num =seat;
	q->next=NULL;
	(*p)->next=q;
	(*p)=(*p)->next;
 //   PR("insert %d	,%dis succssed!\n",e,bl);
	return OK;
	}

airline *modefy_airline(airline *l,char *line_num)//修改airline链表中的数据
{
	airline *p;
	p=l->next ;
	for(;p!=NULL;p=p->next )
	{
		if(strcmp(line_num,p->line_num )==0)
		{
			p->left ++;
	//		PR("modefy %s\n",p->line_num );
			return l;
		}
	}
	PR("没有这个航班,无法完成修改任务!\n");
	return 0;
}

status delete_airline(airline *h,char *line_num)//删除航班
{
	airline *p,*pr;
	pr=h;
	p=pr->next ;
	while(p!=NULL)
	{
		if(strcmp(line_num,p->line_num )==0)
		{
			pr->next =p->next ;
			PR("删除  %s  航班\n",p->line_num  );
			return OK;
		}
		pr=pr->next ;
		p=pr->next ;
	}
	PR("无此航班,无法删除!\n");
	return ERROR;
}

status delete_customer(customer *h,char *line_num)//删除顾客
{
	customer *p,*pr;
	pr=h;
	p=pr->next ;
	while(p!=NULL)
	{
		if(strcmp(line_num,p->line_num )==0)
		{
			pr->next =p->next ;
		}
		pr=pr->next ;
		p=pr->next ;
	}
//	PR("无此航班,无法删除!\n");
	return OK;
}

status delete_cus(customer *h,airline *l,char *name)//顾客退票
{	
	customer *p,*pr;
	char line_num[8];
//	qr=h;
	pr=h;
	p=pr->next ;
//	PR("开始删除\n");
	while(p!=NULL)
	{
		if(strcmp(name,p->name )==0)
		{
			strcpy(line_num,p->line_num );
			l=modefy_airline(l,line_num);
			pr->next =p->next ;
			PR("顾客 %s 退票成功!\n",p->name );
			return OK;
		}
		pr=pr->next ;
		p=pr->next ;
	}
	PR("无此顾客,无法退票!\n");
	return ERROR;
}

status save_airline(airline *l)//保存airline.dat
{
	FILE *fp_airline;
	char ch='#';
	airline *p=l->next ;
	char filename[]="c:\\airline.dat";
	if((fp_airline=fopen(filename,"wb"))==NULL)
	{
		printf("can not open file to write:%s\n",filename);
		return ERROR;
	}
	for(;p!=NULL;p=p->next )
	{
	//	printf("%s,%s,%s,%d,%d\n",p->line_num ,p->plane_num ,p->end_place ,p->total ,p->left );
		fprintf(fp_airline,"%s,%s,%s,%d,%d%c\n",p->line_num ,p->plane_num ,p->end_place ,p->total ,p->left ,ch);
	}	
	fclose(fp_airline);
	return OK;
}

status save_customer(customer *l)//保存顾客信息 customer.dat
{
	FILE *fp_customer;
	char ch='#';
	customer *p=l->next ;
	char filename[]="c:\\customer.dat";
	if((fp_customer=fopen(filename,"wb"))==NULL)
	{
		printf("can not open file to write:%s\n",filename);
		return ERROR;
	}
	for(;p!=NULL;p=p->next )
	{
	//	PR("%s,%s,%d\n",p->name ,p->line_num ,p->seat_num );
		fprintf(fp_customer,"%s,%s,%d%c",p->name ,p->line_num ,p->seat_num ,ch);
	}	
	fclose(fp_customer);
	return OK;
}	

int changStrInt(char *ch)//把字符串转化为整型
{
	int a=1,b=0,c=0,i;
	for (i=strlen(ch)-1;i>=0;i--)
	{
		if (ch[i]<58&&ch[i]>47)
		{
			b=a*(ch[i]-48);
			a=a*10;
			c=c+b;

		}
		else 
		{
			PR("%c 不合法,无法将此字符串转化为整形!\n",ch[i]);
			return 0;
		}
//	printf("the c is %d\n",c);
	}
	return c;
}

status creat_airline(airline **l)//创建airline单链表
{
	airline *p=*l;
	int i=0;
	char *line_num[3]={"bjgy01","bjsh02","shgz03"};
	char *plane_num[3]={"plane1","plane2","plane3"};
	char *end_place[3]={"贵阳","上海","广州"};
	int total[3]={100,100,100};
	int left[3]={51,50,78};
	for (i=0;i<3;i++){
		insert_airline(&p,line_num[i],plane_num[i],end_place[i],total[i],left[i]);
	}
	return OK;
}

status creat_customer(customer **l)////创建customer单链表
{
	customer *p=*l;
	int i=0;
	char *name[3]={"李勇","张洁","刘胜"};
	char *line_num[3]={"bjgy01","bjsh02","shgz03"};
	int seat_num[3]={1,5,10};
	for (i=0;i<3;i++){
		insert_customer(&p,name[i],line_num[i],seat_num[i]);
	}
	return OK;
}

status increase_air(airline *l,char *line_num,char *plane_num,char *end_place,int total)//增加航线
{
	airline *p=l->next ;
	for(;p->next !=NULL;p=p->next){}
	insert_airline(&p,line_num,plane_num,end_place,total,total);
	PR("增加航班 %s 成功!\n",line_num);
	return OK;
}

status book(airline *l,char *line_num,customer *c,char *name)//订票
{
	airline *p=l;	
	customer *q=c->next ;
	p=l->next ;
	for(;q->next !=NULL;q=q->next){}		
//	PR("%s\n",q->name );
	for(;p!=NULL;p=p->next )
	{
		if(strcmp(line_num,p->line_num )==0)
		{
			if(p->left >0)
			{
				PR("恭喜您!订票成功!\n");
				PR("你的座位号是:  %d\n",(p->total -p->left +1));
				insert_customer(&q,name,line_num,p->total -p->left +1);
				p->left --;
				return OK;
			}
			else PR("对不起,座位已满!\n");
			return 0;
		}
	}
	PR("对不起,没有这个航班号!\n");
	return ERROR;
}

status print_airline(airline *l)//打印航线信息
{
	airline *p=l->next ;
	for(;p!=NULL;p=p->next )
	{
		PR("%8s%8s%8s%9d%9d\n",p->line_num ,p->plane_num ,p->end_place ,p->total ,p->left );
	}
	return OK;
}

status print_customer(customer *l)//打印顾客信息
{
	customer *p=l->next ;
	for(;p!=NULL;p=p->next )
	{
		PR("%10s      %10s       %d\n",p->name ,p->line_num ,p->seat_num );
	}
	return OK;
}

enum
{
	eNumber = 0,	//操作数
	eOperator = 1	//算子
};

int oper[7]={43,45,42,47,40,41,35};

typedef struct sqlist{
	int bol;//bol is 0 when num_ch is a number;bol is 1 when the num_ch is a oprater
	int num_ch;
	struct sqlist *next;
	}sqlist;

typedef struct sqstack{
	int *base;
	int *top;
	int stacksize;
	}sqstack;

unsigned char Prior[7][7] = {     // 表3.1  算符间的优先关系
  	  '>','>','<','<','<','>','>',
	  '>','>','<','<','<','>','>',
	  '>','>','>','>','<','>','>',
	  '>','>','>','>','<','>','>',	
	  '<','<','<','<','<','=',' ',
	  '>','>','>','>',' ','>','>',
	  '<','<','<','<','<',' ','='
};

char OPSET[OPSETSIZE]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'};

status init_sq(sqlist *l){//初始化链表
	l=(sqlist*)malloc(sizeof(sqlist));
	if(l==NULL){
		exit(OVERFLOW);
		}
	l->next=NULL;
	return OK;
	}

status insert_sq(sqlist **p,int e,int bl){//链表插入操作
	sqlist *q;
	q=(sqlist*)malloc(sizeof(sqlist));
	q->num_ch=e;
	q->bol=bl;
	q->next=NULL;
	(*p)->next=q;
	(*p)=(*p)->next;
 //   printf("insert %d	,%dis succssed!\n",e,bl);
	return OK;
	}

int check(sqlist l)//保证输入的数字是给出的四个数字
{
	int right=1,find=0,i;
	sqlist *q=&l;
	q=q->next ;
	for (;q->next!=NULL;q=q->next){
		if(q->bol==1){
			if(q->num_ch <=39||q->num_ch>57||q->num_ch==44||q->num_ch==46){
				right=0;
				printf("%c不是有效的运算符!\n");
			}
		}
		else {
			find=0;
			for(i=0;i<4;i++){
				if(number[1][i]==0&&number[0][i]==q->num_ch ){
					number[1][i]=1;
					find=1;
					break;
				}
			}
			if(find==0){				
				printf("%d 不在给出的四个数字中!\n",q->num_ch );
				right=0;
			}
		}
	}//end for
//	printf("the right is %d\n",right);
/*	for (i=0;i<4;i++){
		printf("number[1][%d]=%d\n",i,number[1][i]);
	}*/
	for (i=0;i<4;i++){
		if(number[1][i]==0){
			printf("%d没有用上!\n",number[0][i]);
			right=0;
		}
	}
	return right;
	
}

int chang(char *s,sqlist *l){//将用户的输入转化为单链表
	int t=0;
	unsigned int i=0;
   	 int bl,ch;
	int a1,a2,a;
	sqlist *p=l;
	for (;i<strlen(s);i++){
		if(s[i]>47&&s[i]<58&&t==0){
			a1=(int)s[i]-48;
			t++;
			}
		else if(s[i]>47&&s[i]<58&&t==1){
			a2=(int)s[i]-48;
			a=a1*10+a2;
			t++;
            }
		else if(s[i]<48&&s[i]>39&&s[i]!=44&&s[i]!=46){
			if(t==1){
                        bl=0;
						insert_sq(&p,a1,bl);
		                t=0;
				}
			else if(t==2){
				bl=0;
				insert_sq(&p,a,bl);
                t=0;
				}
			bl=1;
			ch=(int)s[i];
			insert_sq(&p,ch,bl);
            t=0;
			}
		else {
			printf("%c不是有效的运算符!\n",s[i]);
		}
		}   //end for
	i=strlen(s)-1;
	if(s[i]>47&&s[i]<58){
		if(s[i-1]>47&&s[i-1]<58){
		           bl=0;
			   insert_sq(&p,a,bl);
			   }
		else {
			bl=0;
			insert_sq(&p,a1,bl);
			}
		}
	bl=1;
	a=35;
	insert_sq(&p,a,bl);
//    printf("chang is OK\n");
	return (check(*l));
	}

/*status print_sq(sqlist *l)
{
	sqlist *p=l;
	if(l){
		for (p=p->next ;p!=NULL;p=p->next){

				printf("%d,%d\n",p->num_ch,p->bol );

				//printf("OK!");
				}
		}
	else printf("空!");
	return OK;
}*/

int Operate(int a,int theta, int b) {//计算
//	printf("a=%d,theta=%c,b=%d\n",a,theta,b);
   switch(theta) {

      case 43: return a+b;
      case 45: return a-b;
      case 42: return a*b;
      case 47:
		  {
			  if(b==0){
			//	printf("除数不能为0!\n");
				return -2000;
			//	exit(ERROR);
				}
			  if (a%b==0){
			  return a/b;
			  }
			  else {
			  //printf("不能为小数\n");
				return -100;
			 // exit(0);
			  }
		  }
      default : return 0;
   } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -