数据结构课设作业-----飞机订票系统

#include <iostream>
#include<iomanip>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<stdlib.h>
#include <conio.h>
using namespace std;
FILE *fp;
int count=0;
int user=0;
int inf=0;
struct plane {
    int ID;
    char time[20];
    char data[10];
    char start[10];
    char end[10];
    int pnum;
    int price;
data[100];
struct people {
    int bill;
    char name[10];
    char card[10];
    int ticket;
    int airID;
pass[100];

void Login();
void Search();
void Book();
void Back();
void bill();
void drop();
void menu();
void admire();
void input();
void fix();
void Delete();
void Search_end();
void Search_id();
int readData();
void passager();
int userData();
int userWrite();
int account;
void admire() {
    int n;
    printf("-------欢迎使用订票管理系统--------\n");
    printf("|           1.航班信息录入      |\n");
    printf("|           2.航班信息修改      |\n");
    printf("|           3.航班乘客信息      |\n");
    printf("|           4.返回主菜单        |\n");
    printf("|           0.退出              |\n");
    printf("+----------------------------------+\n");
    printf("请输入操作指令:");
    cin>>n;
    switch(n) {
        case 1:
            input();
            break;
        case 2:
            fix();
            break;
        case 3:
            passager();
            break;
        case 4:
            menu();
            break;
        case 0:
            drop();
            break;
        default:
            printf("输入错误,请重新输入");
            admire();
            break;
    }
}
int userData() {
    int i=0;
    if((fp=fopen("user.txt","r+"))==NULL) {
        return 0;
    }
    while(fscanf(fp,"%d %s %s %d %d",&pass[i].bill,pass[i].name,pass[i].card,&pass[i].ticket,&pass[i].airID)) {
        i++;
        user++;
    }
    fclose(fp);
    return 0;
}
int readData() {
    int i=0;
    if((fp=fopen("plane.txt","r+"))==NULL) {
        return 0;
    }
    while(fscanf(fp,"%d %s %s %s %s %d %d",&data[i].ID,data[i].time,data[i].data,data[i].start,data[i].end,&data[i].pnum,&data[i].price)) {
        i++;
        count++;
    }
    fclose(fp);
    return 0;
}
int writeData() {
    int i;
    if((fp=fopen("plane.txt","w"))==NULL) {
        return 0;
    }
    for(i=0; i<count; i++) {
        fprintf(fp,"%d %s %s %s %s %d %d\n",data[i].ID,data[i].time,data[i].data,data[i].start,data[i].end,data[i].pnum,data[i].price);
    }
    fclose(fp);
    return 0;
}
int userWrite() {
    int i;
    if((fp=fopen("user.txt","w"))==NULL) {
        return 0;
    }
    for(i=0; i<user; i++) {
        fprintf(fp,"%d %s %s %d %d\n",pass[i].bill,pass[i].name,pass[i].card,pass[i].ticket,pass[i].airID);
    }
    fclose(fp);
    return 0;
}
void menu() {
    account=0;
    int 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",&n);
    switch(n) {
        case 0:
            Login();
            break;
        case 1:
            Search();
            break;
        case 2:
            Book();
            break;
        case 3:
            Back();
            break;
        case 4:
            bill();
            break;
        case 5:
            drop();
            break;
        default:
            printf("输入错误,请重新输入");
            menu();
            break;
    }
}
void Login() {
    int i;
    printf("请输入账号:");
    scanf("%d",&account);
    if (account==1) {
        printf("\n");
        admire();
    }
}
void Search() {
    int choice;
    int air_id,i=0,sum=0;
    printf("***************************************************\n");
    printf("        1、按航班号查询     2、按抵达地点查询      \n");
    printf("***************************************************\n");
    printf("请输入查询方式:");
    cin>>choice;
    if(choice==1)  Search_id();
    else if(choice==2)  Search_end();
    else {
        printf("请重新选择\n");
        system("cls");
        Search();
    }
}
int binary_research(int low,int high,int element) {
    while(low<=high) {
        int mid = (low+high)/2;
        if(data[mid].ID>element) {
            high = mid - 1;
        } else if(data[mid].ID<element) {
            low = mid + 1;
        } else {
            return mid;
        }
    }
}
void Search_id() {
    int id,c=count;
    int i=-1, w = 0;
    printf("请您输入要查询的飞机航班号:");
    cin>>id;
    system("cls");
    i=binary_research(0,c,id);
    if (i== -1)
        printf("没有您想查询的航班!\n");
    if (i<-1)
        printf("航班号错误!\n");
        else {
            cout<<"航班ID"<<setw(10)<<"起降时间"<<setw(10)<<"起飞日期"<<setw(10)<<"起点"<<setw(8)<<" 终点"<<setw(8)<<"价格"<<setw(8)<<"剩余票数"<<endl; 
            cout<<data[i].ID<<setw(10)<<data[i].time<<setw(10)<<data[i].data<<setw(10)<<data[i].start<<setw(8)<<data[i].end<<setw(8)<<data[i].pnum<<setw(8)<<data[i].price<<endl;
        }
    system("pause");
    menu();
}
void Search_end() {
    char area[20];
    int i, w = 0;
    printf("请您输入要前往的目的地:");
    scanf("%s", area);
    cout<<"航班ID"<<setw(10)<<"起降时间"<<setw(10)<<"起飞日期"<<setw(10)<<"起点"<<setw(8)<<" 终点"<<setw(8)<<"价格"<<setw(8)<<"剩余票数"<<endl; 
    for(i=0; i<count; i++) {
        if(strcmp(area,data[i].end)==0) {
            w=1;
            cout<<data[i].ID<<setw(10)<<data[i].time<<setw(10)<<data[i].data<<setw(10)<<data[i].start<<setw(8)<<data[i].end<<setw(8)<<data[i].pnum<<setw(8)<<data[i].price<<endl;
        }
    }
    printf("\n");
    if (w==0)
        printf("没有您想查询的航班!\n");
    menu();
}
void Book() {
    char end[20];
    char name[10];
    char card[10];
    int i, w = 0,p,num,s;
    printf("请您输入要前往的目的地:");
    scanf("%s", end);
    cout<<"航班ID"<<setw(10)<<"起降时间"<<setw(10)<<"起飞日期"<<setw(10)<<"起点"<<setw(8)<<" 终点"<<setw(8)<<"价格"<<setw(8)<<"剩余票数"<<endl; 
    for(i=0; i<count; i++) {
        if(strcmp(end,data[i].end)==0&&data[i].pnum!=0) {
            w=1;
            cout<<data[i].ID<<setw(10)<<data[i].time<<setw(10)<<data[i].data<<setw(10)<<data[i].start<<setw(8)<<data[i].end<<setw(8)<<data[i].pnum<<setw(8)<<data[i].price<<endl;
        }
    }
    if(w!=0) {
        printf("请选择您的航班ID:");
        cin>>p;
        for(i=0; i<count; i++) {
            if(p==data[i].ID) {
                printf("请输入你的名字:");
                cin>>pass[user].name;
                printf("请输入你的证件号:");
                cin>>pass[user].card;
                printf("请输入购票数");
                cin>>num;
                printf("确认购票请支付%d元,请按1确认:",data[i].price*num);
                cin>>s;
                if(s==1) {
                    cout<<"购买成功!"<<endl;
                }
                while(s!=1) {
                    cout<<"输入错误,请重新输入!\n";
                    cin>>s;
                    if(s==1) {
                        cout<<"购买成功!"<<endl;
                        break;
                    }
                }
                data[i].pnum-=num;
                writeData();
                pass[user].bill=user+1009;
                pass[user].ticket=num;
                pass[user].airID=data[i].ID;
                user++;
                userWrite();
            }
        }
    }
    printf("\n");
    if (w==0)
        printf("没有您想查询的航班!\n");
    system("pause");
    menu();
}
void Back() {
    char name[10];
    char card[10];
    int b,y;
    int i,w=0;
    printf("请输入您的姓名:");
    cin>>name;
    printf("请输入您的证件号:");
    scanf("%s",card);
    for(i=0; i<user; i++)
        if(strcmp(name,pass[i].name)==0&&pass[i].ticket!=0&&strcmp(card,pass[i].card)==0) {
            if(w==0)
            cout<<"乘客订单"<<setw(10)<<"姓名"<<setw(8)<<"身份证号"<<setw(10)<<"票数"<<setw(8)<<"航班号"<<endl;
            cout<<pass[i].bill<<setw(10)<<pass[i].name<<setw(8)<<pass[i].card<<setw(10)<<pass[i].ticket<<setw(8)<<pass[i].airID<<endl;
            w++;
        }
    if(w==0) {
        printf("对不起,不存在该乘客!\n");
    } else {
        printf("请输入订单:");
        cin>>b;
        int j,t;
        for(i=0; i<user; i++)
            if(b==pass[i].bill) {
                t=i;
                printf("输入1确认退票:");
                cin>>y;
                if(y==1) {
                    for(j=0; j<count; j++) {
                        if(data[j].ID==b) {
                            data[j].pnum+=pass[i].ticket;
                            writeData();
                        }
                    }
                    pass[t].ticket=0;
                    pass[t].airID=0;
                    userWrite();
                    printf("退票成功!\n");
                }
            }
    }
    menu();
}
void bill() {
    char name[10];
    char card[10];
    int i,w=0;
    printf("请输入您的姓名:");
    cin>>name;
    printf("请输入您的证件号:");
    scanf("%s",card);
    for(i=0; i<user; i++)
        if(strcmp(name,pass[i].name)==0&&pass[i].ticket!=0&&strcmp(card,pass[i].card)==0) {
            if(w==0)
                cout<<"乘客订单"<<setw(10)<<"姓名"<<setw(8)<<"身份证号"<<setw(10)<<"票数"<<setw(8)<<"航班号"<<endl;
                cout<<pass[i].bill<<setw(10)<<pass[i].name<<setw(8)<<pass[i].card<<setw(10)<<pass[i].ticket<<setw(8)<<pass[i].airID<<endl;
            w=1;
        }
    if(w==0) {
        printf("对不起,不存在该乘客!\n");
    }
    printf("\n");
    menu();
}
void drop() {
    printf("|+---------------------------+|\n");
    printf("|         感谢您的使用        |\n");
    printf("|+---------------------------+|\n");
}

void input() {
    int next,i;
    inf++;
    printf("请输入航班号\n");
    scanf("%d",&data[count].ID);
    for(i=0;i<count;i++){
        if(data[count].ID==data[i].ID){
            cout<<"该航班已经存在,请重新输入!"<<endl;
            input();
        }
    }
    printf("请输入起降时间\n");
    scanf("%s",data[count].time);
    printf("请输入日期\n");
    scanf("%s",data[count].data);
    printf("请输入起点\n");
    scanf("%s",data[count].start);
    printf("请输入终点\n");
    scanf("%s",data[count].end);
    printf("请输入余票\n");
    scanf("%d",&data[count].pnum);
    printf("请输入票价\n");
    scanf("%d",&data[count].price);
    count++;
    writeData();
    printf("%d个数据录入完毕\n",inf);
    printf("按1继续录入,按0结束操作\n");
    scanf("%d",&next);
    system("cls");
    if(next==0)
        admire();
    else if(next==1)
        input();
}
void fix() {
    int p_id,t=-1,i;
    printf("请输入需操作的航班ID:");
    scanf("%d",&p_id);
    for(i=0; i<count; i++) {
        if(data[i].ID==p_id) {
            t=i;
            break;
        }
    }
    if(t==-1) {
        printf("对不起,无此次航班。重新操作请按1,返回请按0\n");
        int s;
        scanf("%d",&s);
        if(s==1)
            fix();
        if(s==0)
            admire();
    } else {
        printf("请输入起降时间\n");
        scanf("%s",data[t].time);
        printf("请输入日期\n");
        scanf("%s",data[t].data);
        printf("请输入起点\n");
        scanf("%s",data[t].start);
        printf("请输入终点\n");
        scanf("%s",data[t].end);
        printf("请输入票价\n");
        scanf("%d",&data[t].pnum);
        printf("请输入剩余票数\n");
        scanf("%d",&data[t].price);
        writeData();
        printf("修改成功!\n");
        admire();
    }
}
void passager() {
    int i;
    if(user==0) {
        printf("没有乘客可查看\n");
        admire();
    }
    cout<<"乘客订单"<<setw(10)<<"姓名"<<setw(8)<<"身份证号"<<setw(10)<<"票数"<<setw(8)<<"航班号"<<endl;
    for(i=0; i<user; i++)
        if(pass[i].ticket!=0)
            cout<<pass[i].bill<<setw(10)<<pass[i].name<<setw(8)<<pass[i].card<<setw(10)<<pass[i].ticket<<setw(8)<<pass[i].airID<<endl;
    printf("\n");
    admire();
}
int main() {
    readData();
    userData();
    menu();
}
posted @ 2020-02-08 12:53  EternallyW  阅读(754)  评论(1编辑  收藏  举报