zoukankan      html  css  js  c++  java
  • 作业二

    电梯类

    承接上次作业,浅谈一波面向过程和面向对象的区别
    面向过程,是分析问题解决步骤,并一步一步按照逻辑顺序用代码实现下来
    面向对象是将构成问题的要素抽象出来,概括物体的属性和行为,内化为对象的变量和方法。

    以下是 大冰 - Essay的心得

    面向过程与面向对象的优缺点
    • 面向过程
        优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源,比如单片机、嵌入式开发、Linux/Unix等一般采用面向过程开发,性能是最重要的因素。
        缺点:没有面向对象易维护、易复用、易扩展
    • 面向对象
        优点:易维护、易复用、易扩展,由于面向对象有封装、继承、多态性的特性,可以设计出低耦合的系统,使系统更加灵活、更加易于维护
        缺点:性能比面向过程低

    然后写了一个挺水的电梯类作业,方法和变量都不是挺多,刚好实现作业二的需求而已,也没有很深究逻辑的合理性。
    写一个电梯类外加脑补的超简单的乘客类;

    乘客类:

    #pragma once
    #include<string>
    using namespace std;
    class Passenger
    {
    public:
    	int time, location, destination;
    	string status;
    	Passenger() ;
    	~Passenger();
    };
    

    电梯类:

    #pragma once
    #include"Passenger.h"
    #include<string>
    #include<queue>
    #include<iostream>
    #include<fstream>
    using namespace std;
    class Elevator
    {
    private:
    	int time;												//当前时间
    	int floor;												//当前楼层
    	int destination;										//运行方向(1上0下)
    	queue <int> destination_queue;							//请求队列
    	int load;												//当前负载
    public:
    	Elevator() {
    		time = floor = 0;
    	};
    	~Elevator();
    	void currentTime();										//显示当前时间
    	void currentFloor();										//显示当前楼层
    	void updateTime(Passenger a);							//更新时间
    	void updateFloor(Passenger a);							//更新电梯楼层
    };
    

    电梯类的方法:

    #include "Elevator.h"
    
    
    ifstream infile("input.txt");
    ofstream outfile("output.txt");
    
    Elevator::~Elevator()
    {
    }
    void Elevator::currentTime() {
    	outfile << "当前时间:" << time << endl;
    }
    void Elevator::currentFloor() {
    	cout << "当前楼层:" << floor << endl;
    }
    void Elevator::updateTime(Passenger a) {//时间为当前时间加电梯运行到请求楼层的时间加停留接客时间加电梯运行到目的地时间加电梯停留送客时间
    	time = time + abs(floor - a.location) + 1 + abs(a.destination - a.location) + 1;
    }
    void Elevator::updateFloor(Passenger a) {
    	floor = a.destination;
    }
    

    main函数:

    #include"Elevator.h"
    #include<iostream>
    #include "main.h"
    int main() {
    	ifstream infile("input.txt");	
    	ofstream outfile("output.txt");
    	int n;
    	infile >> n;
    	Elevator elevator;
    	Passenger* man = new Passenger[n + 1];//创建动态对象数组
    	for (int i = 1;i!=EOF;i++) {//输入
    		infile >> man[i].time >> man[i].location >> man[i].destination;
    	}
    	for (int i = 1;i <= n;i++) {
    		elevator.updateTime(man[i]);
    		elevator.updateFloor(man[i]);
    		elevator.currentFloor();
    	}
    

    类图如下 ↓

    欢迎各位提出意见和建议~~

    THE END


  • 相关阅读:
    window安装php的mongodb扩展
    Android NDK开发步骤(r9)
    osgViewer销毁bug
    C/C++ typedef用法
    Java并发编程:Lock
    java中构造器(Constructor)
    进程和线程关系及区别
    Java总结篇系列:Java泛型
    在powerdesigner中创建物理数据模型
    非常好的理解遗传算法的例子
  • 原文地址:https://www.cnblogs.com/elis/p/8999345.html
Copyright © 2011-2022 走看看