C语言双向循环链表

目录

项目:航空管理系统

用C语言双链表实现

两个登陆界面

登陆界面功能区别

程序文件 

项目功能

代码部分 

 1、main.c

2、flight.h

 3、flight.c

4、print.h

5、print.c

6、menu.c

7、file.h

8、file.c

9、makefile(不使用变量)

10、makefile(使用一个变量)

11、makefile(使用多个变量)


项目:航空管理系统

用C语言双链表实现

两个登陆界面

一、管理员登陆界面

二、购票者登陆界面

登陆界面功能区别

管理员登陆界面

ß 1 、增
ß 2 、删
ß 3 、保存到文件
ß 4 、文件信息 读到链表
ß 5 、指定查找
ß 6 、排序
ß 7 、查看所有航班信息
ß 8 、查看操作日志
ß 9 、录入多个航班信息
ß 10 、修改航班信息
ß 0 、退回登陆界面

 购票者登陆界面

ß 1 指定信息查找航班信息
ß 2 、排序
ß 3 查看所有航班 信息
ß 4 、购票
ß5、退票
ß 0 退回登陆界面

程序文件 

ß 1 主程序

                          ​​​​​​​        ​​​​​​​         main.c

ß 2 、链表程序

           ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        flight.c    flight.h

ß 3 、显示

                                  ​​​​​​​        print.c      print.h

ß 4 菜单

          ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        menu.c

ß 5 、文件操作

          ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        file.c  file.h

项目功能

ß 1 、登陆(输入验证码)
ß
ß
ß 2 、链表的增、删、改、查、排序、清空数据
ß
ß
ß 3 、文件的操作:写入文件,从文件中读取,  日志

 

代码部分 

 1、main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "include/flight.h"
#include "include/print.h"
#include "include/file.h"

/*1、	功能:
实现以下功能:
1)	信息录入
2)	信息显示(按照起飞时间先后顺序显示)
3)	信息查询(可根据不同的关键字进行查询)
2、	要求:
1)	使用链表对录入的信息进行存储
2)	对录入的信息进行排序(可按起飞时间进行排序)
3)	使用make对工程进行管理
*/

int main()
{
    Node_list* head = creat_flight_list();
    if (head == NULL)
    {
        perror("malloc error");
        return -1;
    }
    
    head = menu_flight(head);//调用菜单
    
    return 0;
}

2、flight.h

#ifndef _FLIGHT_H_
#define _FLIGHT_H_

typedef struct flight //航班信息数据结构
{
    char number[20];     //航班号
    char staddress[30];  //起点站
    char arraddress[30]; //终点站
    char DATE[30];       //班期
    char TYPE[20];       //机型
    char stime[30];      //起飞时间
    char atime[30];      //到达时间
    char value[20];      //票价
    int poll;            //票数
} Data_type;

typedef struct Node //双向链表结点
{
    Data_type *info;
    struct Node *next, *prev;
} Node_list;

typedef void (*showfun_t)(Data_type *info);
Node_list *creat_flight_list(); //创建双链表

int length_list(Node_list *head);       //链表元素个数
int data_cmp(char *data1, char *data2); //对比

void info_find(Node_list *head);              //查找信息
int find_number(Node_list *head, char *info); //航班位置查找

Node_list *cmp_find(Node_list *head);         //排序查找
Node_list *sort_time_list(Node_list *head);   //按起飞时间排序
Node_list *sort_value_list(Node_list *head);  //按航班票价排序
Node_list *sort_number_list(Node_list *head); //按航班号排序

void input_one_info(Data_type *data);    //录入一个信息
Node_list *input_tinfo(Node_list *head); //尾插录入信息

Node_list *menu_flight(Node_list *head); //菜单

int input_hinfo(Node_list *head, Data_type *newinfo, int size); //头插法
int rapid_input(Node_list *head);                               //头插法录入多个航班信息
Node_list *revamp_flight_info(Node_list *head);                 //修改

Node_list *delete_loca_flight(Node_list *head, int loca); //按位置删除的航空信息
Node_list *destroy_list(Node_list *head);                 //清空链表中的数据

void show_one1(Data_type *datainfo);
#endif

 3、flight.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "include/flight.h"
#include "include/print.h"
#include "include/file.h"

/*******************双向循环链表*********************/

/*************创建双链表***************/
Node_list *creat_flight_list()
{
    Node_list *head = (Node_list *)malloc(sizeof(Node_list));
    if (head == NULL)
    {
        perror("malloc error");
        return NULL;
    }
    head->info = NULL;
    head->next = head->prev = head;
    return head;
}

//录入一个信息
void input_one_info(Data_type *data)
{
    printf("************请输入您要录入的信息**********\n");
    printf("请输入航班号(如:CZ3355):");
    scanf("%s", data->number);
    printf("请输入航班起点站(如:长沙):");
    scanf("%s", data->staddress);
    printf("请输入航班终点站(如:北京):");
    scanf("%s", data->arraddress);
    printf("请输入航班班期(如:202207):");
    scanf("%s", data->DATE);
    printf("请输入航班机型(如:737):");
    scanf("%s", data->TYPE);
    printf("请输入航班起飞时间(如:12:20):");
    scanf("%s", data->stime);
    printf("请输入航班到达时间(如:15:10):");
    scanf("%s", data->atime);
    printf("请输入航班票价(如:800):");
    scanf("%s", data->value);
    int i = 0;
    for (i = 0; data->value[i] != '\0'; i++)
        ;
    strcpy(data->value + i, "元");
    printf("请输入航班票数(如:50):");
    scanf("%d", data->poll);
}

//尾插循环录入信息
Node_list *input_tinfo(Node_list *head)
{
    if (head == NULL)
    {
        perror("head is null\n");
        return NULL;
    }
    char ch;
    //申请节点空间,
    while (1)
    {
        Node_list *newnode = (Node_list *)malloc(sizeof(Node_list));
        if (newnode == NULL)
        {
            perror("malloc error\n");
        }
        //数据节点
        newnode->info = (Data_type *)malloc(sizeof(Data_type));

        input_one_info(newnode->info);

        head->prev->next = newnode;
        newnode->prev = head->prev;
        newnode->next = head;
        head->prev = newnode;

        printf("是否继续录入(y/n):");
        getchar(); //空吞回车
        ch = getchar();
        system("clear");
        if (ch == 'n' || ch == 'N')
        {
            return head;
        }
        Ulog_flight("尾插法录入信息");
    }
}

//头插法录入信息
int input_hinfo(Node_list *head, Data_type *newinfo, int size)
{
    if (head == NULL || newinfo == NULL || size <= 0)
    {
        perror("head is null\n");
        return -1;
    }

    //申请节点空间,
    Node_list *newnode = (Node_list *)malloc(sizeof(Node_list));
    if (newnode == NULL)
    {
        perror("malloc error\n");
    }
    //数据节点
    newnode->info = malloc(size);

    memcpy(newnode->info, newinfo, size);

    newnode->next = head->next;
    head->next->prev = newnode;
    head->next = newnode;
    newnode->prev = head;
    Ulog_flight("头插法录入信息");
    return 0;
}

//按位置删除的航空信息
Node_list *delete_loca_flight(Node_list *head, int loca)
{
    if (head == NULL || head->next == head)
    {
        perror("head is null or 没数据请先录入!!!\n");
        return head;
    }
    int len = length_list(head);
    if (loca <= 0 || loca > len)
    {
        printf("失败!!!第%d的位置没数据\n", loca);
        return head;
    }
    int i = 0;
    Node_list *temp = head;
    while (i != loca && temp->next != head)
    {
        temp = temp->next;
        i++;
    }
    if (i == loca)
    {
        temp->prev->next = temp->next;
        temp->next->prev = temp->prev;
        free(temp);
    }
    return head;
}

//清空链表
Node_list *destroy_list(Node_list *head)
{
    if (head == NULL)
    {
        perror("head is NULL");
        return NULL;
    }
    Node_list *temp = head;
    Node_list *var = NULL;
    while (temp->next != head)
    {
        temp = temp->next;
        var = temp;
        free(var->info);
        free(var);
    }
    temp = NULL;
    var = NULL;
    head->next = head->prev = head;
    printf("链表数据已清空\n");
    return head;
    Ulog_flight("清空数据");
}

//链表元素个数
int length_list(Node_list *head)
{
    if (head == NULL)
    {
        perror("head is null\n");
        return 0;
    }
    int i;
    Node_list *temp = head;
    while (temp->next != head)
    {
        temp = temp->next;
        i++;
    }
    return i;
}

int data_cmp(char *data1, char *data2) //对比
{
    return strcmp(data1, data2);
}

/***************信息查找*****************/
void info_find(Node_list *head)
{
    if (head == NULL || head->next == head)
    {
        printf("head is null\n");
        return;
    }
    char info[30] = {0};
    char str2[10] = {0};
    int se = 0;
    int flag = 0; //标记有没有查找到
    printf("|****************************************************************************************|\n");
    printf("\t\t1、航班号  \t2、起点站  \t3、终点站  \t4、班期\n");
    printf("\t\t5、机型    \t6、起飞时间\t7、到达时间\t8、票价\n");
    printf("|****************************************************************************************|\n");
    printf("请输入你要查找哪类信息序号(如:1):");
    scanf("%d", &se);
    if (1 == se)
        strcpy(str2, "航班号");
    if (2 == se)
        strcpy(str2, "起点站");
    if (3 == se)
        strcpy(str2, "终点站");
    if (4 == se)
        strcpy(str2, "班期");
    if (5 == se)
        strcpy(str2, "机型");
    if (6 == se)
        strcpy(str2, "起飞时间");
    if (7 == se)
        strcpy(str2, "到达时间");
    if (8 == se)
        strcpy(str2, "票价");
    printf("请输入你要查找的%s:", str2);
    scanf("%s", info);
    Node_list *temp = head;
    while (temp->next != head)
    {
        temp = temp->next;
        if (1 == se)
        {
            if (!data_cmp(temp->info->number, info))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
        if (2 == se)
        {
            if (!data_cmp(temp->info->staddress, info))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
        if (3 == se)
        {
            if (!data_cmp(temp->info->arraddress, info))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
        if (4 == se)
        {
            if (!data_cmp(temp->info->DATE, info))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
        if (5 == se)
        {
            if (!data_cmp(temp->info->TYPE, info))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
        if (6 == se)
        {
            if (!data_cmp(temp->info->stime, info))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
        if (7 == se)
        {
            if (!data_cmp(temp->info->atime, info))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
        if (8 == se)
        {
            if (!strncmp(temp->info->value, info, strlen(info) - 1))
            {
                printf("|****************************************************************************************|\n");
                show_one(temp->info);
                flag = 1;
            }
        }
    }
    if (0 == flag)
    {
        printf("没有你要查找的信息\n");
    }
    Ulog_flight("查找航班信息");
}

//查找航班号
int find_number(Node_list *head, char *info)
{
    Node_list *temp = head;
    int i = 0;
    while (temp->next != head)
    {
        temp = temp->next;
        if (!strcmp(temp->info->number, info))
        {
            printf("购票成功\n");
            printf("请核对您购买的票\n");
            printf("|****************************************************************************************|\n");
            show_one1(temp->info);
            i++;
            temp->info->poll--;
            return i;
        }
        i++;
    }
    printf("购票失败,没有您要购买的票\n");
    return i;
}

//要排序的元素
Node_list *cmp_find(Node_list *head)
{
    int info = 0;
    printf("|***************************************************|\n");
    printf("\t1、航班号\t2、起飞时间\t3、票价\n");
    printf("|***************************************************|\n");
    printf("请输入你要排序的内容序号:");
    scanf("%d", &info);
    if (1 == info)
    {
        head = sort_number_list(head);
    }

    if (2 == info)
    {
        head = sort_time_list(head);
    }
    if (3 == info)
    {
        head = sort_value_list(head);
    }

    if (info != 1 && info != 2 && info != 3)
        printf("输入错误\n");
    return head;
}

//按航班号排序
Node_list *sort_number_list(Node_list *head)
{
    if (head == NULL || head->next == head)
    {
        perror("head is null\n");
        return NULL;
    }
    int len = length_list(head);
    Node_list *before = head;
    Node_list *temp = NULL;
    Node_list *after = NULL;

    for (int i = 0; i < len - 1; i++)
    {
        before = head;
        for (int j = 0; j < len - i - 1; j++)
        {
            temp = before->next;
            after = temp->next;
            if (data_cmp(temp->info->number, after->info->number) > 0)
            {
                temp->next = after->next;
                temp->next->prev = temp;
                temp->prev = after;
                after->next = temp;
                after->prev = before;
                before->next = after;
            }
            before = before->next;
        }
        before = before->next;
    }
    system("clear");
    printf("排序完成\n");
    Ulog_flight("将航班信息按航班号排序");
    return head;
}

//按航班票价排序
Node_list *sort_value_list(Node_list *head)
{
    if (head == NULL || head->next == head)
    {
        perror("head is null\n");
        return NULL;
    }
    int len = length_list(head);
    Node_list *before = head;
    Node_list *temp = NULL;
    Node_list *after = NULL;

    for (int i = 0; i < len - 1; i++)
    {
        before = head;
        for (int j = 0; j < len - i - 1; j++)
        {
            temp = before->next;
            after = temp->next;
            if (data_cmp(temp->info->value, after->info->value) > 0)
            {
                temp->next = after->next;
                temp->next->prev = temp;
                temp->prev = after;
                after->next = temp;
                after->prev = before;
                before->next = after;
            }
            before = before->next;
        }
        before = before->next;
    }
    system("clear");
    printf("排序完成\n");
    Ulog_flight("将航班信息按票价排序");
    return head;
}

//按起飞时间排序
Node_list *sort_time_list(Node_list *head)
{
    if (head == NULL || head->next == head)
    {
        perror("head is null\n");
        return NULL;
    }
    int len = length_list(head);
    Node_list *before = head;
    Node_list *temp = NULL;
    Node_list *after = NULL;

    for (int i = 0; i < len - 1; i++)
    {
        before = head;
        for (int j = 0; j < len - i - 1; j++)
        {
            temp = before->next;
            after = temp->next;
            if (data_cmp(temp->info->stime, after->info->stime) > 0)
            {
                temp->next = after->next;
                temp->next->prev = temp;
                temp->prev = after;
                after->next = temp;
                after->prev = before;
                before->next = after;
            }
            before = before->next;
        }
        before = before->next;
    }
    system("clear");
    printf("排序完成\n");
    Ulog_flight("将航班信息按起飞时间排序");
    return head;
}

//头插法录入多个航班信息
int rapid_input(Node_list *head)
{
    int i = 0;
    Data_type flight1[11][20] = {{"CZ3355", "武汉", "深圳", "202207", "737", "16:30", "18:25", "553元",50},
                                 {"MF8555", "长沙", "北京", "202207", "736", "16:00", "18:10", "450元",50},
                                 {"CA4308", "广州", "成都", "202207", "321", "14:00", "16:35", "720元",50},
                                 {"CZ4463", "北京", "上海", "202207", "738", "09:45", "11:50", "484元",50},
                                 {"JD5768", "长沙", "三亚", "202207", "320", "12:00", "14:20", "998元",50}};
    for (i = 0; i < 5; i++)
    {
        input_hinfo(head, flight1[i], sizeof(Data_type));
    }
    printf("成功录入%d个信息\n", i);
    Ulog_flight("一起性录入多个信息");
    return 0;
}

//修改
Node_list *revamp_flight_info(Node_list *head)
{
    if (head == NULL || head->next == head)
    {
        perror("head is null\n");
        return head;
    }
    char info[30] = {0};
    int s = 0;
    int loca = 0;
    int flag = 0; //标记有没有查找到
    int len = length_list(head);
    show_all_flight_info(head, show_one);
    printf("请输入你要修改的第几个位置的航班信息:");
    scanf("%d", &loca);
    if (loca <= 0 || loca > len)
    {
        printf("修改失败!!!要修改的位置没数据,请先录入\n");
        return NULL;
    }
    system("clear");
    printf("|****************************************************************************************|\n");
    printf("\t\t1、航班号  \t2、起点站  \t3、终点站  \t4、班期\n");
    printf("\t\t5、机型    \t6、起飞时间\t7、到达时间\t8、票价\n");
    printf("|****************************************************************************************|\n");
    printf("请输入你要修改哪类信息序号(如:1):");
    scanf("%d", &s);
    Node_list *temp = head;
    int i = 0;
    while (i != loca && temp->next != head)
    {
        temp = temp->next;
        i++;
    }
    show_one(temp->info);
    printf("请输入你要将信息修改为:");
    scanf("%s", info);

    if (i == loca)
    {
        if (1 == s)
        {
            strcpy(temp->info->number, info);
            printf("航班号修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        if (2 == s)
        {
            strcpy(temp->info->staddress, info);
            printf("起点站修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        if (3 == s)
        {
            strcpy(temp->info->arraddress, info);
            printf("终点站修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        if (4 == s)
        {
            strcpy(temp->info->DATE, info);
            printf("班期修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        if (5 == s)
        {
            strcpy(temp->info->TYPE, info);
            printf("机型修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        if (6 == s)
        {
            strcpy(temp->info->stime, info);
            printf("起飞时间修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        if (7 == s)
        {
            strcpy(temp->info->atime, info);
            printf("到达时间修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        if (8 == s)
        {
            int i = 0;
            for (i = 0; info[i] != '\0'; i++)
                ;
            strcpy(info + i, "元");
            strcpy(temp->info->value, info);
            printf("票价修改成功\n");
            printf("|****************************************************************************************|\n");
            show_one(temp->info);
            flag = 1;
        }
        printf("第%d个位置的航班系统信息已修改\n", loca);
    }
    if (0 == flag)
    {
        printf("没有你要查找的信息\n");
    }
    Ulog_flight("航班信息修改");
    return head;
}

4、print.h

#ifndef _PRINT_H_
#define _PRINT_H_

void show_one(Data_type *datainfo);//显示一个航班信息
void show_all_flight_info(Node_list *head,showfun_t infoflight);//显示所有航班信息

#endif

5、print.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "include/flight.h"
#include "include/print.h"
#include "include/file.h"

//显示一个信息
void show_one(Data_type *datainfo)
{
    printf("\t%-9s%-10s%-8s%-9s%-9s%-16s%-16s%-8s%d\n", datainfo->number, datainfo->staddress,
           datainfo->arraddress, datainfo->DATE, datainfo->TYPE,
           datainfo->stime, datainfo->atime, datainfo->value,datainfo->poll);
}

void show_one1(Data_type *datainfo)
{
    printf("\t%-9s%-10s%-8s%-9s%-9s%-16s%-16s%-8s\n", datainfo->number, datainfo->staddress,
           datainfo->arraddress, datainfo->DATE, datainfo->TYPE,
           datainfo->stime, datainfo->atime, datainfo->value);
}

//显示所有航班信息
void show_all_flight_info(Node_list *head, showfun_t infoflight)
{
    if (head == NULL || head->next->info == NULL)
    {
        perror("没有航班信息,请先录入!!!\n");
        return;
    }
    Node_list *temp = head;
    printf("|**************************************************************************************************|\n");
    printf("\t航班号\t起点站\t终点站\t班期\t机型\t起飞时间\t到达时间\t  票价\t余票\n");
    printf("|**************************************************************************************************|\n");
    while (temp->next != head)
    {
        temp = temp->next;
        infoflight(temp->info);
    }
    puts("");
    Ulog_flight("查看所有航班信息");
}

6、menu.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "include/flight.h"
#include "include/print.h"
#include "include/file.h"

Node_list *fli_login_interface(Node_list *head);  //登陆界面
void verification_code();                         //验证码
Node_list *Administrator_flight(Node_list *head); //管理员登陆界面
Node_list *Ticket_buyers_flight(Node_list *head); //购票者登陆界面
/**************菜单***************/
Node_list *menu_flight(Node_list *head)
{
    fli_login_interface(head);
    return head;
}

//登录界面
Node_list *fli_login_interface(Node_list *head)
{
    system("clear");
    int se = 0;
    printf("|******************************************|\n");
    printf("|*********   \033[33m 选择登陆界面\033[0m    *************|\n");
    printf("|******************************************|\n");
    printf("|********   1、管理员登陆界面   ***********|\n");
    printf("|********   2、购票者登陆界面   ***********|\n");
    printf("|********   0、退出             ***********|\n");
    printf("|******************************************|\n");
    printf("请选择登录界面:>");
    while (1)
    {
        scanf("%d", &se);
        switch (se)
        {
        case 2:
            head = Ticket_buyers_flight(head);
            return head;
        case 1:
            head = Administrator_flight(head);
            return head;
        case 0:
            return head;
            break;
        default:
            printf("输入错误,请重新输入\n");
        }
    }
}

//管理员登陆界面
Node_list *Administrator_flight(Node_list *head)
{
    printf("正在登陆");
    fflush(stdout);
    sleep(1);
    printf(".");
    fflush(stdout);
    sleep(1);
    printf(".");
    fflush(stdout);
    sleep(1);
    printf(".");
    fflush(stdout);
    sleep(1);
    system("clear");
    verification_code();
    printf("登陆成功\n");
    system("clear");
    char ch;
    int se = 0;
    int loca = 0;
    while (1)
    {
        printf("|**************************************************|\n");
        printf("|**************   \033[34m 管理员登陆界面\033[0m    **************|\n");
        printf("|**************************************************|\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("|********  9、录入多个航班信息           **********|\n");
        printf("|********  10、按位置修改航班信息        **********|\n");
        printf("|********  0、退回登陆界面               **********|\n");
        printf("|**************************************************|\n");
        printf("请输入你的选项:>");
        scanf("%d", &se);
        system("clear");
        int len = 0;
        int flag = 0;
        switch (se)
        {
        case 11:
            super_witre_password();
            flag = super_password();
            if (1 == flag)
            {
                printf("是否清空链表数据(y/n):");
                getchar();
                ch = getchar();
                if (ch == 'y' || ch == 'Y')
                {
                    destroy_list(head);
                    if (head == NULL)
                        head = creat_flight_list();
                }
            }
            else
            {
                printf("\033[31m 密码错误\033[0m \n");
            }
            break;
        case 10:
            head = revamp_flight_info(head);
            break;
        case 9:
            rapid_input(head);
            break;
        case 8:
            cat_log_info();
            break;
        case 7:
            len = length_list(head);
            printf("航班信息个数为%d个\n", len);
            show_all_flight_info(head, show_one);
            break;
        case 6:
            head = cmp_find(head);
            show_all_flight_info(head, show_one);
            break;
        case 5:
            info_find(head);
            break;
        case 4:
            flight_read_file(head);
            printf("文件信息已读入链表中\n");
            break;
        case 3:
            flight_write_file(head);
            printf("链表信息已写入文件中\n");
            break;

        case 2:
            len = length_list(head);
            show_all_flight_info(head, show_one);
            if (head != NULL && head->next != head)
            {
                printf("请输入你要删除的第几个位置的航班信息:");
                scanf("%d", &loca);
            }
            head = delete_loca_flight(head, loca);
            if (head != NULL && loca > 0 && loca <= len)
            {
                system("clear");
                printf("第%d个位置的航班系统信息已删除\n", loca);
                Ulog_flight("按位置删除航班信息");
            }
            show_all_flight_info(head, show_one);
            break;
        case 1:
            head = input_tinfo(head);
            break;
        case 0:
            return fli_login_interface(head);
        default:
            printf("输入错误\n");
            break;
        }
        printf("按 Enter 键继续");
        getchar();
        getchar();
        system("clear");
    }
    return head;
}

//购票者登陆界面
Node_list *Ticket_buyers_flight(Node_list *head)
{
    printf("正在登陆");
    fflush(stdout);
    sleep(1);
    printf(".");
    fflush(stdout);
    sleep(1);
    printf(".");
    fflush(stdout);
    sleep(1);
    printf(".");
    fflush(stdout);
    sleep(1);
    system("clear");
    verification_code();
    printf("登陆成功\n");
    system("clear");
    int se = 0;
    int flag = 0;
    char loca[10];
    char info[10];
    while (1)
    {
        printf("|**************************************************|\n");
        printf("|**************   \033[31m 购票者登陆界面\033[0m    **************|\n");
        printf("|**************************************************|\n");
        printf("|*********  1、指定信息查找航班信息       *********|\n");
        printf("|*********  2、航班信息排序               *********|\n");
        printf("|*********  3、查看所有航班信息           *********|\n");
        printf("|*********  4、购票                       *********|\n");
        printf("|*********  5、退票                       *********|\n");
        printf("|*********  0、退回登陆界面               *********|\n");
        printf("|**************************************************|\n");
        printf("请输入你的选项:>");
        scanf("%d", &se);
        system("clear");
        Node_list *temp = head;
        switch (se)
        {
        case 6:
            printf("请输入你要退票的航班号:");
            scanf("%s", info);
            while (temp->next != head)
            {
                temp = temp->next;
                if (!strcmp(temp->info->number, info))
                {
                    printf("退票成功\n");
                    printf("请核对您退的票\n");
                    printf("|****************************************************************************************|\n");
                    show_one1(temp->info);
                    temp->info->poll++;
                    flag = 1;
                    break;
                }
            }
            if (flag = 0)
            {
                printf("退票失败\n");
            }
            break;
            case 5:
        case 4:
            if (head->next == head)
            {
                printf("暂时没票\n");
                break;
            }
            length_list(head);
            if (head->next != head)
            {
                show_all_flight_info(head, show_one);
                printf("请输入你要购买的航班号:");
                scanf("%s", loca);
                find_number(head, loca);
            }
            Ulog_flight("购票");
            break;
        case 3:
            length_list(head);
            show_all_flight_info(head, show_one);
            break;
        case 2:
            head = cmp_find(head);
            show_all_flight_info(head, show_one);
            break;
        case 1:
            info_find(head);
            break;
        case 0:
            return fli_login_interface(head);
        default:
            printf("输入错误\n");
            break;
        }
        printf("按 Enter 键继续");
        getchar();
        getchar();
        system("clear");
    }
    return head;
}

//验证码
void verification_code()
{
    //验证码
    /***********************************************/
    srand(time(NULL));
    int n = 5;
    char str[10] = {0};
    int i, j, len;
    char str1[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJLMNOPQRSTUVWXYZ";
    len = strlen(str1); //求字符串pstr的长度
    int flag = 0;
    int c = 5; //验证码次数
    char code[10];

    while (c)
    {
        for (i = 0; i < n; i++)
        {
            j = rand() % len; //生成0~len-1的随机数
            str[i] = str1[j];
        }
        str[i] = '\0';
        printf("验证码:%s\n", str);
        /*********************输入验证码**********************/
        printf("请输入验证码:");
        scanf("%s", code);
        c--;
        if (strncasecmp(code, str, 5) == 0)
        { //不区分大小写的验证码
            n = 0;
            flag = 1;
            printf("验证正确.\n");
            break;
        }
        printf("输入错误\n");
        printf("<您还剩%d机会>\n", c);
        sleep(1);
        system("clear");
    }
    if (flag == 0)
        printf("对不起,您的账号已锁定.\n");
}

7、file.h

#ifndef _FILEE_H_
#define _FILEE_H_
#include "flight.h"
#include <stdio.h>

#define flight_info "flight.txt"
#define log "log.txt"

void show_one_file(FILE *fp, Data_type *datainfo); 写一个信息到文件内
int flight_write_file(Node_list *head);            //将航班信息写入文件中
int flight_read_file(Node_list *head);             //将航班文件数据读取到链表中
void Ulog_flight(char *str);                       //日志
void cat_log_info();                               //查看日志

void super_witre_password(); //设置密码
int super_password();        //输入密码
#endif

 

8、file.c


/******************************文件操作*******************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "include/flight.h"
#include "include/print.h"
#include "include/file.h"
/*************日志*****************/
void Ulog_flight(char *str)
{
    FILE *fp = NULL;
    fp = fopen(log, "a");

    time_t timeloca = time(NULL);
    char *timer = ctime(&timeloca);

    fprintf(fp, "%s\t操作内容:%s\n", timer, str);
    fclose(fp);
}

//写一个信息到文件内
void show_one_file(FILE *fp, Data_type *datainfo)
{
    fprintf(fp, "\t%s\t%s\t%s\t%s\t%s\t%6s\t%14s\t%13s\t%d\n", datainfo->number, datainfo->staddress,
            datainfo->arraddress, datainfo->DATE, datainfo->TYPE,
            datainfo->stime, datainfo->atime, datainfo->value,datainfo->poll);
}

//将航班信息写入文件中
int flight_write_file(Node_list *head)
{
    FILE *fp = NULL;
    int len = length_list(head);
    fp = fopen(flight_info, "w");
    Node_list *temp = head;
    fprintf(fp, "\t航班号\t起点站\t终点站\t班期\t机型\t起飞时间\t到达时间\t  票价\t余票\n");
    for (int i = 0; i < len; i++)
    {
        temp = temp->next;
        show_one_file(fp, temp->info);
    }
    fclose(fp);
    Ulog_flight("将航班链表信息已写入文件中");
    return 0;
}

//将航班文件数据读取到链表中
int flight_read_file(Node_list *head)
{
    FILE *fp = NULL;
    fp = fopen(flight_info, "r");
    int count;
    fscanf(fp, "\t航班号\t起点站\t终点站\t班期\t机型\t起飞时间\t到达时间\t  票价\t余票\n");
    Data_type datainfo;
    while (1)
    {
        count = fscanf(fp, "\t%s%s%s%s%s%s%s%s%d\n", datainfo.number, datainfo.staddress,
                       datainfo.arraddress, datainfo.DATE, datainfo.TYPE,
                       datainfo.stime, datainfo.atime, datainfo.value,datainfo.poll);
        if (count < 8)
        {
            break;
        }
        else
        {
            input_hinfo(head, &datainfo, sizeof(Data_type));
        }
    }
    fclose(fp);
    return 0;
    /****************日志****************/
    Ulog_flight("将航班文件数据读取到链表中");
}

void super_witre_password()
{
    FILE *fp = NULL;
    fp = fopen("password.txt", "w");
    fprintf(fp, "超级用户密码:");
    fprintf(fp, "123");
    fclose(fp);
}

//超级管理员密码
int super_password()
{
    FILE *fp = NULL;
    int flag = 0;
    char str[10] = {0};
    char str1[10] = {0};
    fp = fopen("password.txt", "r");
    fscanf(fp, "超级用户密码:");
    fscanf(fp, "%s", str);
    fclose(fp);
    printf("\033[44m 请输入超级管理员密码:\033[0m");
    // system("stty -echo");//隐藏输入,输入时看不然入root权限密码
    scanf("%s", str1);
    // system("stty echo");
    system("clear");
    if (!strcmp(str, str1))
    {
        flag = 1;
    }
    return flag;
}

//查看日志信息
void cat_log_info()
{
    FILE *fp = NULL;
    char str[128] = {0};
    fp = fopen(log, "r");
    fgets(str, 127, fp);
    while (!feof(fp))
    {
        fputs(str, stdout);
        fgets(str, 127, fp);
    }
    fputs(str, stdout);
    fclose(fp);
    Ulog_flight("查看日志");
}

9、makefile(不使用变量)

edit:main.o menu.o flight.o file.o print.o 
	gcc main.o menu.o flight.o file.o print.o -o a.out
main.o:main.c 
	gcc -Wall -o -g -c main.c -o main.o
menu.o:menu.c 
	gcc -Wall -o -g -c menu.c -o menu.o
flight.o:flight.c 
	gcc -Wall -o -g -c flight.c -o flight.o
file.o:file.c 
	gcc -Wall -o -g -c file.c -o file.o
print.o:print.c  
	gcc -Wall -o -g -c print.c -o print.o
clean:
	rm *.o a.out

10、makefile(使用一个变量)

OBJS = main.o file.o print.o menu.o flight.o

edit:$(OBJS)
	gcc -o edit $(OBJS)
main.o:main.c 
	gcc -Wall -o -g -c main.c -o main.o
menu.o:menu.c 
	gcc -Wall -o -g -c menu.c -o menu.o
flight.o:flight.c 
	gcc -Wall -o -g -c flight.c -o flight.o
file.o:file.c 
	gcc -Wall -o -g -c file.c -o file.o
print.o:print.c  
	gcc -Wall -o -g -c print.c -o print.o
.PHONY:clean
clean:
	rm $(OBJS) edit

11、makefile(使用多个变量)

OBJS = main.o file.o print.o menu.o flight.o
CC = gcc
CFLAGS = -Wall -o -g

edit : $(OBJS)
	$(CC) $^ -o $@
main.o:main.c 
	$(CC) $(CFLAGS) -c $< -o $@
menu.o:menu.c 
	$(CC) $(CFLAGS) -c $< -o $@
flight.o:flight.c 
	$(CC) $(CFLAGS) -c $< -o $@
file.o:file.c 
	$(CC) $(CFLAGS) -c $< -o $@
print.o:print.c  
	$(CC) $(CFLAGS) -c $< -o $@
.PHONY:clean
clean:
	rm $(OBJS) edit

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值