Untitled
unknown
c_cpp
a year ago
5.4 kB
2
Indexable
Never
// Include the necessary libraries #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // Define some constants for the tram parameters #define MAX_SPEED 72 // The maximum speed in km/h #define MAX_ACCEL 1.2 // The maximum acceleration in m/s^2 #define MAX_DECEL 2.4 // The maximum deceleration in m/s^2 #define DOOR_TIME 10 // The time in seconds to open or close the doors // Define a structure for the tram state typedef struct { double speed; // The current speed in m/s double distance; // The distance travelled in m double time; // The elapsed time in s bool doors_open; // The flag for the doors status } tram_state; // Define a function to convert km/h to m/s double kmh_to_ms(double kmh) { return kmh / 3.6; } // Define a function to convert m/s to km/h double ms_to_kmh(double ms) { return ms * 3.6; } // Define a function to update the tram state based on the desired speed and distance void update_tram_state(tram_state *state, double desired_speed, double desired_distance) { // Convert the desired speed from km/h to m/s desired_speed = kmh_to_ms(desired_speed); // Calculate the difference between the desired speed and the current speed double speed_diff = desired_speed - state->speed; // Calculate the acceleration or deceleration needed to reach the desired speed double accel = 0; if (speed_diff > 0) { // Accelerate with the maximum acceleration or less if the speed difference is small accel = (speed_diff < MAX_ACCEL) ? speed_diff : MAX_ACCEL; } else if (speed_diff < 0) { // Decelerate with the maximum deceleration or less if the speed difference is small accel = (speed_diff > -MAX_DECEL) ? speed_diff : -MAX_DECEL; } // Calculate the time needed to reach the desired speed with the acceleration or deceleration double time = speed_diff / accel; // Calculate the distance travelled during the acceleration or deceleration double distance = state->speed * time + 0.5 * accel * time * time; // Check if the distance travelled is more than the desired distance if (distance > desired_distance) { // Adjust the time and distance to match the desired distance time = (-state->speed + sqrt(state->speed * state->speed + 2 * accel * desired_distance)) / accel; distance = desired_distance; } // Update the tram state with the new speed, distance, and time state->speed = state->speed + accel * time; state->distance = state->distance + distance; state->time = state->time + time; } // Define a function to simulate a tram ride with a given route and stops void simulate_tram_ride(int route, int stops[]) { // Create a tram state object and initialize it with zero values tram_state state = {0, 0, 0, false}; // Print a message with the route number and the number of stops printf("Simulating tram ride for route %d with %d stops.\n", route, stops[0]); // Loop through each stop in the array (skipping the first element which is the number of stops) for (int i = 1; i <= stops[0]; i++) { // Print a message with the current stop number and distance printf("Stop %d: %d m\n", i, stops[i]); // Update the tram state to reach the maximum speed before the stop update_tram_state(&state, MAX_SPEED, stops[i] - 100); // Print a message with the current speed, distance, and time printf("Speed: %.2f km/h, Distance: %.2f m, Time: %.2f s\n", ms_to_kmh(state.speed), state.distance, state.time); // Update the tram state to stop at the stop update_tram_state(&state, 0, stops[i]); // Print a message with the current speed, distance, and time printf("Speed: %.2f km/h, Distance: %.2f m, Time: %.2f s\n", ms_to_kmh(state.speed), state.distance, state.time); // Update the tram state to open the doors and wait for passengers state.doors_open = true; state.time += DOOR_TIME; // Print a message with the current doors status and time printf("Doors: %s, Time: %.2f s\n", state.doors_open ? "Open" : "Closed", state.time); // Update the tram state to close the doors and resume the ride state.doors_open = false; state.time += DOOR_TIME; // Print a message with the current doors status and time printf("Doors: %s, Time: %.2f s\n", state.doors_open ? "Open" : "Closed", state.time); } // Print a message with the final speed, distance, and time printf("Final speed: %.2f km/h, Final distance: %.2f m, Final time: %.2f s\n", ms_to_kmh(state.speed), state.distance, state.time); } // Define the main function that runs the app int main() { // Define an array of stops for route 1 (the first element is the number of stops) int route1[] = {5, 0, 500, 1200, 2000, 3000}; // Define an array of stops for route 2 (the first element is the number of stops) int route2[] = {4, 0, 800, 1500, 2500}; // Simulate a tram ride for route 1 simulate_tram_ride(1, route1); // Simulate a tram ride for route 2 simulate_tram_ride(2, route2); // Return zero to indicate successful execution return 0; }