PWM Constructor Modes

#include <iostream>
using namespace std;

// Write your PWMController class here
class PWMController{
    int frequency, duty;
    bool enabled;

 public:
      PWMController(int freq){
           frequency = freq;
           duty = 0;
           enabled = false;
       }
       PWMController(int freq, int dutycycle){
           frequency = freq;
            if (dutycycle >100)dutycycle= 100;
            duty = dutycycle;
            enabled = true;
       }
       void setDuty(int d){
        if (d >100) d= 100;
         duty = d;
       }
       void disable(){
            enabled = false;
       }
       void print(){
            cout<<"F="<<frequency<<" D="<<duty<<" EN="<<enabled;
       }

      
};

int main() {
    int mode;
    cin >> mode;

    PWMController pwm(0); // temporary placeholder

    if (mode == 1) {
        int f;
        cin >> f;
        pwm = PWMController(f);
    }
    else {
        int f, d;
        cin >> f >> d;
        pwm = PWMController(f, d);
    }

    int x;
    cin >> x;

    if (x == -1)
        pwm.disable();
    else
        pwm.setDuty(x);

    pwm.print();
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

1 1000 50

Expected Output

F=1000 D=50 EN=0