单链表管理数据----管理员操作程序
内核链表管理客户/用户端信息
文件IO实现存储录入数据,数据与链表之间可进行相互调用
进入管理员界面
用户登录界面
项目具有一些BUG,为方便缺少一些判断
主函数2.c
#include "myhead.h"
#include "kernel_list.h"
extern int root_inquire1();
extern int root_add();
extern int root_delete();
//定义一个内核链表
struct airplane
{
char ID[10];
//航班号
char Start_Place[10];
//城市
char End_Place[10];
//城市
char name[20];
//名字
char time[20];
//时间
//城市
float price;
//价格
struct list_head mypoint;
};
struct airplane *list_init()
{
struct airplane *head=malloc(sizeof(struct airplane));
//初始化头结点里面的指针
INIT_LIST_HEAD(&(head->mypoint));
return head;
}
//增加票房
int fly_insert(struct airplane*head)
{
char sometime[20];
char someStart_Place[20];
char someEnd_Place[20];
char someID[10];
float someprice;
printf("输入航班ID:");
scanf("%s", someID);
printf("输入始发地:");
scanf("%s", someStart_Place);
printf("输入目的地:");
scanf("%s", someEnd_Place);
printf("输入航班起飞时间:");
scanf("%s", sometime);
printf("输入价格:");
scanf("%f", &someprice);
printf("添加航班信息完成!\n\n");
struct airplane*newnode=malloc(sizeof(struct airplane));
strcpy(newnode->time,sometime);
strcpy(newnode->Start_Place,someStart_Place);
strcpy(newnode->End_Place,someEnd_Place);
newnode->price=someprice;
strcpy(newnode->ID,someID);
INIT_LIST_HEAD(&(newnode->mypoint));
FILE*file=fopen("/mnt/hgfs/share/航班管理系统/1.txt","a+");
fprintf(file,"编号ID:%s 航线:%s->%s 时间:%s 价格: %f\n",someID,someStart_Place,someEnd_Place,sometime,someprice);
fclose(file);
//尾插
list_add_tail(&(newnode->mypoint),&(head->mypoint));
return 0;
}
//查询
int inquire(struct airplane*head)
{
struct airplane*p;
list_for_each_entry(p,&(head->mypoint),mypoint)
{
printf("\n");
printf("欢迎乘坐钟落潭航班,您已经预定了:航班编号ID:%s 的航班\n",p->ID);
printf("==============================================================================================\n");
}
FILE * adminop = fopen("/mnt/hgfs/share/航班管理系统/1.txt","r+");
char buf[100]={0};
//将每个管理员账号密码分配到单独的单链表节点中
while((fgets(buf,100,adminop))!=NULL)
{
printf("%s",buf);
printf("\n");
}
}
int root_inquire(struct airplane*head)
{
struct airplane*p;
list_for_each_entry(p,&(head->mypoint),mypoint)
{
printf("\n");
printf("您已经添加了:1.航班编号ID:%s 2.时间: %s 3.始发地城市:%s 4.目的地城市:%s 5.价格:%f的航班\n",p->ID,p->time,p->Start_Place, p->End_Place,p->price );
printf("==============================================================================================\n");
printf("目前在售航班:\n");
}
FILE * adminop = fopen("/mnt/hgfs/share/航班管理系统/1.txt","r+");
char buf[100]={0};
//将每个管理员账号密码分配到单独的单链表节点中
while((fgets(buf,100,adminop))!=NULL)
{
printf("%s",buf);
printf("\n");
}
}
//删除信息
int delete(struct airplane *head)
{
char del_ID[20]={0};
printf("请输入要删除的飞机票编号!\n");
scanf("%s",del_ID);
//遍历内核链表找到要删除的书籍
struct airplane *pos,*n;
//注意:下面这个是for循环,请你不要画蛇添足加上分号
list_for_each_entry_safe(pos,n,&(head->mypoint),mypoint)
{
if(strcmp(pos->ID,del_ID)==0) //找到就删除
{
list_del(&(pos->mypoint));
}
}
return 0;
}
int airplane_update(struct airplane *head)
{
char oldname[20]={0};
printf("请输入你想修改哪张票的信息,请输入旅客名字!\n");
scanf("%s",oldname);
struct airplane *pos;
list_for_each_entry(pos,&(head->mypoint),mypoint)
{
if(strcmp(pos->name,oldname)==0)
{
printf("输入修改票上编号!\n");
scanf("%s",pos->ID);
printf("输入修改票上旅客名字!\n");
scanf("%s",pos->name);
}
}
return 0;
}
//订票
int BookAirPlane(struct airplane*head)
{
char newname[20];
char someID[10];
printf("输入航班ID:");
scanf("%s", someID);
printf("输入名字:");
scanf("%s", newname);
printf("预定完成!\n\n");
struct airplane*newnode1=malloc(sizeof(struct airplane));
strcpy(newnode1->ID,someID);
strcpy(newnode1->name,newname);
INIT_LIST_HEAD(&(newnode1->mypoint));
//尾插
list_add_tail(&(newnode1->mypoint),&(head->mypoint));
return 0;
}
//注册账号密码
int vip_init()
{
FILE *file1;
char name[20]={0};
char passwd[20]={0};
char buf[100]={0};
//打开文件
file1=fopen("/mnt/hgfs/share/航班管理系统/2.txt","a+");
if(file1==NULL)
{
perror("打开文件失败!\n");
return -1;
}
bzero(name,20);
bzero(passwd,20);
printf("请输入要注册的用户名!\n");
scanf("%s",name);
if(strcmp(name,"quit")==0)
{
printf("推出,已返回登录界面!!\n");
return -1;
}
printf("请输入要注册的密码!\n");
scanf("%s",passwd);
fprintf(file1,"%s@%s\n",name,passwd);
printf("恭喜您刚才输入的用户名密码注册成功!\n");
printf("已返回登录界面");
printf("\n");
fclose(file1);
return 0;
}
//判断黑名单用户
int vip_init5(char * name2)
{
FILE *file1;
file1=fopen("/mnt/hgfs/share/航班管理系统/3.黑名单.txt","r");
if(file1==NULL)
{
perror("打开文件失败balck!\n");
return -1;
}
char buf[20]={0};
while ((fgets(buf, 20, file1)) != NULL)
{
char blackname[20] = {0};
// 从文件读取的账号密码最后会多个换行符\n
for (int i = 0; i < strlen(buf) - 1; i++)
{
blackname[i]=buf[i];
}
if(strcmp(blackname,name2)==0){
//黑名单检测
printf("black\n");
return -1;
}
bzero(buf, 40);
}
fclose(file1);
return 0;
}
//管理员拉黑
//登录账号
int vip1_init()
{
FILE *file2;
char name1[20]={0};
char passwd1[10]={0};
char buf[40];
//打开文件
file2=fopen("/mnt/hgfs/share/航班管理系统/2.txt","r");
if(file2==NULL)
{
perror("打开文件失败!\n");
return -1;
}
lb:
printf("请输入用户名和密码登录,输入quit退出!\n");
scanf("%s%s",name1,passwd1);
if(strcmp(name1,"quit")==0)
{
return -1;
}
//判断你输入的用户名是否被占用
while ((fgets(buf, 40, file2)) != NULL)
{
char rightname[20] = {0};
char rightpwd[15] = {0};
// 从文件读取的账号密码最后会多个换行符\n
for (int i = 0, j = 0, flag = 0; i < strlen(buf) - 1; i++)
{
if (buf[i] == '@')
{
flag = 1;
continue;
}
if (flag == 0)
{
rightname[i] = buf[i];
}
else
{
rightpwd[j++] = buf[i];
}
}
if(strcmp(rightname,name1)==0&&strcmp(rightpwd,passwd1)==0){
//黑名单检测
if(vip_init5(name1)==-1)
{
printf("你已被拉入黑名单");
return -1;
}
fclose(file2);
return 0;
}
bzero(buf, 40);
}
printf("对不起,登录失败\n");
goto lb;
}
int vip_init6(){
FILE *file1;
char name3[20];
file1=fopen("/mnt/hgfs/share/航班管理系统/3.黑名单.txt","a");
if(file1==NULL)
{
perror("打开文件失败balck!\n");
return -1;
}
printf("请输入要拉黑的用户名!\n");
scanf("%s",name3);
if(strcmp(name3,"quit")==0)
{
printf("推出,已返回登录界面!!\n");
return -1;
}
sprintf(name3,"%s\n",name3);
fputs(name3, file1);
fseek(file1, 0, SEEK_CUR);
// fprintf(file1,"%s@\n",name3);
printf("恭喜您刚才输入的用户名成功拉进黑名单!\n");
printf("已返回登录界面");
printf("\n");
}
int main()
{
system("clear");
int a, i, j;
struct airplane * myhead=list_init();
struct airplane *pos;
lb:
printf("\n*****************************欢迎进入飞机票务系统*****************************\n");
printf("\n*************您好,现在要确认您的身份!票务人员请按 1 ,旅客请按 0 ***********\n");
printf("\n***************************** 新用户注册请按2 ***************************\n");
printf("\n***************************** 推出程序请按3 *****************************\n");
scanf("%d", &a);
system("clear");
while(1)
{
if (a == 0)
{
if(vip1_init()==-1)
{goto lb;}
printf("恭喜您登录成功!\n");
do{
printf("\n*********************** 1.订票 **********************\n");
printf("\n*********************** 2.改签 **********************\n");
printf("\n*********************** 3.退票 **********************\n");
printf("\n*********************** 4.浏览航班信息 **************\n");
printf("\n*********************** 5.查询个人订票信息 **********\n");
printf("\n*********************** 6.返回登录界面 **************\n");
printf("\n*********************** 0.退出 **********************\n");
scanf("%d", &j);
switch (j)
{
case 0:
return 0;
break;
case 1:
BookAirPlane(myhead);
break;
case 2:
airplane_update(myhead);
break;
case 3:
delete(myhead);
break;
case 4:
system("clear");
inquire(myhead);
break;
case 5:
system("clear");
list_for_each_entry(pos,&(myhead->mypoint),mypoint)
{
printf("\n");
printf("欢迎乘坐钟落潭航班,目前已经订的票编号: %s 旅客:%s \n",pos->ID,pos->name);
printf("==============================================================================================\n");
} break;
case 6:
system("clear");
goto lb;
break;
}
}while(j!=0);
}
//航班管理人员操作
if (a == 1)
{
//printf("请输入管理员账号:");
char one[20]={"123456"};
//printf("请输入管理员密码:");
char two[20]={"123456"};
char newone[20]={0};
char newtwo[20]={0};
printf("请输入账号");
scanf("%s",newone);
printf("请输入密码");
scanf("%s",newtwo);
if(strcmp(newone,one)==0&&strcmp(newtwo,two)==0)
{
do {
printf("\n*********************** 1.添加航班信息! **********************\n");
printf("\n*********************** 2.删除航班信息! **********************\n");
printf("\n*********************** 3.浏览航班信息! **********************\n");
printf("\n*********************** 4.返回登录界面! **********************\n");
printf("\n*********************** 5.拉黑用户! **************************\n");
printf("\n*********************** 0.退出程序! **********************\n");
scanf("%d", &i);
switch (i)
{
case 0:
return -1;
break;
case 1:
root_add();
//fly_insert(myhead);
break;
case 2:
root_delete();
break;
case 3:
system("clear");
root_inquire1();
break;
case 4:
system("clear");
goto lb;
break;
case 5:
vip_init6();
break;
}
} while (i != 0);
}else
{
printf("输入密码账号不对\n");
goto lb;
}
//乘客操作
}
if(a==2)
{
vip_init();
goto lb;
}
if(a==3)
{
return 0;
}
}
}
========================================================================
单链表文件
4.单链表.c
#include "myhead.h"
//定义一个结构体用来表示单链表
#include "head.h"
int root_add()
{
//初始化单链表
struct siglelist *myhead1=list1_init();
root_init1(myhead1);
char buf[20]={0};
char root_time[20];
char root_Start_Place[20];
char root_End_Place[20];
char root_ID[10];
float root_price;
printf("输入航班ID:");
scanf("%s", root_ID);
printf("输入始发地:");
scanf("%s", root_Start_Place);
printf("输入目的地:");
scanf("%s", root_End_Place);
printf("输入航班起飞时间:");
scanf("%s", root_time);
printf("输入价格:");
scanf("%f", &root_price);
list_insert1(myhead1,root_ID,root_Start_Place,root_End_Place,root_time,root_price);
printf("添加航班信息完成!\n\n");
siglelist1(myhead1);
list_show1(myhead1);
return 0;
}
int root_inquire1()
{
struct siglelist *myhead1=list1_init();
root_init1(myhead1);
list_show1(myhead1);
return 0;
}
int root_delete()
{
//初始化单链表
struct siglelist *myhead1=list1_init();
// list_insert(myhead,"1010","广州","北京","12:00",55);
char buf[20]={0};
root_init1(myhead1);
printf("请输入你要删除的航班ID");
scanf("%s",buf);
list_delete1(myhead1,buf);
siglelist1(myhead1);
list_show1(myhead1);
return 0;
}
========================================================================
头文件head.c
#include "myhead.h"
//定义一个结构体用来表示单链表
struct siglelist
{
//数据域--》不是说只有一个数据,可以有多个数据
char ID[10];
//航班号
char Start_Place[10];
//城市
char End_Place[10];
//城市
char time[20];
//时间
float price;
//价格
//指针域
struct siglelist *next; //存放下一个节点在内存中的首地址
};
//单链表的初始化
struct siglelist *list1_init()
{
//头节点不存放任何有效数据
struct siglelist *head=malloc(sizeof(struct siglelist));
head->next=NULL; //暂时不知道后面的数据是谁
return head;
}
//插入数据--》尾插
int list_insert1(struct siglelist *list,char * ID,char*Start_Place,char *End_Place,char*time,float price)
{
struct siglelist *newnode=malloc(sizeof(struct siglelist));
strcpy(newnode->time,time);
strcpy(newnode->Start_Place,Start_Place);
strcpy(newnode->End_Place,End_Place);
newnode->price=price;
strcpy(newnode->ID,ID);
newnode->next=NULL;
struct siglelist *p=list; //指针p指向链表的头
while(p->next!=NULL)
p=p->next; //p往后挪动,循环结束的时候,p指向的就是链表最后面的那个节点
p->next=newnode;
return 0;
}
int root_init1(struct siglelist *head)
{
FILE *commoditop = fopen("/mnt/hgfs/share/航班管理系统/1.txt", "r");
char buf[50] = {0};
while ((fgets(buf, 50, commoditop)) != NULL)
{
char sometime[20]={0};
char someStart_Place[20]={0};
char someEnd_Place[20]={0};
char someID[10]={0};
float someprice;
char *p = NULL;
p = strtok(buf, "-");
sprintf(someID, "%s", p);
p = strtok(NULL, "-");
sprintf(someStart_Place, "%s", p);
p = strtok(NULL, "-");
sprintf(someEnd_Place, "%s", p);
p = strtok(NULL, "-");
sprintf(sometime, "%s", p);
p = strtok(NULL, "-");
someprice=atof(p);
// sprintf(someprice, "%s", p);
list_insert1(head, someID, someStart_Place, someEnd_Place, sometime,someprice);
bzero(buf, 50);
}
}
//查询打印数据
int list_show1(struct siglelist *list)
{
struct siglelist *p=list; //p指向链表的头节点
while(p->next!=NULL)
{
p=p->next;
printf("1.航班编号ID:%s 2.始发地城市:%s 3.目的地城市:%s 4.时间: %s 5.价格:%f 的航班\n",p->ID,p->Start_Place, p->End_Place,p->time,p->price );
}
return 0;
}
int list_delete1(struct siglelist *list,char * someID)
{
// 定义标志位标记要删除的数据是否存在
int flag=0;
// 定义两个指针,一前一后
struct siglelist *q=list; //q指向头
struct siglelist *p=list->next; //p指向头的下一个
// 通过p找到要删除的那个节点
while(p!=NULL)
{
if(strcmp(p->ID,someID)==0) //找到要删除的节点
{
q->next=p->next;
p->next=NULL;
free(p);
// 释放p指向的节点以后,程序还需要继续往后找其它的deldata
p=q->next;
flag=1;
}
else
{
p=p->next;
q=q->next;
}
}
if(flag==0)
{
printf("对不起,没有你要删除的数据!\n");
return -1;
}
return 0;
}
int siglelist1(struct siglelist *head)
//链表头
{
FILE *op = fopen("/mnt/hgfs/share/航班管理系统/1.txt","w+");
struct siglelist*p = head;
while (p->next != NULL)
{
p = p->next;
char buf[100] = {0};
sprintf(buf, "%s-%s-%s-%s-%f\n", p->ID, p->Start_Place, p->End_Place, p->time,p->price);//每一行写入文件里
fputs(buf, op);
fseek(op, 0, SEEK_CUR);
// 此句不加,会等程序正常结束才进行写入,若程序异常则不写入
}
}
=========================================================================
#include "myhead.h"
/*
把所有常用的头文件都包含进来,方便使用,不需要再去查重复的头文件
*/
#ifndef _MYHEAD_H
#define _MYHEAD_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
===================================================================
内核链表头文件
#include "kernel_list.h"
======================================================================
3个txt文档
注意文档光标位置,光标位置不对会产生段错误,把光标依到起点/文档内容删除就行