📄 rail_vehicle.cpp
字号:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "rail_vehicle.h"
using namespace std;
Cargo::Cargo(string name_, int mass_)
{
mass = mass_;
name = name_;
nextCargo = NULL;
cargoPtr = NULL;
}
Cargo::~Cargo()
{
}
Cargo* Cargo::findLastCargo( Cargo *temp)
{
if(temp->nextCargo == NULL)
{
return temp;
}
else
{
return (findLastCargo(temp->nextCargo));
}
}
int Cargo::getMassThis()
{
return mass;
}
FreightCar::FreightCar(int mass_)
{
mass = mass_;
cargoPtr = NULL;
front = NULL;
back = NULL;
typeOfVehicle = 2;
}
FreightCar::~FreightCar()
{
while(cargoPtr != NULL)
{
Cargo *temp = cargoPtr;
delete temp;
cargoPtr = cargoPtr->nextCargo;
}
}
void FreightCar::addFreight(Cargo *loadPtr)
{
Cargo *temp;
if (getSpeed()!= 0)
{
throw MyException("Train is moving - u can't add cargo");
}
if(loadPtr->car != NULL)
{
throw MyException("Cargo is already loaded");
}
loadPtr->nextCargo = cargoPtr;
loadPtr->car = this;
cargoPtr = loadPtr;
}
Cargo* FreightCar::removeFreight()
{
Cargo *temp = NULL;
if (cargoPtr != NULL)
{
cargoPtr->car = NULL;
temp = cargoPtr;
cargoPtr = cargoPtr->nextCargo;
temp->nextCargo = NULL;
}
return temp;
}
void FreightCar::showCargo(ostream &o)
{
Cargo *temp = cargoPtr;
while(temp != NULL)
{
o << temp->name << "mass " << temp->mass << endl;
}
}
int FreightCar::getMassThis()const
{
int temp = mass;
Cargo *tempPtr = cargoPtr;
while(tempPtr != NULL)
{
temp += tempPtr->getMassThis();
//temp +=temp->mass;
tempPtr = tempPtr->nextCargo;
}
return temp;
}
void FreightCar::showThis(ostream &o)const
{
o <<"towarowy"<<endl;
o <<"weigt total " << getMassThis() <<endl
<<"weight " << mass <<endl;
}
int Engine::checkMaxSpeedAvailable()
{
float tempMass = getMassAll();
tempMass = tempMass / maxWeight ;
return (maxSpeed - tempMass * maxSpeed /2);
}
Engine::Engine(int mass_, int maxSpeed_, int maxWeight_)
{
mass = mass_;
maxSpeed = maxSpeed_;
maxWeight = maxWeight_;
front = NULL;
back = NULL;
typeOfVehicle = 1;
currentSpeed = 0;
}
Engine::~Engine()
{
}
void Engine::changeSpeed(int value)
{
if(value >=0 && value < checkMaxSpeedAvailable())
{
currentSpeed = value;
}
else
{
throw MyException("Speed is large enough");
}
}
int Engine::getCurrentSpeed()
{
return currentSpeed;
}
int Engine::getMaxSpeed()
{
return maxSpeed;
}
int Engine::getMassThis()const
{
return mass;
}
void Engine::showThis(ostream& o)const
{
o <<"Engine" << endl;
o <<"weight " << mass <<endl
<<"max speed " << maxSpeed <<endl
<<"max weight" << maxWeight<<endl;
/*if (front == NULL)
{
o << "FRONT: NONE";
}
else
{
o << "front connected";
}
if (back == NULL)
{
o << "Back: NONE";
}
else
{
o << "back connected";
}*/
}
int Engine::getMaxWeight()
{
return maxWeight;
}
/*RailVehicle::RailVehicle()
{
front = NULL;
back = NULL;
typeOfVehicle = 0;
}*/
RailVehicle::~RailVehicle()
{
if(back != NULL)
{
back->front = NULL;
}
if(front != NULL)
{
front->back = NULL;
}
}
int RailVehicle::getMassAll()
{
int massAll = 0;
RailVehicle *first;
first = getFirst();
while(first->back != NULL)
{
massAll += first->getMassThis();
first = first->back;
}
//massAll += tempPtr->getMassThis();
return massAll;
}
int RailVehicle::getSpeed()
{
const RailVehicle *first ;
first = getFirst();
if(first->typeOfVehicle == 1)
{
return((Engine *)(first))->getCurrentSpeed();
}
else
{
//throw MyException("No engine in front");
//cout<<"no engine in front"<<endl;
return 0;
}
}
RailVehicle* RailVehicle::connectToNext(RailVehicle *nextPtr)
{
if(nextPtr->getSpeed() !=0 && getSpeed() != 0)
{
throw MyException("Cannot connect vehicle. Train is in motion!");
}
if(back != NULL)
{
if(nextPtr == NULL)
{
back->front = NULL;
back = NULL;
return NULL;
}
else
{
throw MyException("Vehicle already connected");
}
}
back = nextPtr;
back->front = this;
return back;
}
RailVehicle* RailVehicle::getFirst()
{
RailVehicle *first = this;
while(first->front != NULL)
{
first = first->front;
}
return first;
}
void RailVehicle::showAll(ostream &o)
{
RailVehicle *first;
first = getFirst();
while(first != NULL)
{
first->showThis(o);
first = first->back;
}
//first->showThis(o);
cout << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -