Object Oriented Programming Assignment 2

Object Oriented Programming Assignment 2

C and C++

C language implementation steps

  1. Read passenger requests (asktime request time, askfloor request floor, askspace request destination)
  2. Analyze passenger requests, judge the current status of the elevator, and respond to passenger requests
  3. Determine whether the elevator has reached the requested floor or the requested destination. If it arrives, the elevator will stop, otherwise it will continue to respond to the request.
  4. If all requests are processed, end

C++ class diagram


Compare the two

From learning C++ during the winter vacation to now, from following the requirements in a cluttered way, simply using C++ syntax but the actual idea is still process-oriented, and now I have roughly understood the difference between process-oriented and object-oriented, which can be said to be a rich experience. learning experience. Let me tell you what I think. If there is something wrong, please give me more advice.

Process-oriented: analyze the steps we need to solve a problem, and then implement these steps step by step with code. For example, in this elevator, we must first receive the passenger's request, analyze the request, and then pick up the passenger and send the passenger to the destination. These steps are clear, and then you have to use code to implement them step by step.

Object-oriented: Analyze the problem we want to solve from another angle, isolate the objects in it, analyze the status of these objects in this problem, and solve the problem according to the constructors of these objects. For example, the elevator problem can be roughly divided into two categories: passengers and elevators. Analyze the status of passengers and elevators in the elevator scheduling process, and solve the problem by changing their status. (Actually, I still feel a little confused)


Elevator

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
class Elevator{
public:
    Elevator();
    ~Elevator();
    int nowfloor;//当前电梯位置
    int indicator; //当前电梯运行方向
    int nowtime;//当前时间
    void gotofloor(int askfloor); //接受到达某一层的指令
    void stop(int time, int floor); //停靠在当前楼层
    void show(int nowfloor); //显示当前所处楼层位置

private:
    int sumtime;//乘客到达目的地之前的总等待时间
    int nowman;//当前在电梯中乘客的总人数
    void ifstop();//判断当前电梯是否需要停靠,如果要,执行stop
};


Elevator::Elevator(){
    nowtime = nowfloor = sumtime = nowman = 0;
    indicator = 1;
}

Elevator:: ~Elevator(){

}

void Elevator::gotofloor(int askfloor){
    nowtime += fabs(nowfloor - askfloor);
    nowfloor = askfloor;
}

void Elevator::show(int nowfloor){
    cout << "nowfloor:"  << nowfloor<< endl;
}

void Elevator::stop(int time, int floor) {
    cout << time << " " << floor << endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325315712&siteId=291194637