Grupo
Programación en Tiempo Real en C++
Ojetivo
Escriba un programa en C++ que simule el control en tiempo real de una máquina de producción. El programa debe simular los estados de la máquina (Inactiva, En ejecución, Detenida) y actualizar su estado a intervalos regulares. Permita al usuario introducir el tiempo que la máquina permanece en cada estado y ejecutarla continuamente, cambiando sus estados según los intervalos especificados.
Escriba un programa que simule el control de una máquina de producción en tiempo real.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the input-output stream library
#include <thread> // Include the thread library to control time intervals
#include <chrono> // Include the chrono library for time manipulation
using namespace std;
// Function to simulate the production machine control system
void productionMachineControl(int idleDuration, int runningDuration, int stoppedDuration) {
while (true) { // Infinite loop to simulate continuous machine control
// Simulate Idle State
cout << "Machine is IDLE - Not in operation" << endl;
this_thread::sleep_for(chrono::seconds(idleDuration)); // Wait for the specified idle duration
// Simulate Running State
cout << "Machine is RUNNING - Production in progress" << endl;
this_thread::sleep_for(chrono::seconds(runningDuration)); // Wait for the specified running duration
// Simulate Stopped State
cout << "Machine is STOPPED - Production halted" << endl;
this_thread::sleep_for(chrono::seconds(stoppedDuration)); // Wait for the specified stopped duration
}
}
int main() {
int idleDuration, runningDuration, stoppedDuration;
// Ask the user to input the time durations for each machine state
cout << "Enter the duration for IDLE state in seconds: ";
cin >> idleDuration;
cout << "Enter the duration for RUNNING state in seconds: ";
cin >> runningDuration;
cout << "Enter the duration for STOPPED state in seconds: ";
cin >> stoppedDuration;
// Start the production machine control system with the user-defined durations
productionMachineControl(idleDuration, runningDuration, stoppedDuration);
return 0;
}
Salida
Enter the duration for IDLE state in seconds: 3
Enter the duration for RUNNING state in seconds: 5
Enter the duration for STOPPED state in seconds: 2
Machine is IDLE - Not in operation
Machine is RUNNING - Production in progress
Machine is STOPPED - Production halted
...
Comparte este ejercicio C++