Hardware Timer Default Constructor

#include <iostream>
using namespace std;

class HardwareTimer {
private:
    int frequency;
    bool enabled;
    int prescaler;

public:
    // Default Constructor
    // Initializes the timer to a safe reset state
    HardwareTimer() {
        frequency = 0;
        enabled = false;
        prescaler = 1;
    }

    // Configures the timer with user values and enables it
    void configure(int freq, int pre) {
        frequency = freq;
        prescaler = pre;
        enabled = true;
    }

    // Disables the timer without changing other settings
    void stop() {
        enabled = false;
    }

    // Displays the state in the required format: F=<freq> P=<pre> EN=<0/1>
    void print() {
        cout << "F=" << frequency 
             << " P=" << prescaler 
             << " EN=" << (enabled ? 1 : 0) << endl;
    }
};

int main() {
    // 1. Create object (Default constructor sets F=0, P=1, EN=0)
    HardwareTimer t; 

    int f, p, x;
    // 2. Read frequency and prescaler
    if (!(cin >> f >> p)) return 0;

    // 3. Configure the timer
    t.configure(f, p);

    // 4. Read stop flag
    cin >> x;
    if (x == 0) {
        t.stop();
    }

    // 5. Final Output
    t.print();
    
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

1000 8 1

Expected Output

F=1000 P=8 EN=1