• 订票例子


    #include<iostream>
    #include<string>
    #include<fstream>
    #include<sstream>
    using namespace std;
    #define _CRT_SECURE_NO_WARNINGS
    class Tair //通过定义一个类来定义数据录入的函数
    {
     char flight[20]; char data[20]; char start[20];
     char finish[20]; char stime[20]; char ftime[20]; int price;
    public:
     Tair()
     {};
     Tair(char fl[20], char D[20], char S[20], char Fin[20], char st[20], char ft[20], int pr);
     friend void main(); //将主函数定义为友元函数,方便访问类中的成员
    };

    Tair::Tair(char fl[20], char D[20], char S[20], char Fin[20], char st[20], char ft[20], int pr)
    {
     strcpy(flight, fl); //
     strcpy(data, D);
     strcpy(start, S);
     strcpy(finish, Fin);
     strcpy(stime, st);
     strcpy(ftime, ft);
     price = pr; //
    }

    void main()
    {
     int p = 0; //全局变量,用于选择菜单时的输入
     Tair *s[100]; //类定义的指针将所录入的数据按类中的形式分别存储
     ofstream *file[100];
     int i = 0; //全局变量,用于做每个录入数据的下标
     int j = 0;
     int chris1 = 0;//用于判断do—while语句
     while (p != 6)
     {
      cout << " ************ 欢迎来到民航售票系统,请选择您需要的操作并输入相应数字! *********** ";
      cout << "(1)录入信息 ";
      cout << "(2)按起点查出所有航班的信息 ";
      cout << "(3)按终点查出所有航班的信息 ";
      cout << "(4)按日期、航班号查询航班信息 ";
      cout << "(5)按起点站统计航班数 ";
      cout << "(6)退出 ";
      do //判断输入指令是否正确
      {
       cin >> p;
       if ((p >= 1 && p <= 6))
        chris1 = 1;
       else
        cout << "指令错误!请重新输入:" << endl;
      } while (chris1 == 0);
      do
      {
       switch (p)
       {
       case 1://录入
       {
            char flight[20], data[20], start[20], finish[20], stime[20], ftime[20];
            int price; char c; //用于在小项中选择是否继续操作
            do
            {
             cout << "请依次输入航班号、日期、起点站、终点站、起飞时间、到达时间、票价 ";
             cin >> flight >> data >> start >> finish >> stime >> ftime >> price;
             fstream outfile, infile; //作一个文本文档在文件夹中用于显示所录入的数据
             outfile.open("flight.txt", ios::out | ios::app); //以向文件追加写入的方式打开文件flight.txt
             if (!file)
             {
              cout << "flight.txt can not open. ";
              return;
             }
             outfile << "航班号:" << flight << " " << "日期:" << data << " " << "起点站:" << start << " " << "终点站:" << finish << " " << "起飞时间:" << stime << " " << "到达时间:" << ftime << " " << "票价:" << price << endl;

             outfile.close();
             infile.open("flight.txt", ios::in);
             if (!file) //判断文件是否打开
             {
              cout << "f1.txt can not open. ";
              return;
             }
             char ch;
             while (infile.get(ch))
              cout << ch;
             cout << endl << endl;
             infile.close();
             j++;
             s[i] = new Tair(flight, data, start, finish, stime, ftime, price);
             i++;
             cout << "已录入" << i << "条航班信息,想继续录入吗(y/n)" << endl;
             cin >> c;
             chris1 = 0;

             if (c != 'y'&&c != 'n')
             {
              cout << "指令错误!请重新输入!" << endl;
              cin >> c;
             }
            }
            while (c == 'y');
            break;
       }
       case 2://按起点站查询
       {
            char st[20]; char c; int j = 0; int flag;
            if (i == 0)
            {
             cout << "系统中没有航班信息,请先进行录入操作!" << endl; break;
            }
            do
            {
             flag = 0;
             cout << "请输入要查询的航班的起点站: " << endl;
             cin >> st;
             cout << "您要查询的起点站为%c的航班信息如下:" << st << endl << endl;
             for (int j = 0; j<i; j++)
             {
              if (strcmp(st, (*s[j]).start) == 0)//比较字符串相等
              {
               flag = 1;

               cout << "航班号:" << (*s[j]).flight << " 日期:" << (*s[j]).data << " 终点站:" << (*s[j]).finish << " 起飞时间:" << (*s[j]).stime << " 到达时间:" << (*s[j]).ftime << " 票价:" << (*s[j]).price << endl;
              }
             }
             if (flag == 0) cout << "对不起,您查询的航班信息不存在!" << endl << endl;
             cout << "您想继续查询吗?(y/n):";
             cin >> c;
             if (c != 'n'&&c != 'y')
             {
              cout << "指令错误!请重新输入:" << endl;
              cin >> c;
             }
            } while (c == 'y');
            break;
       }
       case 3://按终点站查询
       {
            char fin[20]; char c; int j = 0; int flag;
            if (i == 0)
            {
             cout << "系统中没有航班信息,请先进行录入操作!" << endl; break;
            }
            do
            {
             flag = 0;
             cout << "请输入要查询的航班的终点站: " << endl;
             cin >> fin;
             cout << "您要查询的终点站为" << fin << "的航班信息如下:" << endl << endl;
             for (int j = 0; j<i; j++)
             {
              if (strcmp(fin, (*s[j]).finish) == 0)
              {
               flag = 1;

               cout << "航班号:" << (*s[j]).flight << " 日期:" << (*s[j]).data << " 起点站:" << (*s[j]).start << " 起飞时间:" << (*s[j]).stime << " 到达时间:" << (*s[j]).ftime << " 票价:" << (*s[j]).price << endl;
              }
             }
             if (flag == 0) cout << "对不起,您查询的航班信息不存在!" << endl;
             cout << "您想继续查询吗?(y/n):";
             cin >> c;
             if (c != 'n'&&c != 'y')
             {
              cout << "指令错误!请重新输入:" << endl;
              cin >> c;
             }
            } while (c == 'y');
            break;
       }
       case 4://按日期、航班号查询
       {
            char dat[20], fli[20]; char c; int j, flag;
            if (i == 0)
            {
             cout << "系统中没有航班信息,请先进行录入操作!" << endl; break;
            }
            do
            {
             flag = 0;
             cout << "请输入要查询的航班的日期和航班号: " << endl;
             cin >> dat >> fli;
             cout << "您要查询的日期为" << dat << ",航班号为" << fli << "的航班信息如下:" << endl << endl;
             for (j = 0; j<i; j++)
             {
              if (strcmp(dat, (*s[j]).data) == 0 && strcmp(fli, (*s[j]).flight) == 0)
              {
               flag = 1;

               cout << "起点站:" << (*s[j]).start << " 终点站:" << (*s[j]).finish << " 起飞时间:" << (*s[j]).stime << " 到达时间:" << (*s[j]).ftime << " 票价:" << (*s[j]).price << endl;
              }
             }
             if (flag == 0) cout << "对不起,您查询的航班信息不存在!" << endl;
             cout << "您想继续查询吗?(y/n):";
             cin >> c;
             if (c != 'n'&&c != 'y')
             {
              cout << "指令错误!请重新输入:" << endl;
              cin >> c;
             }
            } while (c == 'y');
            break;
       }
       case 5://按起点或终点统计一周的航班数
       {
            int count, flag, j;
            char str[20]; char c;
            if (i == 0)
            {
             cout << " 系统中没有航班信息,请先进行录入操作!" << endl; break;
            }
            do
            {
             flag = 0; count = 0;
             cout << " 请输入要统计的航班起点站:" << endl;
             cin >> str;
             for (j = 0; j<i; j++)
             {
              if (strcmp(str, (*s[j]).start) == 0)
              {
               flag = 1;
               count++;
              }
             }
             if (j >= i && flag == 0)
              cout << "对不起,您查询的航班信息不存在!" << endl << endl;
             else
              cout << " 您要查询的起点站为" << str << "的航班数为:" << count << endl << endl;
             cout << "您想继续查询吗?(y/n):";
             cin >> c;
             if (c != 'n'&&c != 'y')
             {
              cout << "指令错误!请重新输入:" << endl;
              cin >> c;
             }
            } while (c == 'y');
            break;
       }
       case 6://退出
       {
            cout << endl << "********* 感谢您的使用,( ^_^ )/~~拜拜! **********" << endl;
            break;
       }

       }break;
      } while (chris1 == 0);
     }
    }

  • 相关阅读:
    OpenStack 发行版本
    刷新linux硬盘存储接口
    LVM实践
    LVM man帮助
    ansible --help 文档
    nmcli connection modify eth1 ipv4.addr "192.168.31.23" ipv4.method manual
    自己动手使用, MetaWeblog 发布博客(cnblogs)
    测试图片上传 on Markdown editor
    大批量更新数据mysql批量更新的四种方法
    PHP print_r 转换/还原为数组
  • 原文地址:https://www.cnblogs.com/rong123/p/8056683.html
Copyright © 2020-2023  润新知