Having trouble with html conversions....

Hi, so i've been given an assignment which asks me to simulate traffic on a road, the best output would be if there was a road with two lanes going in different directions, where the cars are of different speeds and the cars are able to overtake other vehicles, this is to be coded in c++. However, I've mostly worked with html and converting it into c++ is a proving to be more than a problem. I'll include the html code followed by the converted c++ code.

Road.php

<?php

class Road {

public $time = 0; // seconds

public $lanes = array();

public function addLane($lane) {
$this->lanes[] = $lane;
}

public function runSimulation($simulationTime) {
while ($this->time < $simulationTime) {
echo "Time: " . $this->time . " seconds\n";
foreach ($this->lanes as $laneNumber => $currentLane) {
echo " - Lane " . $laneNumber . ":\n";
$output = $currentLane->updateVehicles();
}
sleep(0.01); // seconds
$this->time++;
};
}

}

Lane.php

<?php

class Lane {

public $direction;

public $length;

public $speedLimit;

public $vehicleTypes;

public $vehicleQueue;

public function __construct($vehicleTypes) {
$this->vehicleTypes = $vehicleTypes;
$this->addVehicle($this->speedLimit);
}

public function updateVehicles() {
foreach ($this->vehicleQueue as $index => $vehicle) {
echo " - Vehicle " . $index . ": " . $vehicle->position . " metres (" . $vehicle->speed . ")\n";
$vehicle->updatePosition();
}
}

public function addVehicle() {
$vehicle = $this->vehicleTypes->getRandomVehicleType();
$vehicle->speed = $this->speedLimit;
$this->vehicleQueue[] = $vehicle;
}

public function isVehicleToBeAdded() {
$random = rand(1, 100);
if ($random >= 98) {
return true;
} else {
return false;
}
}

public function kilometerToMeter($length) {
return $length * 1000;
}

public function draw() {
return str_repeat('.', $this->kilometerToMeter($this->length));
}
}

Vehicle.php

<?php

class Vehicle {

public $speed = 0;

public $acceleration = null;

public $position = 0;

public $initialSpeed = 0;

public function updatePosition() {
$this->position += $this->speed; // put real calculation
}

}

VehicleTypes.php

<?php

class VehicleTypes {

public $vehicleTypes = array();

public function addVehicleType($vehicle) {
$this->vehicleTypes[] = $vehicle;
}

public function getRandomVehicleType() {
$randomIndex = array_rand($this->vehicleTypes);
return $this->vehicleTypes[$randomIndex];
}

}

traffic.php

<?php

// Automatically loads files when instantiating missing classes.
// eg. `new Car()` will require `classes/Car.php` which should define `class Car {}`
function __autoload($className) {
require_once('classes' . DIRECTORY_SEPARATOR . $className . '.php');
}

// Types of cars according to DVLA, speeds in km/h

function main() {

$car = new Vehicle();
$car->acceleration = 0.84; // m/s^2
$car->deceleration = 1.18;
$car->speedPercentage = 0.98; // % of speed limit

$truck = new Vehicle();
$truck->acceleration = 0.29;
$truck->deceleration = 0.79;
$truck->speedPercentage = 0.95;

$van = new Vehicle();
$van->acceleration = 0.83;
$van->deceleration = 0.91;
$van->speedPercentage = 0.90;

$bus = new Vehicle();
$bus->acceleration = 0.29;
$bus->deceleration = 0.80;
$bus->speedPercentage = 0.85;

$vehicleTypes = new VehicleTypes();
$vehicleTypes->addVehicleType($car);
$vehicleTypes->addVehicleType($bus);
$vehicleTypes->addVehicleType($truck);
$vehicleTypes->addVehicleType($van);

$lane1 = new Lane($vehicleTypes);
$lane1->direction = 'east';
$lane1->length = 5; // km
$lane1->speedLimit = 30; // km/h

$road = new Road();
$road->addLane($lane1);
$road->runSimulation(60); // seconds

}

// start program
main();

Now here is the c++ (i'm running into so many errors everywhere, some code hasn't been converted yet)

road.h

#include"lane.h"
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<ctime>



class Road
{
private:
int time;
array lanes;
public:
void SetTime(int value)
{
time = 0; // seconds
}
void Setlanes(int value)
{
lanes = [];
}

int function (void *addLane(void *lane))
{
this->lanes[] = lane;
}

public function runSimulation(simulationTime) {
while (this->time < simulationTime) {
echo "Time: " . this->time . " seconds\n";
foreach (this->lanes as laneNumber => currentLane) {
echo " - Lane " . laneNumber . ":\n";
output = currentLane->updateVehicles();
}
sleep(0.01); // seconds
this->time++;
};
}

}

lane.h

#include"traffic.h"
class Lane
{

public: int direction;
int length;
int speedLimit;
int vehicleTypes;
int vehicleQueue;
int function (__construct(vehicleTypes))
{
this->vehicleTypes = vehicleTypes;
this->addVehicle(this->speedLimit);
}

function updateVehicles() {
foreach (this->vehicleQueue as index => vehicle) {
echo " - Vehicle " . index . ": " . vehicle->position . " metres (" . vehicle->speed . ")\n";
vehicle->updatePosition();
}
}

function addVehicle() {
vehicle = this->vehicleTypes->getRandomVehicleType();
vehicle->speed = this->speedLimit;
this->vehicleQueue[] = vehicle;
}

function isVehicleToBeAdded()
{
random = rand(1, 100);
if (random >= 98)
{
return true;
}
else
{
return false;
}
}

public function kilometerToMeter(length) {
return length * 1000;
}

public function draw() {
return str_repeat('.', this->kilometerToMeter(this->length));
}
}

traffic.h
#include"vehicle.h"


// Automatically loads files when instantiating missing classes.
// eg. `new Car()` will require `classes/Car.php` which should define `class Car {}`
function __autoload(className) {
require_once('classes' . DIRECTORY_SEPARATOR . className . '.php');
}

// Types of cars according to DVLA, speeds in km/h
int main()
{

car = new Vehicle();
car->acceleration = 0.84; // m/s^2
car->deceleration = 1.18;
car->speedPercentage = 0.98; // % of speed limit

truck = new Vehicle();
truck->acceleration = 0.29;
truck->deceleration = 0.79;
truck->speedPercentage = 0.95;

van = new Vehicle();
van->acceleration = 0.83;
van->deceleration = 0.91;
van->speedPercentage = 0.90;

bus = new Vehicle();
bus->acceleration = 0.29;
bus->deceleration = 0.80;
bus->speedPercentage = 0.85;

vehicleTypes = new VehicleTypes();
vehicleTypes->addVehicleType(car);
vehicleTypes->addVehicleType(bus);
vehicleTypes->addVehicleType(truck);
vehicleTypes->addVehicleType(van);

lane1 = new Lane(vehicleTypes);
lane1->direction = 'east';
lane1->length = 5; // km
lane1->speedLimit = 30; // km/h

road = new Road();
road->addLane(lane1);
road->runSimulation(60); // seconds

}
// start program
main();

vehicle.h

#include"vehicle types.h"
class Vehicle
{
private:
int speed;
int acceleration;
int position;
int InitialSpeed;
public:
void SetSpeed(int value)
{
speed = 0;
}
void SetAcceleration(int value)
{
acceleration = (NULL);
}
void SetPosition(int value)
{
position = 0;
}
void SetInitialSpeed(int value)
{
InitialSpeed = 0;
}

int updatePosition()
{
this->position += this->speed; // put real calculation
}

}

vehicle types.h
class VehicleTypes {

public:
VehicleTypes();
~VehicleTypes();

int function (void *addVehicleType,void *vehicle)
{
this->VehicleTypes = vehicle;
}

int function (void getRandomVehicleType())
{
randomIndex = array_rand(this->vehicleTypes);
return this->vehicleTypes[randomIndex];
}

}

main.cpp

#include"vehicle types.h"
using namespace std;

Time: 0 seconds
- Lane 0:
class Vehicle3 (6)
{
private:
int Speed
double Acceleration =>
int Position => (0)
int InitialSpeed => (0)
double Deceleration => (0.91)
double SpeedPercentage => (0.9)
public:
void SetSpeed(int value)
{
Speed = NULL;
}

void SetAcceleration(double value)
{
Acceleration = 0.83
}
void SetPosition (int value)
{
Position = 0
}
void SetInitialSpeed (int value)
{
InitialSpeed = 0
}
void SetDeceleration (double value)
{
Deceleration = 0.91
}

int main ()
{
speed = (NULL);
acceleration = (0.83);
Topic archived. No new replies allowed.