c++作业2

从c中看电梯

寒假写的电梯写的很不认真 觉得写的很差 后来思考了下觉得c打电梯应该需要用结构体 就我寒假打的代码 我是一个用户一个用户的搭载 然后计算总时间

从c++看电梯

没有按照老师给的queue < int > destinationQueue; //目的地队列 int currentFloor; //当前位置 int indicator; //运行方向 goToFloor() //前往队列首的楼层 stop() //停靠在当前楼层 updateDestinationQueue() //更新目的地队列 这个样例来 自己写了个构造函数和其他几个函数

区别

c++更高级些 电梯我用c来打内容很多修改也不太清楚

而c++把乘客他们分成一个对象 用c++打的 我觉得看得可能更加清楚把 模块化 但其实还是感觉还不够深刻 特别区别 估计是代码打得少 打多了可能更能理解把


c++代码


#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;

}

猜你喜欢

转载自www.cnblogs.com/ronghuijun/p/8999412.html