【昊昊带你学】软院特别篇-简单订票系统

A simple airline ticket reservation program

Prerequisites, Goals, and Outcomes

Prerequisites: Students should have mastered the following prerequisite skills.

  • Pointers - Using pointers to indirectly reference and modify objects
  • Linked Lists - Understanding of a linked list implementation
  • Dynamic Memory Management - Use of new and delete

Goals: This assignment is designed to reinforce the student's understanding of linked lists.

Outcomes: Students successfully completing this assignment would master the following outcomes.

  • Understand implementation of linked lists
  • Use pointers
  • Use dynamic memory management

Description

A simple airline ticket reservation program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of the flights. Create a linked list of flights with each node including a pointer to a linked list of passengers.

Tasks

Write the fuller version of the ticket reservation program according to the description above.


**************************转载请注明出处!******************************

         今天昊昊怀着万分激动,去上第一次算法数据结构的上机课,结果。。。。。。结果尼玛被虐了有木有~!~!~!~!~!~!怀着虐题的心态一顿乱写,突然发现,尼玛是个坑啊~~~~无数次惨痛的教训啊~~~~写东西要现画好设计图啊~~~~别干写啊~~~~没前途啊有木有~~~~

 

         好了,刚刚先咆哮一下,接下来咱干正事儿。一上来拿到这个题,觉得屌爆了。后来发现,居然是抄来的。这题我就看了好久,到最后也没明白fuller版本想表达的是什么,不过不是重点,把数据结构实现之后,这些都是小事儿。昊昊是这么设计的:

     class Passenger      //乘客

     {

     public:

        string name;    //姓名

        Passenger *next; //下一位乘客

 

        Passenger(string name, Passenger *p)

        {

            this->name = name;

            next = p;

        }

    };

 

    class Flight //航班,因为题目要求比较简单,只写了最基本的。

    {

    public:

        string name; //航班号

        Passenger *p; //指向该航班上的乘客列表

        Flight *next;     //下一班航班

        Flight(string name, Flight *p)

        {

            this->p = new Passenger("pHead",0);

            this->name = name;

            next = p;

        }

    };

 

    class FlightList          //航班列表

    {

    private:

        Flight *head;  //表头

 

    public:

        FlightList()

        {

            head = new Flight("head", 0);

        }

        void addFlight(string name); //添加航班,就是在一个链表中加入一个节点

        Flight* findFlight(string name);//搜索一个叫name的航班,链表搜索

        Passenger* findPassenger(string name, Passenger *p);

         //找乘客,在p指针后查找一个叫name的乘客。返回他的前驱。P由findFlight找到。

        void insertPassenger(string name, Passenger *p);

                            //插入乘客,p指针后插入一个乘客。P由findFlight找到。

        bool deletePassenger(Passenger *p);

                            //删除乘客,删除p->link指向的节点

        bool Reserve(string flight, string name);

                            //订票,给定航班flight,和姓名name,加入一个Passenger

        bool cancelReserve(string flight, string name);

                            //取消,给定航班flight,姓名name,删除此人。

        void showFlight();

                            //显示现有航班情况。

        void showFlight(string name);

                            //显示name航班上乘客情况。

        void showAll();

                            //显示所有航班及乘客信息。

    };

 

上面就是这个程序的数据结构。下面来讲几个函数的实现。(只讲关键的步骤,一些异常处理请大家自己思考。)

 

 

Flight* FlightList :: findFlight(string name);           //链表节点查找

 

PS:单链表查找就只能从前往后遍历。先用临时指针指向第一个结点:Flight *tmp =

head->next; 接着 while ( tmp != 0 && tmp->name != name ) 时,tmp = tmp->next;

查找就结束了。当然这里会有查找不到的情况,自行解决~

 

void addFlight(string name);             //链表结点插入

 

PS:FlightList有head指针,它指向一个Flight组成的list。addFlight就是在这个list中添

      加一个航班。用指针新建一个节点,Flight *tmp = new Flight(name, head->next)来初始

  化。此时Flight->nex指向head->next,我们只需要 head->next = tmp 就完成了

  addFlight。

 

bool deletePassenger(Passenger *p);  //链表结点删除

 

PS: 这个函数的功能是删除p指针的后继。因为我们这里用的是单向列表,如果给出的是要

删除的结点本身就会造成许多麻烦。所以索性就直接给出要删除结点的前驱。删除的过

程也没什么亮点,先找个指针记录一下要删的节点:Passenger *tmp = p->next 将p->next

指向下一个节点:p->next = tmp->next 删除tmp。

void showFlight(string name);   //显示name航班的乘客信息

{

    Flight* tFlight = findFlight(name);

    Passenger* tPassenger;

 

    if ( tFlight != 0 )

    {

        cout << tFlight->name << ':' << '\t';

        tPassenger = tFlight->p->next;

        while ( tPassenger != 0 )

        {

            cout << tPassenger->name << '\t';

            tPassenger = tPassenger->next;

        }

        cout << endl;

    }

    else

    {

        cout << "No Such Flight" << endl;

    }

}

 

PS:这里直接贴一个函数上来,里面调了findFlight,如果findFlight返回的是空指针,就打印“No Such Flight”。

 

         上面列举了三个函数,增、删、查,主要也就是这几个操作了。咱们之前数据结构那部分已经讲过了,实现其实应该没有难度。最重要的部分其实还是数据结构和一些方法的设计。只要设计部分比较细致,工作做得到位,其实要写这个订票系统也就是三两小时的时间。在今后的生涯中,希望大家能听一句劝。不论做题,还是项目开发。一定要事先设计好,无论东西看起来怎样简单,不要轻易得上手。按照设计图来写,不仅有条有理,而且对你DeBug也是很有好处的。代码已上传至QQ群。各位懂的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构第16次作业,hash表 Spellchecking Prerequisites, Goals, and Outcomes Prerequisites: Students should have mastered the following prerequisite skills. • Hash Tables - Understanding of the concept of a recursive function • Inheritance - Enhancing an existing data structure through specialization Goals: This assignment is designed to reinforce the student's understanding of the use of hash tables as searchable containers. Outcomes: Students successfully completing this assignment would master the following outcomes. • Familiarize how to use hash tables, specifically hash sets Background Any word processing application will typically contain a spell check feature. Not only does this feature point out potentially misspelled words; it also suggests possible corrections. Description The program to be completed for this assessment is a spell checker. Below is a screen shot of the program in execution? The program begins by opening a word list text file, specified by a command line parameter. The program outputs an error message and terminates if it cannot open the specified word list text file. A sample word list text file (wordlist.txt) is given in the supplied wordlist.zip archive. After successfully opening the specified word list text file, the program then stores each word into a hash table. The program then opens a file to spell check. This user specifies this file through the command line. After opening this file, the program then compares each word in the file against the words stored in the hash table. The program considers a word to be misspelled if the word does not exist in the hash table. When this occurs, the program displays the line number the word appeared in, the word, and a list of possible corrections. The list of possible corrections for a misspelled word is generated using a simple algorithm. Any variation of a misspelled word that is itself a word (i.e. it is found in the word list file) is a possible correction. Your solution to this asses

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值