From the elevator problem, look at the difference between C and C + + (somewhat understood) illusion illusion

Source: Internet
Author: User

The bumps also successively use C and C + + constructs a lot of elevators. Although not satisfied with the performance of the ego, but overall there is a certain harvest, the difference between C and C + + feel a little bit of the doorway ...

The steps to construct the elevator in C language:


The first step:

Analyze what this elevator needs to do ———— take the passengers to the destination, record the time to complete the operation, and then compare the shortest time

Step Two:

Variables that need to be introduced 1. Storage elevator now on the floor:

2. Time of request made by the passenger: RequestTime

3. The requested floor of the passenger: Requestfloor

4. The passenger is going to the floor: Tofloor

Step Three:

Write function

The first function is the time of operation of the elevator

The second function is the function of the elevator running to the target floor.

Building Elevators with C + +:

We are facing the object of the elevator, not the elevator to the passengers. So we are the construction class, the main purpose is not to complete the problem.

The first step:

Construct elevator This class

(Personal feeling in order to be able to better finish should add passengers this class, is friends, now learn to sell a wave) even if the illusion

Step Two:

Private member variables: The current position of the elevator and the direction of operation of the elevator.

Step Three:

member functions:

Functions that go to the target floor are Tofloor ()

function to calculate elapsed time ()

The lift stops at the destination floor of the guest's function stop ()

Update this elevator nowfloor ()

For the difference between C and C + +:

In my opinion, C language is only for this problem, he is to solve the problem itself is the shortest time, that is, I ask for the elevator to complete the action, the elevator is how, the programmer is not the tube, that is, we write the code and the problem is very close, that is, if there is no problem to mention what code

and for C + +, we are more concerned about the elevator object, the shortest time by elevator is just one of the elevator problems, that is, if the problem is lost elevator this class is still meaningful, that is, after this class we want to solve what problem can directly refer to this class, and other problems together, can also be a perfect fit. This is to satisfy the C + + Spirit ———— comprehensible, testability and modifiable. Let the code become more flexible.

Elevator type:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
using namespace Std;

Class Elevator
{
Public
int flag;//through this to determine whether the elevator is up or down
int currentfloor=0;//Elevator Now on the floor
int ntime=0;//Elevator Time of operation
Elevator ();
~elevator ();
int Nowfloor (int tofloor);//The floor where you are now
int command (int currentfloor, int tofloor, int time);//Command
};

#include "Elevator.h"

Elevator::elevator ()
{
}


Elevator::~elevator ()
{
}

int Elevator::nowfloor (int tofloor)
{
Currentfloor = Tofloor;
return currentfloor;
}

int Elevator::command (int currentfloor, int tofloor, int time)
{
Ntime = time;//The last time a guest was transported
Ntime + = ABS (Tofloor-currentfloor);
ntime++;
Nowfloor (Tofloor);//Update the location where the elevator is now
return ntime;
}

In any case, the feeling is very skilled, I may like to hit the code of this feeling, I may be sick of it ~ ~

From the elevator problem, look at the difference between C and C + + (somewhat understood) illusion illusion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.