zoukankan      html  css  js  c++  java
  • SSD5_ Exam 2分析

    这个是STL的堆栈适配器练习

    看一下它给的描述吧

    Description

    This assessment tests your ability to use the STL stack adapter, the STL vector container, and the STL find algorithm to solve a problem. You are asked to finish the implementation of a program that simulates a multiple-aisle parking lot. When cars are parked bumper-to-bumper, each aisle in this parking lot can hold three cars. There are five aisles in the parking lot.

    It is your task to finish the implementation of the simulation that processes the vehicle arrivals and departures. The goal of the simulation is to keep track of and report how many times individual cars are moved while handling the departure of other cars. The simulation also displays an alphabetized list of all the cars that visited the parking lot during the simulation. 

    然后是工作流程;

    1. First, finish the implementation of function find_car. This function returns a reference to the Car object stored in the vector cars whose license plate equals the parameter plate. Use the STL find function to perform this task. To use this function correctly, you must supply it with three arguments. The first two arguments specify a range to search. The third argument is the value that the function attempts to find. This argument must be of type Car.
    2. Next, finish the implementation of function handle_arrival. This function should iterate through the vector of stacks, looking for the first stack that does not contain three cars. If all five aisles (stacks) are full, output a message indicating such; otherwise place the license plate into the first non-full stack. This is essentially "parking" the car. For this arriving car, also add an entry of type Car to the vector cars. In this Car instance, make sure to record properly the index of the aisle where this car is parked.
    3. Then, finish the implementation of function handle_departure. This function should locate the departing vehicle from the cars vector using function find_car. Then this function should remove the departing car's license plate from the appropriate aisle. Another stack must be used to move, temporarily, any cars that may be in front of the departing car. Record the number of times a car is moved when accommodating the departure of another car. For the departing car, display the number of times it was moved while it was parked in the lot.

        Finally, write the code that displays an alphabetized list of all the cars the visited the list. To do this you must first sort the vector cars using the STL sort function. Then, using iterators, traverse the sorted vector and display the license plates of the cars. 

    我把写好的代码给附上

    (里面的牌号和型号的意思差不多)

    Car.h

    #include <iostream>
    #include <string>
    #ifndef _CAR_H_
    #define _CAR_H_
    //这个和前面那个栈练习差不多
    using namespace std;
    
    class Car {
    
    private:
        string license_plate;
        int moved;
        int aisle;
    
    public:
        //两个构造函数
        Car(string);
        Car(string, int);
        //得到和设置移动次数
        int getTimesMoved(void) const;
        void setTimesMoved(int);
        //得到牌号和通道
        string getPlate(void) const;
        int getAisle(void) const;
        //重载两个运算符都没有难度
        bool operator < (const Car& rhs) const;
        bool operator == (const Car& rhs) const;
    };
    
    #endif

    Car.cpp

    #include "car.h"
    
    Car::Car(string plate) :license_plate(plate), moved(0), aisle( -1) {}
    
    Car::Car(string plate, int aisle) : license_plate(plate),moved(0), aisle(aisle) {}
    
    int Car::getTimesMoved(void) const {
    
        return moved;
    }
    
    void Car::setTimesMoved(int m) {
    
        moved = m;
    }
    
    int Car::getAisle(void) const {
    
        return aisle;
    }
    
    string Car::getPlate(void) const {
    
        return license_plate;
    }
    
    bool Car::operator < (const Car& rhs) const {
    
        return this->getPlate() < rhs.getPlate();
    }
    
    bool Car::operator == (const Car& rhs) const {
    
        return this->getPlate() == rhs.getPlate();
    }

    main.cpp

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <stack>
    #include <stdexcept>
    #include <vector>
    #include <algorithm>
    
    #include "car.h"
    /*
     * 作者:白强
     * 日期:2013.5.6
     * 参考及解析
     * 这个停车场的数据结构大概是停车场为容器,然后每个停车场item都是一个通道栈
     */
    using namespace std;
    
    const unsigned int PARKING_SPOTS_PER_AISLE = 3;
    const unsigned int NUMBER_OF_AISLES = 5;
    
    void handle_arrival(vector<Car>&, vector<stack<string> >&, const string&);
    void handle_departure(vector<Car>&, vector<stack<string> >&, const string&);
    Car& find_car(vector<Car>&, string);
    
    int main(int argc, char* argv[]) {
    
        try {
    
            if (argc != 2) {
                cerr << "Usage:\n" << argv[0] << " data-file";
                return EXIT_FAILURE;
            }
    
            ifstream inf(argv[1]);
            if (! inf) {
                cerr << "Could not open " << argv[1];
                return EXIT_FAILURE;
            }
    
            vector<Car> cars;
            //这个类型应该是在aisle栈,停车场为vector组合出来的
            vector< stack<string> > parking_lot(NUMBER_OF_AISLES);
    
            while (! inf.eof()) {
    
                string action, plate;
                inf >> plate >> action;
    
                if (action == "arrives") {
                    handle_arrival(cars, parking_lot, plate);
                } else if (action == "departs") {
                    handle_departure(cars, parking_lot, plate);
                } else {
                    cerr << "Unknown action: " << action << endl;
                }
    
            }
            inf.close();
    
            cout << "\nHere are all the cars that visited the lot today:\n";
    
            // TODO: Output the license plates of all the
            // cars that visited the lot, in alphabetical order
            //按照题目的意思先按字母排序,再列出所有来的车的牌号
            //STL中的sort函数,相当于调用sort(cars.begin(), cars.end(), less<Car>() );
            sort(cars.begin(), cars.end());
            //使用迭代器遍历vector<Car>,用getPlate()取得牌号
            vector<Car>::iterator it = cars.begin();
            cout << "\n Cars visited the lot today:\n";
            for ( ; it != cars.end(); it++) {
                cout << it->getPlate() << endl;
            }
            return EXIT_SUCCESS;
    
        } catch (exception& e) {
            cerr << e.what() << endl;
        } catch (...) {
            cerr << "Unknown exception caught!" << endl;
        }
    
        return EXIT_FAILURE;
    }
    
    void handle_arrival(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
        //需要自己完成的方法
        //临时变量看每个通道是否已满
        bool isFull;
        //使用停车场迭代器
        vector< stack<string> >::iterator i=parking_lot.begin();
        //通道数
        int aisle=0;
        //for循环,其实是循环5次
        for ( ; i != parking_lot.end(); i++, aisle++) {
            //注意PARKING_SPOTS_PER_AISLE的值是3,当每种牌号的超过3个时候那个通道就满了
                if (i->size() !=PARKING_SPOTS_PER_AISLE ) {
                    isFull = false;
                    break;
                }
            }
        //每个通道不满的时候
        if (!isFull) {
                //我们创建个Car对象
                Car arrival(plate, aisle);
                //在停车场容器的通道栈把这个牌号压入栈
                i->push(plate);
                //在总的Car的vector容器中把这个车放进去
                cars.push_back(arrival);
    
            } else {
    
                cout << "Sorry " << plate << ",is full"<<endl;
            }
    }
    
    void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
            //找到要离开的车
            Car departing = find_car(cars, plate);
            //创建临时栈存放车
            stack<string> temp;
            //得到要走的车的通道
            stack<string>& aisle = parking_lot[departing.getAisle()];
            //如果通道最顶端不是这个型号的车
            while (aisle.top() != plate) {
                //找最顶端的车的型号
                Car& moving = find_car(cars, aisle.top());
                //移动次数加一
                moving.setTimesMoved(moving.getTimesMoved() + 1);
                //存放到临时栈
                temp.push(aisle.top());
                //这个栈顶除去
                aisle.pop();
            }
            //输出根据牌号找到的车的牌号及移动次数
            cout << departing.getPlate() << " was moved "
            << departing.getTimesMoved() << " times it was moved\n";
            //核心出栈方法
            aisle.pop();
            //当临时栈不为空的时候,再把临时栈的移回原来的地方
            while (! temp.empty()) {
                aisle.push(temp.top());
                temp.pop();
            }
    }
    
    Car& find_car(vector<Car>& cars, string plate) {
            //调用STl中的find函数在vector<Car>查找指定牌号的车
            vector<Car>::iterator it;
            //通过plate创建一个临时的Car对象
            Car key(plate);
            //函数调用find(_IIter, _IIter, const _Tp&);
            it = find(cars.begin(), cars.end(), key);
            //返回一个Car的引用
            return *it;
    }

    最后再附上测试文件

    data.txt

    COOLONE arrives
    TKG-123 arrives
    QWE-839 arrives
    UTU-K90 arrives
    RRR-877 arrives
    GHL-GII arrives
    PROGRAM arrives
    COOLONE departs
    HEAD-DR arrives
    DMS-RJS arrives
    TUE-87B arrives
    WEW-GH1 arrives
    THE-MAN arrives
    PORSCHE arrives
    ERU-883 arrives
    APPLE-1 arrives
    BKE-284 arrives
    TKG-123 departs
    WEW-GH1 departs
    APPLE-1 departs
    APPLE-2 arrives
    APPLE-2 departs
    BKE-284 departs
    QWE-839 departs
    GHL-GII departs
    RRR-877 departs
    PROGRAM departs
    ERU-883 departs
    UTU-K90 departs
    HEAD-DR departs
    MR-JOHN arrives
    DMS-RJS departs
    TUE-87B departs
    THE-MAN departs
    PORSCHE departs
    MR-JOHN departs

    把main.cpp提交即可



        --------------------------------欢迎指错,分享交流

  • 相关阅读:
    抖音的服务器到底啥配置?
    三句话搞懂 Redis 缓存穿透、击穿、雪崩!
    Windows环境下安装Redis
    Redis可视化工具 Redis Desktop Manager
    Eureka自我保护机制
    Eureka介绍
    Spring Cloud OpenFeign 工作原理解析
    客户端负载均衡Ribbon:Loadbalance的源码
    spring boot中的约定优于配置
    Arrays.asList()返回的集合不能进行add,remove等操作
  • 原文地址:https://www.cnblogs.com/bq12345/p/3063188.html
Copyright © 2011-2022 走看看