挑战题二

来源:互联网 发布:纳米盒软件下载 编辑:程序博客网 时间:2024/06/06 23:10




#include<iostream>#include<stdlib.h>#include<iomanip>#include<cstring>struct train{    int num;    char set[10];    char dest[10];    char time[20];    int rest;};typedef train ElemType;struct List{    ElemType list[10];    int size;};void InitList(List &L){    L.size = 0;}std::ostream &operator<<(std::ostream &out, ElemType listitem){        std::cout << listitem.num << "                 " << std::setw(3)        << listitem.set << std::setw(3) << "          " << listitem.dest <<        std::setw(3) << "         " << listitem.time << std::setw(3) << "           "        << listitem.rest << std::endl;        return out;}void TraverseList(List &L){    for (int i = 0; i < L.size; i++)        std::cout << L.list[i] ;    std::cout << std::endl;}bool operator <(ElemType item, ElemType listitem){    return item.num < listitem.num;}bool InsertList(List &L, ElemType item, int pos){    if (pos<-1 || pos> L.size+ 1)    {        std::cout << "pos值无效!" << std::endl;        return false;    }    int i;    if (pos == 0)    {        for (i = 0; i < L.size; i++)            if (item< L.list[i])                break;        pos = i + 1;    }    else if (pos == -1)        pos = L.size + 1;    if (L.size == 10)    {        std::cout << "表满,不能继续插入";        return false;    }    for (i = L.size - 1; i >= pos - 1; i--)        L.list[i + 1] = L.list[i];    L.list[pos - 1] = item;    L.size++;    return true;}bool operator==(ElemType listitem, ElemType item){    return (strcmp(listitem.dest, item.dest) == 0||listitem.num==item.num);}bool FindList(List &L, ElemType& item){    for (int i = 0; i < L.size; i++)        if (L.list[i] == item)        {            item = L.list[i];            return true;        }    return false;}void star(){    int i ;    for (i = 0; i <= 35; i++)        std::cout << " ";    for (i = 1; i <= 36; i++)        std::cout << "*";    std::cout << " " << std::endl;}void mainmenu(){    int a;    star();    for (a = 0; a <= 47; a++)        std::cout << " ";    std::cout << "火车售票系统" << std::endl;    std::cout << std::endl;    std::cout << "                                  请选择你的操作:(按q退出)" << std::endl;    std::cout << "                                    1.录入班车信息;        " << std::endl;    std::cout << "                                    2.浏览班车信息:        " << std::endl;    std::cout << "                                    3.查询行车路线:        " << std::endl;    std::cout << "                                    4.售票与退票系统:      " << std::endl;    std::cout << "请输入你需要的功能:        " << std::endl;    std::cout << std::endl;}void outline(){    std::cout << "班次" << "      " << std::setw(3) << "       "        << "出发站" << std::setw(3) << "       " << "终点站" <<        std::setw(3) << "       " << "发车时间" << std::setw(3) << "       "        << "剩余票数" << std::endl;;}void submenu1(){    star();    std::cout << "                                                1.按班次号查询:        " << std::endl;    std::cout << "                                                2.按终点站查询:        " << std::endl;    std::cout << "                                                3.返回主菜单:        " << std::endl;    std::cout << "请输入你需要的功能:        ";}void submenu2(){    star();    std::cout << "                                                1.订票:        " << std::endl;    std::cout << "                                                2.退票:        " << std::endl;    std::cout << "                                                3.返回主菜单:        " << std::endl;    std::cout << "请选择你需要的功能:";}std::istream &operator>>(std::istream &in, ElemType listitem){    std::cin >> listitem.num >> listitem.set >> listitem.dest >> listitem.time >> listitem.rest;    return in;}void bookticket(int number,List &L,int ticketnum){    int i;    for (i = 0; i < L.size; i++)        if (L.list[i].num == number)        {            L.list[i].rest-=ticketnum;            std::cout << "订票成功!请按时上车!" << std::endl;            outline();            std::cout << L.list[i];        }}void returnticket(int number,List &L,int ticketnum){    int i;    for (i = 0; i < L.size; i++)        if (L.list[i].num == number)        {            L.list[i].rest+=ticketnum;            std::cout << "退票成功!欢迎下次乘搭!" << std::endl;            outline();            std::cout << L.list[i];        }}int main(){    int i, key, search, fun, trainnum, tickets;    train number, destination;    train b[10] = { {01,"广州","北京","6:00",69},{02,"香港","上海","8:00",77},    { 03,"珠海","成都","19:00",89 } };    List L;    InitList(L);    for (i = 0; i < 3; i++)        InsertList(L, b[i], i + 1);    mainmenu();    while (std::cin >> key)    {       switch (key)        {            case 1:                 outline();                 std::cin >> b[i];                 i++;                 std::cout << "录入成功!" << std::endl;                 mainmenu();                 break;            case 2:                 outline();                 TraverseList(L);                 std::cout << std::endl;                 break;            case 3:                submenu1();                while (std::cin >> search)                {                    switch (search)                    {                    case 1:                        std::cout << "输入班次号:";                        std::cin >> number.num;                        if (FindList(L, number))                        {                            std::cout << "查找成功!" << std::endl;                            outline();                            std::cout << number;                        }                        else std::cout << "查找失败!表中无此数据!" << std::endl;                        break;                    case 2:                        std::cout << "输入终点站:        " << std::endl;                        std::cin >> destination.dest;                        if (FindList(L, destination))                        {                            std::cout << "查找成功!" << std::endl;                            outline();                            std::cout << destination;                        }                        else std::cout << "查找失败!表中无此数据!" << std::endl;                        break;                    case 3:                        std::cout << "返回主菜单!" << std::endl;                        break;                    }                    break;                }                mainmenu();                break;            case 4:                submenu2();                std::cin >> fun;                switch (fun)                {                case 1:                    std::cout << "请输入你需要订票的班次:";                    std::cin >> trainnum;                    std::cout << "请输入你需要订的票数:";                    std::cin >> tickets;                    bookticket(trainnum,L,tickets);                    break;                case 2:                    std::cout << "请输入你需要退票的班次:";                    std::cin >> number;                    std::cout << "请输入你需要退的票数:";                    std::cin >> tickets;                    returnticket(trainnum,L,tickets);                    break;                case 3:                    break;                }                std::cout << "返回主菜单!" << std::endl;                mainmenu();                break;        }     }    system("pause");    return 0;}

最近做的比较大的一个作业,然后途中问了很多人。

他们的评价就是我的风格不如用C,很少C++风格,C++的优点很难体现得出来,

很是打击,看来输在起跑线上了大哭


原创粉丝点击