程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Car類car.h

Car類car.h

編輯:C++入門知識

1.先來看一下題目要求

  To complete this assessment, you will need to design and implement class Car and implement the parking-lot simulator program.

  To begin, verify the files needed for this assessment.

1.Extract the archive to retrieve the files needed to complete this assessment.
Following is an ordered list of steps that serves as a guide to completing this assessment. Work and test incrementally. Save often

1.First, declare and implement class Car. Instances of class Car need to store the license plate of the car and the number of times the car has been moved while it has been parked in the lot.
2.Next, begin the implementation of the parking-lot simulator program. Create a filed named main.cpp. Among other libraries, you will need to add the necessary include directives to access the C++ input/output library, the file input/output library, the STL stack adapter, and your class Car. Create function main.
3.Then, write the code that opens the simulation data file. The user of the simulation should specify the data file to use via the command-line. Take appropriate actions if a command-line is not present or if the specified file cannot be opened.
4.Next, declare a stack object to represent the single-aisle parking lot. This object must be of type stack<Car*>
5.Then, read the contents of the data file. Each line in the data file is in the form: license-plate action, where action is either "arrives" or "departs". For each arrival, instantiate an object of type Car in the free store. Simulate parking the car by pushing a pointer to the object into the parking-lot stack. Output a meaningful message if the parking lot is full. The lot is full when the stack contains five elements. For each departure, remove the corresponding Car pointer from the stack, and output the number of times this car was moved while it was parked in the lot. To do this and preserve the order of the other cars, you may need to use a second, temporary stack of type pointer to Car. Be sure to keep track of the number of times a car is moved while accommodating the departure of another car. Do not leak memory.
6.Finally, after processing the contents of the data file, output the number of times each car that remains in the lot (if there are any) was moved.
  所以我們首先聲明Car類car.h


#include <iostream>
#include <string>
/*
*對答案注釋
*作者:白強
*日期:2013/4/17
*
*/
using namespace std;
 /*Instances of class Car need to store the license plate of the car and the number of times the car has been moved while it has been parked in the lot.(來自doc要求)
 要求統計每一輛車的移動次數顧定義moved變量
 車的牌號定義變量license_plate
 相應的 times_moved(void)返回moved
 get_license_plate(void)返回license_plate
 構造函數就不介紹了
 */
class Car {

private:
    string license_plate;
    int moved;

public:
    Car(string);
    int& times_moved(void);
    string get_license_plate(void);
};再就是對Car的定義文件car.cpp


#include "car.h"
//對car.h實現見car.h說明
Car::Car(string plate) :license_plate(plate),
        moved(0) {}

int& Car::times_moved(void) {

    return moved;
}

string Car::get_license_plate(void) {

    return license_plate;
}這個時候再只需一個main.cpp了

總體思想是根據第二個參數判斷是出是入
入比較簡單,直接壓入即可,出的時候考慮一下是否是最頂的車
是的話直接出棧,不是的話先把它頂上的移到臨時棧,使要的車的出棧,然後再把臨時棧的車全部壓回停車場棧即可


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stack>
#include <stdexcept>

#include "car.h"
/*
 * 作者:白強
 * 日期:2013/4/17\
 * All Right Reserved
 */

/*總體思想是根據第二個參數判斷是出是入
 *入比較簡單,直接壓入即可,出的時候考慮一下是否是最頂的車
 *是的話直接出棧,不是的話先把它頂上的移到臨時棧,去除要的車,然後再把臨時棧的車全部壓回停車場棧即可
 *總的來說就是出的時候比較有些繞而已
 */
using namespace std;
//定義停車場最大的停車數
const int NUMBER_OF_PARKING_SPOTS = 5;
//聲明兩個函數車來時如何處理,車走時如何處理
//注意stack的模板使用方法stack<Car*>&,掌握STL中的Stack使用
void handle_arrival(stack<Car*>&, const string&);
void handle_departure(stack<Car*>&, const string&);

int main(int argc, char* argv[]) {
    //如果沒有data.txt參數,程序退出
    if (argc != 2) {
        cerr << "Usage:\n" << argv[0] << " data-file";
        return EXIT_FAILURE;
    }
    //data.txt不能打開。程序異常退出
    ifstream inf(argv[1]);
    if (! inf) {
        cerr << "Could not open " << argv[1];
        return EXIT_FAILURE;
    }
    //讀入data.txt
    try {
        //關鍵的定義棧,停車場棧
        stack<Car*> parking_lot;
        //直到文件末尾
        while (! inf.eof()) {
            //注意是按詞讀取
            string action, plate;
            inf >> plate >> action;
            //根據第二個詞采用不同函數處理
            if (action == "arrives") {
                handle_arrival(parking_lot, plate);
            } else if (action == "departs") {
                handle_departure(parking_lot, plate);
            } else {
                cerr << "Unknown action: " << action << endl;
            }

        }
        inf.close();
        // Handle any cars that remain in the lot.
        while (! parking_lot.empty()) {//當棧不為空
            //得到棧頂的車
            string plate = parking_lot.top()->get_license_plate();
            //處理剩下的車       handle_departure(parking_lot, plate);
        }

        return EXIT_SUCCESS;
    } catch (exception& e) {
        cerr << e.what() << endl;
        if (inf.is_open()) inf.close();
    } catch (...) {
        cerr << "Unknown exception caught!" << endl;
        if (inf.is_open()) inf.close();
    }

    return EXIT_FAILURE;
}

void handle_arrival(stack<Car*>& parking_lot, const string& plate) {
    //判斷是否棧滿
    if ((int) parking_lot.size() == NUMBER_OF_PARKING_SPOTS) {
        //滿則輸出警告
        cout << "Sorry " << plate << ", the lot is full\n";

    } else {
        //不滿則創建Car對象再用Stack的push方法進棧
        Car* arriving_car = new Car(plate);
        parking_lot.push(arriving_car);
    }
}

void handle_departure(stack<Car*>& parking_lot, const string& plate) {

    stack<Car*> the_street;
    //當棧頂的型號和要退出的不一致時,讓頂部的先出去再壓入另外的那個臨時棧
    while (parking_lot.top()->get_license_plate() != plate) {
        //車移動次數加一,直到和參數的plate相同,然後把棧頂的那個車拿出來
        parking_lot.top()->times_moved()++;
        //把停車場棧頂的車壓入臨時棧
        the_street.push(parking_lot.top());
        //停車場棧頂出棧
        parking_lot.pop();
    }
    //棧頂的車並輸入它的型號和移動次數
    Car* departing_car = parking_lot.top();  
    cout << departing_car->get_license_plate() << " was moved "
        << departing_car->times_moved() << " times while it was here\n";
    //棧頂的車出棧
    parking_lot.pop();
    //把已經出棧的車給刪除了
    delete departing_car;
    //然後再把臨時棧的車全部按那個順序再放回停車場棧即可
    while (! the_street.empty()) {
        //臨時棧頂放入停車場棧
        parking_lot.push(the_street.top());
        //臨時棧頂出車
        the_street.pop();
    }
}這樣這個題就算完成了,相信你也對STL的棧有了些認識,所以繼續努力吧

最後附上data.txt的內容

//data.txt

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved