📜  门| GATE CS 2013 |问题26(1)

📅  最后修改于: 2023-12-03 14:58:19.913000             🧑  作者: Mango

GATE CS 2013 Question 26

Introduction

This problem from the GATE CS 2013 exam asks the candidate to write a program in C++ to simulate the working of a two-way traffic intersection equipped with traffic lights.

Problem statement

The program should simulate the following operations:

  • Two lanes of traffic, one moving in each direction, intersect at the intersection.
  • Each lane has its own traffic light.
  • When the traffic light is green, traffic can move through the intersection in that direction.
  • When the traffic light is red, traffic must stop.
  • The traffic lights alternate between green and red: when one light is green, the other is red.
  • The time taken for each light cycle should be configurable.
  • The program should print a message to the console indicating which lane is currently permitted to move through the intersection.
Solution

The solution to this problem is a straightforward implementation of the rules outlined in the problem statement:

  • Create two traffic lights, one for each lane.
  • Create a loop that runs indefinitely, simulating the passage of time.
  • In each iteration of the loop, check the elapsed time since the last light change.
  • If the time is greater than the configured cycle time, change the lights: if the first was green, set it to red and vice versa; if it was red, set it to green.
  • Print a message to the console indicating which light is currently green.
#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

const int CYCLE_TIME = 10; // in seconds

enum TrafficLightState {
    GREEN,
    YELLOW,
    RED
};

class TrafficLight {
    public:
        TrafficLight() {
            state = GREEN;
            startTime = chrono::system_clock::now();
        }

        void setState(TrafficLightState newState) {
            state = newState;
            startTime = chrono::system_clock::now();
        }

        TrafficLightState getState() const {
            return state;
        }

        bool isGreen() const {
            return state == GREEN;
        }

        bool isRed() const {
            return state == RED;
        }

        bool isYellow() const {
            return state == YELLOW;
        }

        void tick() {
            auto elapsed = chrono::system_clock::now() - startTime;
            if (elapsed > chrono::seconds(CYCLE_TIME)) {
                if (state == GREEN) {
                    state = YELLOW;
                } else if (state == YELLOW) {
                    state = RED;
                } else {
                    state = GREEN;
                }
                startTime = chrono::system_clock::now();
            }
        }

    private:
        TrafficLightState state;
        chrono::system_clock::time_point startTime;
};

int main() {
    TrafficLight northSouth;
    TrafficLight eastWest;

    while (true) {
        cout << "North-South traffic ";
        if (northSouth.isGreen()) {
            cout << "can pass." << endl;
        } else {
            cout << "must stop." << endl;
        }

        cout << "East-West traffic ";
        if (eastWest.isGreen()) {
            cout << "can pass." << endl;
        } else {
            cout << "must stop." << endl;
        }

        northSouth.tick();
        eastWest.tick();

        this_thread::sleep_for(chrono::seconds(1));
    }

    return 0;
}

The code above creates a TrafficLight class to model each traffic light. The TrafficLight class has a state (either GREEN, YELLOW, or RED), a start time (when the state was last changed), and methods for changing the state and updating the state based on the elapsed time.

The main loop of the program alternates between checking the northSouth and eastWest traffic lights and printing a message indicating whether traffic is permitted to pass. The loop calls the tick() method of each TrafficLight, which updates the state if the elapsed time exceeds the configured cycle time.

The program also sleeps for one second at the end of each iteration of the loop to simulate the passage of time.

Conclusion

This solution provides a basic implementation of a two-way traffic intersection with traffic lights. The program can be customized by adjusting the CYCLE_TIME constant to change the length of each light cycle.