понедељак, 26. март 2012.

#ifndef FMRADIO_HPP_INCLUDED
#define FMRADIO_HPP_INCLUDED

#include <iostream>
using namespace std;

#define MINF 87.5
#define MAXF 108.0
#define KF 0.5

#define MINV 0
#define MAXV 20
#define KV 1

enum States {sON, sOFF, sOUT};

class FMRadio {
    private:
        States state;
        double freq;
        int vol;
    public:
        FMRadio();
        States getState() const;
        double getFreq() const;
        int getVol() const;
        bool turnOn();
        bool turnOff();
        bool turnOut();
        bool repair();
        bool incF();
        bool decF();
        bool incV();
        bool decV();
};


#endif // FMRADIO_HPP_INCLUDED
------------------------------
#ifndef FMRADIO_CPP_INCLUDED
#define FMRADIO_CPP_INCLUDED

#include "fmradio.hpp"

FMRadio::FMRadio(){
    state=sOFF;
    freq=MINF;
    vol=MINV;
}

States FMRadio::getState() const {return state;}
double FMRadio::getFreq() const {return freq;}
int FMRadio::getVol() const {return vol;}

bool FMRadio::turnOn(){
    if(state==sOFF){
        state=sON;
        return true;
    }
    else
        return false;
}

bool FMRadio::turnOff(){
    if(state==sON){
        state=sOFF;
        return true;
    }
    else
        return false;
}

bool FMRadio::turnOut(){
    if(state!=sOUT){
        state=sOUT;
        freq=-1;
        vol=-1;
        return true;
    }
    else
        return false;
}

bool FMRadio::repair(){
    if(state==sOUT){
        state=sOFF;
        freq=MINF;
        vol=MINV;
        return true;
    }
    else
    return false;
}

bool FMRadio::incF(){
    if(state==sON && freq+KF>=MINF){
        freq+=KF;
        return true;
    }
    else
        return false;
}

bool FMRadio::decF(){
    if(state==sON && freq-KF>=MINF){
        freq-=KF;
        return true;
    }
    else
        return true;
}

bool FMRadio::incV(){
    if(state==sON && vol+KV<=MAXV){
        vol+=KV;
        return true;

    }
    else
        return false;
}

bool FMRadio::decV(){
    if(state==sON && vol-KV>=MINV){
        vol-=KV;
        return true;
    }
    else
        return false;
}

#endif // FMRADIO_CPP_INCLUDED
===================================

#include "fmradio.hpp"

void printFMRadio(const FMRadio &rf) {
    cout<<"*** FMRadio je u stanju:";
    switch(rf.getState()){
        case sON: cout<<"ON"<<endl; break;
        case sOFF: cout<<"OFF"<<endl; break;
        case sOUT: cout<<"OUT"<<endl; break;
    }
    cout<<"*** Frekvencija: "<<rf.getFreq()<<endl;
    cout<<"*** Jacina zvuka: "<<rf.getVol()<<endl;

}

char meni(){
    char odg;
    do {
        cout<<"Izaberi opciju: "<<endl;
        cout<<"1. Ukljuci FMRadio"<<endl;
        cout<<"2. Iskljuci FMRadio"<<endl;
        cout<<"3. Pokvari FMRadio"<<endl;
        cout<<"4. Popravi FMRadio"<<endl;
        cout<<"5. Povecaj frekvenciju"<<endl;
        cout<<"6. Smanji frekvenciju"<<endl;
        cout<<"7. Povecaj jacinu zvuka"<<endl;
        cout<<"8. Smanji jacinu zvuka"<<endl;
        cout<<"9. Kraj rada"<<endl;
        cin>>odg;
    }   while(odg<'1' || odg>'9');
    return odg;
}

int main() {
    FMRadio f;
    char ch;
    do {
        ch=meni();
        switch(ch){
            case'1': if(f.turnOn())
                        cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
            case'2': if(f.turnOff())
                         cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
            case'3': if(f.turnOut())
                         cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
            case'4': if(f.repair())
                         cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
            case'5': if(f.incF())
                         cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
            case'6': if(f.decF())
                         cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
            case'7': if(f.incV())
                         cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
            case'8': if(f.decV())
                         cout<<"Operacija izvrsena!"<<endl;
                    else
                        cout<<"Operacija nije izvrsena!"<<endl;
                    printFMRadio(f);
                    break;
        }
    }   while(ch!='9');
    return 0;
}

Нема коментара:

Постави коментар