航班管理系统
要求:
账号注册与登录
普通用户:查看航班信息,查看已购买的航班信息,退票
管理员:增、删、改、查 航班信息
航班信息:
起始点,目的地,航班时间,剩余票数,价格
已完成:
只能注册一个账号
普通用户:查看航班信息,查看已购买的航班信息,退票
管理员:增、删、改、查 航班信息
待完成:
多用户注册
管理员增加、修改航班信息时增加信息检验是否合法
使用数据结构:
单链表(增删改查)
结构体
指针
完成日期:
2021/09/12 18:00
历时10h
参考资料:
一、数据结构—单链表的基本操作(源代码)
二、C语言实现学生信息管理系统(单链表有错误,仅供参考!)
#include
#include
#include
#include
#define ID 30
#define PW 30
static char user_id[ID] = {0}; //账号
static char user_pw[PW] = {0}; //密码
char flight_name[10]; //航班名称
char flight_start[10]; //起始点
char flight_bourn[10]; //目的地
char flight_schedule[10]; //航班时间
char flight_s_votes[10]; //剩余票数
char flight_price[5]; //价格
typedef struct Flight //航班信息
{
char name[10]; //航班名称
char start[10]; //起始点
char bourn[10]; //目的地
char f_schedule[10]; //航班时间
char spare_votes[10]; //剩余票数
char price[5]; //价格
}flight;
typedef struct LNode //节点
{
flight data; //航班信息
struct LNode *next; //指针
}Node,*LinkList;
void InitList(LinkList *pHead) //初始化链表 //创建头结点
{
*pHead=(LinkList)malloc(sizeof(Node));
if(NULL!=pHead)
(*pHead)->next=NULL;
else
printf("\n开辟内存失败\n");
}
//创建新结点
LinkList get_newNode(LinkList *L)
{
LinkList newNode=(LinkList)malloc(sizeof(Node));
if(NULL!=newNode)
{
printf("请输入要增加的航班名称: \t");
scanf("%s", flight_name);
strcpy(newNode->data.name, flight_name);
// newNode->data.name = flight_name;
printf("请输入该航班的起始点: \t");
scanf("%s", flight_start);
strcpy(newNode -> data.start, flight_start);
printf("请输入该航班的目的地: \t");
scanf("%s", flight_bourn);
strcpy(newNode -> data.bourn, flight_bourn);
printf("请输入该航班的航班时间:\t");
scanf("%s", flight_schedule);
strcpy(newNode -> data.f_schedule, flight_schedule);
printf("请输入该航班的价格: \t");
scanf("%s", flight_price);
strcpy(newNode -> data.price, flight_price);
printf("请输入该航班的剩余票数: \t");
scanf("%s", flight_s_votes);
strcpy(newNode -> data.spare_votes, flight_s_votes);
// printf("%s,%s,%s,%s,%s,%s\n",
// flight_name, flight_start, flight_bourn,
// flight_schedule,flight_price,flight_s_votes);
newNode->next=NULL;
LinkList aa=(*L)->next;
while( aa != NULL) //判断添加的航班是否存在
{
if((strcmp(aa -> data.name, flight_name) == 0)&&
strcmp(aa -> data.start, flight_start) == 0&&
strcmp(aa -> data.bourn, flight_bourn) == 0&&
strcmp(aa -> data.f_schedule, flight_schedule) == 0 )
{
printf("\n此航班已存在!请再次确认!\n");
return 0;
}
aa = aa -> next;
}
return newNode;
}
else
{
printf("\n开辟内存失败\n");
return 0;
}
}
//打印
void print_List(LinkList *L)
{
LinkList p;
p=(*L)->next;
if(p==NULL)
printf("暂无航班!\n");
else
{
int i =0;
while(NULL!=p)
{
printf("\n航班的名称:%s\n", p -> data.name);
printf("航班的起始点:%s\n", p -> data.start);
printf("航班的目的地:%s\n", p -> data.bourn);
printf("航班的航班时间:%s\n", p -> data.f_schedule);
printf("航班的剩余票数:%s\n", p -> data.spare_votes);
printf("航班的价格:%s\n", p -> data.price);
printf("\n");
i++;
p=p->next;
}
printf("共有%d个航班!\n",i);
}
}
void addflight(LinkList *L) //增航班信息
{
int flag = 1;
while(flag)
{
//尾插
char s[2];
int ch;
LinkList tail;
tail=*L;
while(NULL!=tail->next) //让tail指向最后一个结点
tail = tail->next;
tail->next = get_newNode(L);
while(flag)
{
printf("是否继续增加航班信息?是:1 否:0\n");
scanf("%s",s);
if (strspn(s, "01") == strlen(s))
{
ch = atoi(s);
if(ch == 0)
{
printf("已退出增加航班!\n");
flag = 0;
}
else
{
continue;
}
}
else
{
printf("请输入正确的序号!\n");
}
}
}
}
void deleteflight(LinkList *L) //删航班信息
{
printf("请输入要删除的航班信息:\t\n");
printf("航班名称:\t");
scanf("%s", flight_name);
printf("航班起始点:\t");
scanf("%s", flight_start);
printf("航班目的地:\t");
scanf("%s", flight_bourn);
printf("航班时间:\t");
scanf("%s",flight_schedule);
LinkList p,q;
for(p = *L; NULL != p -> next; p = p -> next)
{
if((strcmp(p -> next -> data.name, flight_name) == 0)&&
strcmp(p -> next -> data.start, flight_start) == 0&&
strcmp(p -> next -> data.bourn, flight_bourn) == 0&&
strcmp(p -> next -> data.f_schedule, flight_schedule) == 0 )
{
q = p -> next; //因为最后要释放被删除的结点,所以先记录下来
p -> next = q -> next;
free(q);
q = NULL;
printf("删除成功\n");
return;
}
}
printf("删除失败!,请检查此航班是否存在!\n");
}
void findflight(LinkList *L) //查航班信息
{
char choose[5];
int flag = 1, ch;
while(flag)
{
printf("\n\t** 请输入要查询的方式 **:\n");
printf("\t**1.按航班的名称查询 **\n");
printf("\t**2.按航班的起始点 **\n");
printf("\t**3.按航班的目的地 **\n");
printf("\t**4.按航班的航班时间 **\n");
printf("\t**5.查询所有航班 **\n");
printf("\t**6.退出查询 **\n");
scanf("%s",choose);
if (strspn(choose, "0123456789") == strlen(choose))
{
ch = atoi(choose);
switch(ch)
{
case 1:
printf("请输入要查询的航班名称:\t");
scanf("%s", flight_name);
int ii=0;
LinkList q=(*L)->next;
while(q != NULL)
{
if(strcmp(q -> data.name, flight_name) == 0)
{
printf("\n该航班的名称:%s\n", q -> data.name);
printf("该航班的起始点:%s\n", q -> data.start);
printf("该航班的目的地:%s\n", q -> data.bourn);
printf("该航班的航班时间:%s\n", q -> data.f_schedule);
printf("该航班的剩余票数:%s\n", q -> data.spare_votes);
printf("该航班的价格:%s\n", q -> data.price);
ii++;
}
q = q -> next;
}
if(ii == 0)
{
printf("该航班不存在!\n");
}
else
{
printf("要查询的航班共有%d个\n",ii);
}
break;
case 2:
printf("请输入要查询的航班起始点:\t");
scanf("%s", flight_start);
int ai = 0;
LinkList a=(*L)->next;
while(a != NULL)
{
if(strcmp(a -> data.start, flight_start) == 0)
{
printf("\n该航班的名称:%s\n", a -> data.name);
printf("该航班的起始点:%s\n", a -> data.start);
printf("该航班的目的地:%s\n", a -> data.bourn);
printf("该航班的航班时间:%s\n", a -> data.f_schedule);
printf("该航班的剩余票数:%s\n", a -> data.spare_votes);
printf("该航班的价格:%s\n", a -> data.price);
ai++;
}
a = a -> next;
}
if(ai == 0)
{
printf("该航班不存在!\n");
}
else
{
printf("要查询的航班共有%d个\n",ai);
}
break;
case 3:
printf("请输入要查询的目的地:\t");
scanf("%s", flight_bourn);
int bi=0;
LinkList b=(*L)->next;
while(b != NULL)
{
if(strcmp(b -> data.bourn,flight_bourn) == 0)
{
printf("\n该航班的名称:%s\n", b -> data.name);
printf("该航班的起始点:%s\n", b -> data.start);
printf("该航班的目的地:%s\n", b -> data.bourn);
printf("该航班的航班时间:%s\n", b -> data.f_schedule);
printf("该航班的剩余票数:%s\n", b -> data.spare_votes);
printf("该航班的价格:%s\n", b -> data.price);
bi++;
}
b = b -> next;
}
if(bi == 0)
{
printf("该航班不存在!\n");
}
else
{
printf("要查询的航班共有%d个\n",bi);
}
break;
case 4:
printf("请输入要查询的航班时间:\t");
scanf("%s", flight_schedule);
int ci=0;
LinkList c=(*L)->next;
while(c != NULL)
{
if(strcmp(c -> data.f_schedule,flight_schedule) == 0)
{
printf("\n该航班的名称:%s\n", c -> data.name);
printf("该航班的起始点:%s\n", c -> data.start);
printf("该航班的目的地:%s\n", c -> data.bourn);
printf("该航班的航班时间:%s\n", c -> data.f_schedule);
printf("该航班的剩余票数:%s\n", c -> data.spare_votes);
printf("该航班的价格:%s\n", c -> data.price);
ci++;
}
c = c -> next;
}
if(ci == 0)
{
printf("该航班不存在!\n");
}
else
{
printf("要查询的航班共有%d个\n",ci);
}
break;
case 5:
print_List(L);
break;
case 6:
flag = 0;
break;
default:
printf("请输入正确的序号!\n");
break;
}
}
else
{
printf("请输入正确的序号!\n");
}
}
printf("已退出查询!\n");
}
void changeflight(LinkList *L) //改航班信息
{
int flag = 1;
while(flag)
{
//暂存修改后的数据
char flight_name1[10]; //航班名称
char flight_start1[10]; //起始点
char flight_bourn1[10]; //目的地
char flight_schedule1[10]; //航班时间
char flight_s_votes1[10]; //剩余票数
char flight_price1[5]; //价格
printf("请输入要修改的航班信息:\t\n");
printf("航班名称:\t");
scanf("%s", flight_name);
printf("航班起始点:\t");
scanf("%s", flight_start);
printf("航班目的地:\t");
scanf("%s", flight_bourn);
printf("航班时间:\t");
scanf("%s",flight_schedule);
LinkList w = (*L)->next;
int pp = 0;
while(NULL != w)
{
if((strcmp(w -> data.name, flight_name) == 0)&&
strcmp(w -> data.start, flight_start) == 0&&
strcmp(w -> data.bourn, flight_bourn) == 0&&
strcmp(w -> data.f_schedule, flight_schedule) == 0 )
{
printf("\n修改后的航班名称:\n");
scanf("%s", flight_name1);
strcpy(w->data.name, flight_name1);
printf("修改后的起始点:\n");
scanf("%s", flight_start1);
strcpy(w->data.start, flight_start1);
printf("修改后的目的地:\n");
scanf("%s", flight_bourn1);
strcpy(w->data.bourn, flight_bourn1);
printf("修改后的航班时间:\n");
scanf("%s", flight_schedule1);
strcpy(w->data.f_schedule, flight_schedule1);
printf("修改后的剩余票数:\n");
scanf("%s", flight_s_votes1);
strcpy(w->data.spare_votes, flight_s_votes1);
printf("修改后的价格:\n");
scanf("%s", flight_price1);
strcpy(w->data.price, flight_price1);
pp = 1;
}
else
{
w = w -> next;
}
}
if( pp == 0)
{
printf("修改失败!无此航班!\n");
}
else
{
printf("修改成功!");
}
while(flag)
{
char n[5];
int nn;
printf("是否需要继续修改其他航班信息?继续:1 退出 :0\n");
scanf("%s",n);
if (strspn(n, "01") == strlen(n))
{
nn = atoi(n);
if(nn == 0)
{
flag = 0;
}
else
{
break;
}
}
else
{
printf("请正确输入!\n");
}
}
}
}
void admin(LinkList *L) //管理员 增 删 查 改 航班信息
{
char choose[10];
int flag=1,ch;
while(flag)
{
printf("\n");
printf("请输入你要进行的操作:\t\n\n");
printf("\t***1.增加航班信息 ***\t\n");
printf("\t***2.删除航班信息 ***\t\n");
printf("\t***3.查找航班信息 ***\t\n");
printf("\t***4.修改航班信息 ***\t\n");
printf("\t***5.退出管理员操作***\t\n");
scanf("%s",choose);
if (strspn(choose, "0123456789") == strlen(choose))
{
ch = atoi(choose);
switch(ch)
{
case 1:
addflight(L);
break;
case 2:
deleteflight(L);
break;
case 3:
findflight(L);
break;
case 4:
changeflight(L);
break;
case 5:
flag = 0;
break;
default:
printf("请输入正确的序号!\n");
break;
}
}
else
{
printf("请输入正确的序号!\n");
}
}
printf("已退出管理员操作!\n");
}
void findownerflight(LinkList *z) //查看自己的航班
{
int flag = 1;
while(flag)
{
LinkList q = (*z) -> next;
int i=1, aa = 0;
while(q != NULL)
{
printf("\n购买的第%d个航班信息:\n",i);
printf("已购买航班的名称:%s\n", q -> data.name);
printf("已购买航班的起始点:%s\n", q -> data.start);
printf("已购买航班的目的地:%s\n", q -> data.bourn);
printf("已购买航班的航班时间:%s\n", q -> data.f_schedule);
printf("已购买航班的剩余票数:%s\n", q -> data.spare_votes);
printf("已购买航班的价格:%s\n", q -> data.price);
i++;
aa = 1;
q = q -> next;
}
if( aa == 0)
{
printf("你未购买任何航班\n");
}
while(flag)
{
char n[5];
int nn;
printf("是否退出?继续查看:1 退出 :0\n");
scanf("%s",n);
if (strspn(n, "01") == strlen(n))
{
nn = atoi(n);
if(nn == 0)
{
printf("退出成功!\n");
flag = 0;
}
else
{
break;
}
}
else
{
printf("请正确输入!\n");
}
}
}
}
LinkList buy(LinkList *L, LinkList *z)
{
printf("\n请输入要购买的航班信息:\t\n");
printf("航班名称:\t");
scanf("%s", flight_name);
printf("航班起始点:\t");
scanf("%s", flight_start);
printf("航班目的地:\t");
scanf("%s", flight_bourn);
printf("航班时间:\t");
scanf("%s",flight_schedule);
LinkList w = (*L)->next;
LinkList u=(LinkList)malloc(sizeof(Node));
int pp = 0;
while(NULL != w)
{
if((strcmp(w -> data.name, flight_name) == 0)&&
strcmp(w -> data.start, flight_start) == 0&&
strcmp(w -> data.bourn, flight_bourn) == 0&&
(strcmp(w -> data.f_schedule, flight_schedule) == 0)&&
(NULL != u))
{
strcpy(u->data.name, w->data.name);
strcpy(u->data.start, w->data.name);
strcpy(u->data.bourn, w->data.name);
strcpy(u->data.f_schedule, w->data.name);
strcpy(u->data.spare_votes, w->data.name);
strcpy(u->data.price, w->data.name);
printf("购买成功!\n");
pp = 1;
return u;
}
else
{
w = w -> next;
}
}
if( pp == 0)
{
printf("够买失败!无此航班!\n");
return 0;
}
}
void buyticket(LinkList *L, LinkList *z)
{
int flag = 1;
while(flag)
{
printf("\t**1.搜索航班信息 **\n");
printf("\t**2.购买机票 **\n ");
printf("\t**3.退出购票 **\n");
char n[5];
int nn;
scanf("%s",n);
if (strspn(n, "123") == strlen(n))
{
nn = atoi(n);
LinkList tail1;
switch(nn)
{
case 1:
findflight(L);
break;
case 2:
tail1=*z;
while(NULL!=tail1->next) //让tail指向最后一个结点
tail1 = tail1->next;
tail1->next = buy(L, z);
break;
case 3:
printf("退出成功!");
flag = 0;
break;
}
}
else
{
printf("请正确输入!\n");
}
}
}
void Refund(LinkList *L) //退票
{
printf("请输入要退票的航班信息:\t\n");
printf("航班名称:\t");
scanf("%s", flight_name);
printf("航班起始点:\t");
scanf("%s", flight_start);
printf("航班目的地:\t");
scanf("%s", flight_bourn);
printf("航班时间:\t");
scanf("%s",flight_schedule);
LinkList p,q;
for(p = *L; NULL != p -> next; p = p -> next)
{
if((strcmp(p -> next -> data.name, flight_name) == 0)&&
strcmp(p -> next -> data.start, flight_start) == 0&&
strcmp(p -> next -> data.bourn, flight_bourn) == 0&&
strcmp(p -> next -> data.f_schedule, flight_schedule) == 0 )
{
q = p -> next; //因为最后要释放被删除的结点,所以先记录下来
p -> next = q -> next;
free(q);
q = NULL;
printf("退票成功\n");
return;
}
}
printf("退票失败!,请检查是否已购买此机票!\n");
}
// 普通用户
//只查看航班信息,票的余数
//购买票的数量 可以查看自己的所购买的班次 退票
void common_user(LinkList *L, LinkList *z) //普通用户
{
int flag = 1, ch;
while(flag)
{
printf("\n\t***1.查找航班信息 ***\t\n");
printf("\t***2.查看已购买航班 ***\t\n");
printf("\t***3.购买航班 ***\t\n");
printf("\t***4.退票 ***\t\n");
printf("\t***5.退出普通用户操作 ***\t\n");
printf("\n请输入你要进行的操作:\t\n");
char choose[10];
scanf("%s",choose);
if (strspn(choose, "0123456789") == strlen(choose))
{
ch = atoi(choose);
switch(ch)
{
case 1:
findflight(L);
break;
case 2:
findownerflight(z);
break;
case 3:
buyticket(L, z);
break;
case 4:
Refund(z);
break;
case 5:
flag = 0;
break;
default:
printf("请输入正确的序号!\n");
break;
}
}
else
{
printf("请输入正确的序号!\n");
}
}
printf("退出普通用户操作成功!\n");
}
void login(char *id, char *pw) //注册
{
int flag = 1,n;
char input_num[10] = {0};
while(flag)
{
printf("\t**1.开始注册 2.返回主界面**\t\n");
scanf("%s",input_num);
if (strspn(input_num, "0123456789") == strlen(input_num))
{
n = atoi(input_num);
switch(n)
{
case 1:
printf("请输入账号: \n");
scanf("%s", id);
printf("请输入密码: \n");
scanf("%s", pw);
printf("\n注册成功!请返回登陆!\n");
break;
case 2:
printf("退出注册成功!\n");
flag = 0;
break;
case 0:
flag = 0;
break;
default :
printf("请输入正确的序号!\n");
break;
}
}
else
{
printf("请输入正确的序号!\n");
}
}
}
void sign(char *u_id, char *u_pw, LinkList *L, LinkList *z)
{
char login_id[ID] = {0};
char login_pw[PW] = {0};
// printf("%s\t%s\n", u_id, u_pw);
printf("请输入账号: \t");
scanf("%s", login_id);
printf("请输入密码: \t");
scanf("%s", login_pw);
// printf("\n%s\n%s\n", login_id, login_pw);
if( (strcmp(login_id, "admin") == 0) && (strcmp(login_pw, "8888888") == 0))
{
printf("\n管理员账号登录成功!\n");
admin(L); //管理员
}
else if( (strcmp(login_id, u_id) == 0) && (strcmp(login_id, u_pw) == 0))
{
printf("\n普通用户登录成功!\n");
common_user(L, z);; //普通用户
}
else
{
printf("此账号未注册!请先注册!\n");
}
}
int main()
{
LinkList L ,USER;
InitList(&L);
InitList(&USER);
printf("\n");
int flag = 1, nu;
while(flag)
{
printf("\n");
printf("\t***********************\t\n");
printf("\t******航班购票系统******\t\n");
printf("\t****1.注册 2.登录****\t\n");
printf("\t*********0.退出*********\t\n");
printf("\t***********************\t\n");
char num[10];
scanf("%s",num);
if (strspn(num, "0123456789") == strlen(num))
{
nu = atoi(num);
switch(nu)
{
case 0:
printf("已退出系统!\n");
flag = 0;
break;
case 1:
login(user_id, user_pw); //注册
break;
case 2:
sign(user_id, user_pw, &L, &USER); //登录
break;
default :
printf("请输入正确的序号!\n");
break;
}
}
else
{
printf("请输入正确的序号!\n");
}
}
}