c++ homework 2

Elevator seen from c

The elevator I wrote during the winter vacation was not serious. I thought it was poorly written. After thinking about it, I thought that I should use a structure to hit the elevator. Just the code I wrote during the winter vacation, I was a user and a user. Then calculate the total time.

Looking at the elevator from c++

Not according to the queue given by the teacher < int > destinationQueue; //destination queue int currentFloor; //current position int indicator; //running direction goToFloor() //go to the floor at the head of the queue stop() //stop at the current floor updateDestinationQueue () //Update the destination queue This example writes a constructor and several other functions

the difference

C++ is more advanced. I use c to type a lot of changes and I don't know much about it.

And c++ divides the passengers into one object. I think it may be more clear to see the modularization, but in fact, I still feel that it is not deep enough. The special difference is that the code is typed less and more, which may be more understandable.


c++ code


#include<iostream>

#include <fstream>

using namespace std;

class Elevator {

public:

int destination;

int currentFloor;

Elevator()

{

destination = 0;

currentFloor = 0;

}

int getdestination()

{

return destination;

}

int getcurrentFloor()

{

return currentFloor;

}

void setdestination(int d)

{

destination = d;

}

void setcurrentFloor(int c)

{

currentFloor = c;

}

int time(int d, int b, int t)

{


if (d < b)

t += b - d;

else

t += d - b;

cout << t << " " << b << endl;

return t;

}

int time2(int c, int b, int t)

{

if (b > c)

t += b - c;

else

t += c - b;

cout << t << " " << c << endl;

return t;

}

};

int main()

{

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);


int n, i, a, b, c, d, f, t = 0;

cin >> n;

Elevator e;

for (i = 0; i < n; i++)

{

cin >> a >> b >> c;

if (a > t)

t = a;

d = e.getcurrentFloor();

if (d != b)

{

f = e.time(d, b, t);

t = f + 1;

e.setcurrentFloor(b);

}

e.setdestination(c);

f = e.time2(c, b, t);

t = f + 1;

e.setcurrentFloor(c);

}

return 0;

}

Guess you like

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