Created
April 17, 2018 16:29
-
-
Save alvinmatias69/2b108e03df5d268f6f8bd0d3dc2f5f5a to your computer and use it in GitHub Desktop.
Queue mini project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include <math.h> | |
#include <ctime> | |
using namespace std; | |
class Car { | |
float init_speed; | |
float safe_speed; | |
float breaking_time; | |
float init_time; | |
public: | |
void set_init_speed (float speed) { | |
init_speed = speed; | |
} | |
float get_init_speed () { | |
return init_speed; | |
} | |
void set_safe_speed (float speed) { | |
safe_speed = speed; | |
} | |
float get_safe_speed () { | |
return safe_speed; | |
} | |
void set_breaking_time (float time) { | |
breaking_time = time; | |
} | |
float get_breaking_time () { | |
return breaking_time; | |
} | |
void set_init_time (float time) { | |
init_time = time; | |
} | |
float get_init_time () { | |
return init_time; | |
} | |
}; | |
float calculate_safe_speed ( | |
float breaking, | |
float reaction_time, | |
float safe_distance, | |
float breaking_distance, | |
float car_speed) { | |
return -breaking * reaction_time + | |
sqrt(pow(breaking, 2) * pow(reaction_time, 2) + pow(car_speed, 2) + 2 * breaking * (breaking_distance - safe_distance)); | |
} | |
int main () { | |
int car_count = 5; | |
const float max_speed = 20.0; | |
const float min_speed = 5.0; | |
const float start_speed = 5.0; | |
const float breaking_constant = 0.1; | |
const float reaction_time = 0.1; | |
const float safe_distance = 0.6; | |
const float breaking_distance = 3.0; | |
Car cars_array [car_count]; | |
cars_array[0].set_init_speed(start_speed); | |
cars_array[0].set_safe_speed(start_speed); | |
cars_array[0].set_breaking_time(0.0); | |
cars_array[0].set_init_time(0.0); | |
float new_car_init_speed; | |
float new_car_safe_speed; | |
float new_car_init_time; | |
float new_car_breaking_time; | |
float old_car_init_distance; | |
for (int idx = 1; idx < car_count; idx++) { | |
new_car_init_speed = (float)(rand() % static_cast<int>( max_speed - min_speed + 1.0 )) + min_speed; | |
old_car_init_distance = cars_array[idx-1].get_init_speed() * cars_array[idx-1].get_breaking_time(); | |
if (old_car_init_distance > breaking_distance) { | |
new_car_init_time = breaking_distance * cars_array[idx-1].get_init_speed() + | |
cars_array[idx-1].get_init_time(); | |
new_car_safe_speed = calculate_safe_speed( | |
breaking_constant, | |
reaction_time, | |
safe_distance, | |
breaking_distance, | |
cars_array[idx-1].get_init_speed() | |
); | |
new_car_breaking_time = (-breaking_distance / new_car_init_speed) / | |
(1 - cars_array[idx-1].get_init_speed() / new_car_init_speed) + | |
new_car_init_time; | |
} else { | |
new_car_init_time = old_car_init_distance * cars_array[idx-1].get_init_speed() + | |
(breaking_distance - old_car_init_distance) * cars_array[idx-1].get_safe_speed() + | |
cars_array[idx-1].get_init_time(); | |
new_car_safe_speed = calculate_safe_speed( | |
breaking_constant, | |
reaction_time, | |
safe_distance, | |
breaking_distance, | |
cars_array[idx-1].get_safe_speed() | |
); | |
new_car_breaking_time = (-breaking_distance / new_car_init_speed) / | |
(1 - cars_array[idx-1].get_safe_speed() / new_car_init_speed) + | |
new_car_init_time; | |
} | |
cars_array[idx].set_init_speed(new_car_init_speed); | |
cars_array[idx].set_safe_speed(new_car_safe_speed < new_car_init_speed | |
? new_car_safe_speed | |
: new_car_init_speed); | |
cars_array[idx].set_init_time(new_car_init_time); | |
cars_array[idx].set_breaking_time(new_car_breaking_time); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment