C++实现飞机订票系统

本文为大家分享了C++实现飞机订票系统的具体代码,供大家参考。

// 飞机订票系统.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<conio.h>
#define N 2

typedef struct plane
{
    char ID[10];
    char Start_Place[10];
    char End_Place[10];
    float price;
    int Buy_num;
    int Max_num;
    char time[20];
}PLANE;
PLANE p[N];

void menu();
void menu1();

void count()
{
    char a[10];
    int i, num = 0;
    printf("请您输入要统计的飞机目的地:");
    scanf("%s", a);
    printf("航班代号     起始地点    目的地    票价    已售     乘客容量 时间/n");
    for (i = 0; i<N; i++)
    {
        if (strcmp(a, p[i].End_Place) == 0)
        {
            num++;
            printf("%s    %s    %s    %7.2f    %d    %d    %d/n", p[i].ID, p[i].Start_Place, p[i].End_Place, p[i].price, p[i].Buy_num, p[i].Max_num, p[i].time);
        }
    }
    printf("/n您想统计的航班有%d条!/n", num);
    getchar();
    menu1();

}

void browse()
{
    int i;
    system("cls");
    printf("浏览航班信息:/n");
    printf("航班代号     起始地点    目的地    票价    已售     乘客容量 时间/n");
    for (i = 0; i<N; i++)
    {
        printf("%s    %s    %s    %7.2f    %d    %d    %s/n", p[i].ID, p[i].Start_Place, p[i].End_Place, p[i].price, p[i].Buy_num, p[i].Max_num, p[i].time);
    }
    getchar();
    menu();

}

void order()
{
    int i, j;
    PLANE temp;
    for (i = 0; i<N; i++)
    {
        for (j = i; j<N; j++)
        {
            if (strcmp(p[i].time, p[j].time)>0)
            {
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }

        }
    }
    browse();


}
void enter()
{
    int i;
    system("cls");
    printf("请您输入航班信息:");
    for (i = 0; i<N; i++)
    {
        printf("航班代号:");
        scanf("%s", p[i].ID);
        printf("起飞地点:");
        scanf("%s", p[i].Start_Place);
        printf("目的地点:");
        scanf("%s", p[i].End_Place);
        printf("票价:");
        scanf("%f", &p[i].price);
        p[i].Buy_num = 0;
        printf("乘客容量:");
        scanf("%d", &p[i].Max_num);
        printf("起飞时间:");
        scanf("%s", p[i].time);
    }

    getchar();
    menu();
}

void buy()
{
    char t[20], place[20];
    int i, w = 0, num;
    printf("时间");
    scanf("%s", t);
    printf("mudidi");
    scanf("%s", place);
    for (i = 0; i<N; i++)
    {
        if (strcmp(t, p[i].time) == 0 && strcmp(place, p[i].End_Place) == 0)
        {
            w = 1;
            printf("%s    %s    %s    %7.2f    %d    %d    %s/n", p[i].ID, p[i].Start_Place, p[i].End_Place, p[i].price, p[i].Buy_num, p[i].Max_num, p[i].time);
            printf("买几张票!");
            scanf("%d", &num);
            if (p[i].Buy_num + num>p[i].Max_num)
            {
                printf("当前余票不足,只剩%d张", p[i].Max_num - p[i].Buy_num);
            }
            else
            {
                p[i].Buy_num = p[i].Buy_num + num;
                printf("购票成功!");
            }
        }

    }
    if (w == 0)
        printf("没有您要得票!");
    getchar();
    menu1();

}
void back()
{
    char id[20];
    int i, w = 0;
    printf("id:");
    scanf("%s", id);
    for (i = 0; i<N; i++)
    {
        if (strcmp(id, p[i].ID) == 0)
        {
            p[i].Buy_num--;
            printf("退票成功");
            w = 1;
        }
    }
    if (w == 0)
        printf("退票失败");
    getchar();
    menu1();
}
void bowse_time()
{
    char a[20];
    int i, w = 0;
    printf("请您输入要查询的飞机起飞时间:");
    scanf("%s", a);
    printf("航班代号     起始地点    目的地    票价    已售     乘客容量 时间/n");
    for (i = 0; i<N; i++)
    {
        if (strcmp(a, p[i].time) == 0)
        {
            w = 1;
            printf("%s    %s    %s    %7.2f    %d    %d    %s/n", p[i].ID, p[i].Start_Place, p[i].End_Place, p[i].price, p[i].Buy_num, p[i].Max_num, p[i].time);
        }
    }
    if (w == 0)
        printf("没有您想查询的航班!/n");
    getchar();
    menu1();

}
void bowse_palce()
{
    char a[10];
    int i, w = 0;
    printf("请您输入要查询的飞机目的地:");
    scanf("%s", a);
    printf("航班代号     起始地点    目的地    票价    已售     乘客容量 时间/n");
    for (i = 0; i<N; i++)
    {
        if (strcmp(a, p[i].End_Place) == 0)
        {
            w = 1;
            printf("%s    %s    %s    %7.2f    %d    %d    %d/n", p[i].ID, p[i].Start_Place, p[i].End_Place, p[i].price, p[i].Buy_num, p[i].Max_num, p[i].time);
        }
    }
    if (w == 0)
        printf("没有您想查询的航班!/n");
    getchar();
    menu1();
}
void bowse_ID()
{
    char a[10];
    int i, w = 0;
    printf("请您输入要查询的飞机航班号:");
    scanf("%s", a);
    printf("航班代号     起始地点    目的地    票价    已售     乘客容量 时间/n");
    for (i = 0; i<N; i++)
    {
        if (strcmp(a, p[i].ID) == 0)
        {
            w = 1;
            printf("%s    %s    %s    %7.2f    %d    %d    %d/n", p[i].ID, p[i].Start_Place, p[i].End_Place, p[i].price, p[i].Buy_num, p[i].Max_num, p[i].time);
        }
    }
    if (w == 0)
        printf("没有您想查询的航班!/n");
    getchar();
    menu1();

}
void menu1()
{
    int n, w1;
    do
    {
        system("cls");
        puts("/t/t/t/t    飞机售票操作!/n/n");
        puts("/t/t*********************MENU*********************/n/n");
        puts("/t/t/t/t1.Buy ticket");
        puts("/t/t/t/t2.return ticket");
        puts("/t/t/t/t3.bowse by time");
        puts("/t/t/t/t4.bowse by end palce");
        puts("/t/t/t/t5.bowse by plane ID");
        puts("/t/t/t/t6.return to menu");
        puts("/n/n/t/t**********************************************/n");
        printf("Choice your number(1-6): [ ]/b/b");
        scanf("%d", &n);
        if (n<0 || n>6)
        {
            w1 = 1;
            printf("your choice is not between 1 and 4,Please input again:");
            getchar();
        }
        else    w1 = 0;
    } while (w1 == 1);

    switch (n)
    {
    case 1:buy(); break;
    case 2:back(); break;
    case 3:bowse_time(); break;
    case 4:bowse_palce(); break;
    case 5:bowse_ID(); break;
    case 6:menu();
    }

}
 int save()
{
    int w = 1;
    FILE *fp;
    int i;
    system("cls");
    if ((fp = fopen("c://ticket.txt", "wb")) == NULL)
    {
        printf("/nCannot open file/n");
        return NULL;
    }
    for (i = 0; i<N; i++)
    if (fwrite(&p[i], sizeof(struct plane), 1, fp) != 1)
    {
        printf("file write error/n");
        w = 0;
    }
    if (w == 1)
    {
        printf("file save ok!/n");
    }
    fclose(fp);
    getchar();
    menu();
}

int load()
{
    FILE *fp;
    int i, w;
    w = 1;
    system("cls");
    if ((fp = fopen("c://ticket.txt", "rb")) == NULL)
    {
        printf("/nCannot open file/n");
        w = 0;
        return NULL;
    }
    for (i = 0; !feof(fp); i++)
    {
        fread(&p[i], sizeof(struct plane), 1, fp);
    }
    fclose(fp);
    if (w == 1)
        printf("Load file ok!");
    getchar();
    menu();

}
void main()
{
    menu();
}
void menu()
{
    int n, w1;
    do
    {
        system("cls");
        puts("                     飞机售票系统   ");
        puts("*************************MENU*************************");
        puts("    1.  Enter new plane");
        puts("    2.  Browse all");
        puts("    3.  opreration");
        puts("    4.  Save file");
        puts("    5.  Load file");
        puts("    6.  Exit");
        puts("******************************************************");
        printf("Choice your number(1-6): ");
        scanf("%d", &n);
        if (n<0 || n>6)
        {
            w1 = 1;
            printf("your choice is not between 1 and 6,Please input again:");
            getchar();
        }
        else    w1 = 0;
    } while (w1 == 1);

    switch (n)
    {
    case 1: enter(); break;
    case 2: browse(); break;
    case 3: menu1(); break;
    case 4: save(); break;
    case 5: load(); break;
    case 6: exit(0);
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程教程

 define和const使用 const和define区别#include <stdio.h>#define A 1#define B (A+3) //#de ...