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

semafor

#ifndef SEMAPHORE_HPP_INCLUDED
#define SEMAPHORE_HPP_INCLUDED
#include <iostream>
using namespace std;

enum States{sON, sOFF, sBLINK, sOUT};
enum Colours{cNONE, cBLINK, cRED, cYELLOWRED, cGREEN, cYELLOW};


class Semaphore {
    private:
        States state;
        Colours colour;
    public:
        Semaphore();
        States getState() const;
        Colours getColour() const;
        bool turnOn();
        bool turnOff();
        bool turnBlink();
        bool turnOut();
        bool repair();
        bool changeColour();

};


#endif // SEMAPHORE_HPP_INCLUDED
-------------------------------
#ifndef SEMAPHORE_CPP_INCLUDED
#define SEMAPHORE_CPP_INCLUDED

#include "semaphore.hpp"

Semaphore::Semaphore() {
    state=sOFF;
    colour=cNONE;
}

States Semaphore::getState() const {return state;}
Colours Semaphore::getColour() const {return colour;}

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

bool Semaphore::turnOff(){
    if(state==sON || state==sBLINK){
        state=sOFF;
        colour=cNONE;
        return true;
    }
    else
        return false;
}

bool Semaphore::turnBlink(){
    if(state==sOFF){
        state=sBLINK;
        colour=cBLINK;
        return true;
    }
    else
        return false;
}

bool Semaphore::turnOut(){
    if(state==sOFF || state==sON || state==sBLINK){
        state=sOUT;
        colour=cNONE;
        return true;
    }
    else
        return false;
}

bool Semaphore::repair(){
    if(state==sOUT){
        state=sOFF;
        colour=cNONE;
        return true;
    }
    else
        return false;
}

bool Semaphore::changeColour(){
    if(state==sON){
        switch(colour){
            case cRED:colour=cYELLOWRED; break;
            case cYELLOWRED:colour=cGREEN; break;
            case cGREEN:colour=cYELLOW; break;
            case cYELLOW:colour=cRED; break;
        }
        return true;
    }
    else
        return false;
}

#endif // SEMAPHORE_CPP_INCLUDED
-------------------------------------
#include "semaphore.hpp"

void printSemaphore(const Semaphore &rs){
    cout<<"*** Stanje semafora:";
    switch(rs.getState()){
        case sON:cout<<"ON"<<endl; break;
        case sOFF:cout<<"OFF"<<endl; break;
        case sOUT:cout<<"OUT"<<endl; break;
        case sBLINK:cout<<"BLINK"<<endl; break;
    }
    cout<<"*** Boja:";
    switch(rs.getColour()){
        case cNONE:cout<<"NONE"<<endl; break;
        case cBLINK:cout<<"BLINK"<<endl; break;
        case cRED:cout<<"RED"<<endl; break;
        case cYELLOWRED:cout<<"YELLOWRED"<<endl; break;
        case cGREEN:cout<<"GREEN"<<endl; break;
        case cYELLOW:cout<<"YELLOW"<<endl; break;

    }
}

char meni(){
    char odg;
    do{
        cout<<"Izaberi opciju:\n"<<endl;
        cout<<"1. Ukljuci semafor"<<endl;
        cout<<"2. Iskljuci semafor"<<endl;
        cout<<"3. Ukljuci trepcuce svetlo"<<endl;
        cout<<"4. Pokvari semafor"<<endl;
        cout<<"5. Popravi semafor"<<endl;
        cout<<"6. Promeni boju"<<endl;
        cout<<"7. Kraj rada"<<endl;
        cin>>odg;

    }   while(odg<'1' || odg>'7');
    return odg;
}

int main()
{

    Semaphore s;
    char ch;
    do{
        ch=meni();
        switch(ch){
            case'1': if(s.turnOn())
                        cout<<"\nOperacija izvrsena!\n"<<endl;
                    else
                        cout<<"\nOperacija nije izvrsena!\n"<<endl;
                    printSemaphore(s);
                    break;
            case'2': if(s.turnOff())
                        cout<<"\nOperacija izvrsena!\n"<<endl;
                    else
                        cout<<"\nOperacija nije izvrsena!\n"<<endl;
                    printSemaphore(s);
                    break;
            case'3': if(s.turnBlink())
                         cout<<"\nOperacija izvrsena!\n"<<endl;
                    else
                        cout<<"\nOperacija nije izvrsena!\n"<<endl;
                    printSemaphore(s);
                    break;
            case'4': if(s.turnOut())
                         cout<<"\nOperacija izvrsena!\n"<<endl;
                    else
                        cout<<"\nOperacija nije izvrsena!\n"<<endl;
                    printSemaphore(s);
                    break;
            case'5': if(s.repair())
                         cout<<"\nOperacija izvrsena!\n"<<endl;
                    else
                        cout<<"\nOperacija nije izvrsena!\n"<<endl;
                    printSemaphore(s);
                    break;
            case'6': if(s.changeColour())
                         cout<<"Operacija izvrsena!\n"<<endl;
                    else
                        cout<<"Opreracija nije izvrsena!\n"<<endl;
                    printSemaphore(s);
                    break;
        }
    } while (ch!=7);
    return 0;
}

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

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