机票管理系统(机票删减部分)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define MAX 25
char fpname[] = "flynote.txt";
char hfpname[] = "hjipiao.txt";
typedef struct
{
    int year;
    int month;
    int day;
    int hour;
    int minute;
} mytime;
typedef struct
{
    char fname[MAX];
    mytime stime;
    char splace[MAX];
    char eplace[MAX];
    double flytime;
    int capacity;
    int pcount;
    char remark[MAX];
} Note;
Note arr[102];
void startSystem();
int timecheck(mytime tp)//返回1正确,返回0错误
{
    if(tp.year > 2018)
        return 0;
    int iArr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//每月天数
    if ((tp.year % 4 == 0 && tp.year % 100 != 0) || tp.year % 400 == 0)//闰年判断
    {
        if(tp.month < 1 || tp.month >12)
        {
            return 0;
        }
        else
        {
            iArr[1] = 29;
            if(tp.day < 1 || tp.day > iArr[tp.month-1])
                return 0;
        }
    }
    else
    {
        if(tp.month < 1 || tp.month >12)
        {
            return 0;
        }
        else
        {
            if(tp.day < 1 || tp.day > iArr[tp.month-1])
                return 0;
        }
    }
    if(tp.hour < 0 || tp.hour > 23)
        return 0;
    if(tp.minute > 60 || tp.minute < 0)
        return 0;
    return 1;
}
int timecom(mytime a, mytime b)//时间比较,a > b 返回1,a<b返回-1,a==b返回0
{
    if(a.year > b.year)
        return 1;
    else if(a.year < b.year)
        return -1;
    else if(a.year == b.year)
    {
        if(a.month > b.month)
            return 1;
        else if(a.month < b.month)
            return -1;
        else if(a.month == b.month)
        {
            if(a.day > b.day)
                return 1;
            else if(a.day < b.day)
                return -1;
            else if(a.day == b.day)
            {
                if(a.hour > b.hour)
                    return 1;
                else if(a.hour < b.hour)
                    return -1;
                else
                {
                    if(a.minute > b.minute)
                        return 1;
                    else if(a.minute < b.minute)
                        return -1;
                    else if(a.minute == b.minute)
                        return 0;
                }
            }
        }
    }
}
void writeInfo(FILE *fp, Note tp)//向文件中写入一行信息
{
    fprintf(fp, "%s ", tp.fname);
    fprintf(fp, "%d %d %d %d %d ",
            tp.stime.year,
            tp.stime.month,
            tp.stime.day,
            tp.stime.hour,
            tp.stime.minute);
    fprintf(fp, "%s %s %lf %d %d %s",
            tp.splace, tp.eplace,
            tp.flytime, tp.capacity,
            tp.pcount, tp.remark);
    fprintf(fp, "\n");
}
Note readInfo(FILE *fp)//从文件中读出一行文件
{
    Note tp;
    fscanf(fp, "%s ", tp.fname);
    fscanf(fp, "%d %d %d %d %d ",
           &tp.stime.year,
           &tp.stime.month,
           &tp.stime.day,
           &tp.stime.hour,
           &tp.stime.minute);
    fscanf(fp, "%s %s %lf %d %d %s",
           tp.splace, tp.eplace,
           &tp.flytime,
           &tp.capacity, &tp.pcount,
           tp.remark);
    //fprintf(fp, "\n");
    return tp;
}
void printInfo(Note tp)//向控制台写入一张机票信息
{
    printf("%s ", tp.fname);
    printf("%02d/%02d/%02d/%02d:%02d ",
           tp.stime.year,
           tp.stime.month,
           tp.stime.day,
           tp.stime.hour,
           tp.stime.minute);
    printf("\t%s\t%s\t\%lf %d\t%d\t%s",
           tp.splace, tp.eplace,
           tp.flytime, tp.capacity,
           tp.pcount, tp.remark);
    printf("\n");
}
void getNote()//输出机票信息
{
    printf("\n\n");
    printf("                             机票信息\n");
    printf("\n\n");
    printf("航班号\t起飞时间\t起点\t终点\t飞行时间 容量 订票人数 备注\n");
    printf("-------------------------------------------------------------------\n");
    FILE *fp;
    fp = fopen(fpname, "r");
    if(!fp)
    {
        system("cls");
        printf("\n\n\n打开文件失败,请检查是否存在文件\n\n");
        printf("-----------即将重新启动系统---------------\n");
        Sleep(2000);
        system("cls");
        startSystem();
    }
    Note tp;
    while(!feof(fp))
    {
        tp = readInfo(fp);
        printInfo(tp);
    }
    fclose(fp);
    printf("-------------------------------------------------------------------\n");
    printf("\n\n");
    printf("输入1返回起始界面\n");
    int cho;
    while(1)
    {
        printf("请输入:");
        scanf("%d", &cho);
        if(cho == 1)
        {
            system("cls");
            startSystem();
        }
        else
        {
            MessageBox(NULL, "不存在此选项,请重新输入", "警告", MB_OK);
            continue;
        }
    }
}
void putNote()//写入机票信息
{
    FILE *fp;
    fp = fopen(fpname, "a");
    Note tp;
    char t;
    printf("\n\n");
    printf("                   请输入航班内容(以end结束输入)\n\n\n");

    printf("航班号\t起飞时间\t起点\t终点\t飞行时间 容量 订票人数 备注\n");
    while(1)
    {
        printf("-------------------------------------------------------------------\n");
        scanf("%s", tp.fname);
        if(strcmp(tp.fname, "end") == 0)
            break;
        scanf("%d%c%d%c%d%c%d%c%d",
              &tp.stime.year, &t,
              &tp.stime.month, &t,
              &tp.stime.day, &t,
              &tp.stime.hour, &t,
              &tp.stime.minute);
        if(!timecheck(tp.stime))
        {
            printf("时间输入错误,请重新输入\n");
            scanf("%s %s %lf %d %d %s",
                  tp.splace, tp.eplace,
                  &tp.flytime, &tp.capacity,
                  &tp.pcount, tp.remark);
            continue;
        }
        scanf("%s %s %lf %d %d %s",
              tp.splace, tp.eplace,
              &tp.flytime, &tp.capacity,
              &tp.pcount, tp.remark);

        writeInfo(fp, tp);
    }
    fclose(fp);
    //printf("-------------------------------------------------------------------\n");
    printf("\n\n");
    printf("------------------------------输入结束------------------------------\n");

    printf("\n\n");
    printf("输入1返回上一层\n");
    while(1)
    {
        int cho;
        printf("请输入:");
        scanf("%d", &cho);
        if(cho == 1)
        {
            system("cls");
            startSystem();
        }
        else
        {
            MessageBox(NULL, "不存在此选项,请重新输入", "警告", MB_OK);
            continue;
        }
    }

}
void removeNote()//删除信息
{
    FILE *fp;
    fp = fopen(fpname, "r");
    if(!fp)
    {
        system("cls");
        printf("\n\n\n打开文件失败,请检查是否存在文件\n\n");
        printf("-----------即将重新启动系统---------------\n");
        Sleep(2000);
        system("cls");
        startSystem();
    }
    char flyname[25];
    Note arr[102];
    mytime temp;
    printf("请输入你要删除的航班号和该航班的时间:\n");
    printf("航班号:");
    scanf("%s", flyname);
    printf("时间:");
    char t;
    scanf("%d%c%d%c%d%c%d%c%d",
          &temp.year,
          &t, &temp.month,
          &t, &temp.day,
          &t, &temp.hour,
          &t, &temp.minute);

    int i = 0;
    int flag = -1;
    FILE *hfp;
    hfp = fopen(hfpname, "wa");
    while(!feof(fp))
    {
        arr[i] = readInfo(fp);
        if(strcmp(arr[i].fname, flyname) == 0 && timecom(arr[i].stime, temp) == 0)
        {
            flag = i;
        }
        i++;
    }
    if(flag == -1)
    {
        printf("不存在这张机票\n");
        printf("请重新输入");
        int j = 0;
        for(j = 0; j < 3; j++)
        {
            printf("..");
            Sleep(200);
        }
        system("cls");
        removeNote();
        return;
    }
    writeInfo(hfp, arr[flag]);
    fclose(hfp);
    fclose(fp);
    fp = fopen(fpname, "w");
    int len = i - 1;
    for(i = 0; i < len; i++)
    {
        if(i != flag)
        {
            writeInfo(fp, arr[i]);
        }
    }
    fclose(fp);
    printf("删除成功\n");
    printf("请按1返回上一层,按2继续删除\n");
    int cho;
    while(1)
    {
        scanf("%d", &cho);
        if(cho == 1)
        {
            system("cls");
            startSystem();
        }
        else if(cho == 2)
        {
            system("cls");
            removeNote();
        }
        else
        {
            MessageBox(NULL, "不存在此选项,请重新输入", "警告", MB_OK);
            continue;
        }
    }
}
void removeRecord()
{
    FILE *hfp;
    hfp = fopen(hfpname, "r");
    Note aa[105];
    if(!hfp)
    {
        system("cls");
        printf("\n\n\n打开文件失败,请检查是否存在文件\n\n");
        printf("-----------即将重新启动系统---------------\n");
        Sleep(2000);
        system("cls");
        startSystem();
    }
    int i = 0;
    while(!feof(hfp))
    {
        aa[i++] = readInfo(hfp);
    }
    fclose(hfp);
    int len = i - 1;
    printf("\n\n");
    printf("                              机票删除记录\n\n");
    for(i = 0; i < len; i++)
    {
        printf("-----------------------------------------------------------------------\n");
        printInfo(aa[i]);
    }
    printf("\n\n\n----------------------------------结束----------------------------------\n\n");
    int cho;
    printf("按1返回上一层\n");

    while(1)
    {
        printf("请输入:");
        scanf("%d", &cho);
        if(cho == 1)
        {
            system("cls");
            startSystem();
            break;
        }
        else
        {
            MessageBox(NULL, "不存在此选项,请重新输入", "警告", MB_OK);
            continue;
        }
    }
}
void sp(Note *a, Note *b)//a, b交换
{
    Note t;
    t = *a;
    *a = *b;
    *b = t;
}
void heapAdjust(int i, int n)//堆调整
{
    if (n == 0)return;
    if (i <= n / 2)//如果是叶节点就不用进行调整
    {
        int min = i;
        int lchild = i * 2;
        int rchild = i * 2 + 1;
        if (lchild <= n && timecom(arr[min].stime, arr[lchild].stime) > 0)
            min = lchild;
        if (rchild <= n && timecom(arr[min].stime, arr[rchild].stime) > 0)
            min = rchild;
        if (min != i)
        {
            sp(&arr[i], &arr[min]);
            heapAdjust(min, n);
        }
    }

}
void heapBuild(int n)//堆建立
{
    int i;
    for (i = n / 2; i >= 1; i--)//非叶节点最大序号为size/2
    {
        heapAdjust(i, n);
    }
}
void heapSort(int n)//堆排序
{
    int i;
    for (i = n; i >= 1; i--)
    {
        sp(&arr[1], &arr[i]);
        heapAdjust(1, i - 1);
    }
}
void sortNote()//机票按照时间排序
{
    FILE *fp;
    fp = fopen(fpname, "r");
    if(!fp)
    {
        system("cls");
        printf("\n\n\n打开文件失败,请检查是否存在文件\n\n");
        printf("-----------即将重新启动系统---------------\n");
        Sleep(2000);
        system("cls");
        startSystem();
    }
    printf("开始排序");
    printf("\n\n");
    printf("排序正在进行中");
    int i;
    for(i = 0; i < 30; i++)
    {
        printf("/");
        Sleep(100);
    }
    printf("\n\n");
    printf("排序完成\n\n");


    i = 0;
    while(!feof(fp))
    {
        arr[i++] = readInfo(fp);
    }
    fclose(fp);
    int len = i - 1;
    heapBuild(len);
    heapSort(len);
    fp = fopen(fpname, "w");
    for(i = 0; i < len; i++)
    {
        writeInfo(fp, arr[i]);
    }
    fclose(fp);
    int cho;
    printf("按1返回上一层\n");

    while(1)
    {
        printf("请输入:");
        scanf("%d", &cho);
        if(cho == 1)
        {
            system("cls");
            startSystem();
            break;
        }
        else
        {
            MessageBox(NULL, "不存在此选项,请重新输入", "警告", MB_OK);
            continue;
        }
    }
}
void startSystem()//启动系统
{
    printf("\n\n");
    printf("                   *****************************************\n");
    printf("                   *****************************************\n\n");
    printf("                   *           机票管理系统                *\n\n");
    printf("                   *****************************************\n");
    printf("                   *****************************************\n");
    printf("\n\n");
    printf("正在加载");
    int i;
    for(i = 0; i < 8; i++)
    {
        printf("..");
        Sleep(200);
    }
    system("cls");
    printf("\n\n");
    printf("                ---------------------功能选择---------------------\n");
    int cho;
    printf("\n\n");
    printf("                     1.添加机票\n\n");
    printf("                          2.删除机票\n\n");
    printf("                                3.显示机票\n\n");
    printf("                                      4.主界面\n\n");
    printf("                                           5.机票排序\n\n");
    printf("                                                 6.删除记录\n\n");
    while(1)
    {
        printf("请选择:");
        scanf("%d", &cho);
        if(cho == 1)
        {
            system("cls");
            putNote();
        }
        else if(cho == 2)
        {
            system("cls");
            removeNote();
        }
        else if(cho == 3)
        {
            system("cls");
            getNote();
        }
        else if(cho == 4)
        {
            system("cls");
            printf("此功能暂未开通\n");
            printf("即将返回主界面");
            Sleep(600);
            system("cls");
            startSystem();
        }
        else if(cho == 5)
        {
            system("cls");
            sortNote();
            break;
        }
        else if(cho == 6)
        {
            system("cls");
            removeRecord();
            break;
        }
        else
        {
            MessageBox(NULL, "不存在此选项,请重新输入", "警告", MB_OK);
            continue;
        }
    }
}

int main()
{
    startSystem();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值