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

trougao

#ifndef TROUGAO_HPP_INCLUDED
#define TROUGAO_HPP_INCLUDED
#include <math.h>
#include <iostream>
using namespace std;

class Trougao {
    private:
        double a;
        double b;
        double c;
    public:
        Trougao() {a=3;b=4;c=5;}

        void setA(double aa) {a=aa;}
        void setB(double bb) {b=bb;}
        void setC(double cc) {c=cc;}

        double getA() const {return a;}
        double getB() const {return b;}
        double getC() const {return c;}
        double getO() const;
        double getP() const;
};

#endif // TROUGAO_HPP_INCLUDED
-------------------------------
#ifndef TROUGAO_CPP_INCLUDED
#define TROUGAO_CPP_INCLUDED
#include "trougao.hpp"

double Trougao::getO() const {
    return a+b+c;
}
double Trougao::getP() const {
    double s=(a+b+c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}

#endif // TROUGAO_CPP_INCLUDED
-------------------------------
#include "trougao.hpp"
int main() {
    Trougao t;
    cout<<"Duzina stranice a:"<<t.getA()<<endl;
    cout<<"Duzina stranice b:"<<t.getB()<<endl;
    cout<<"Duzina stranice c:"<<t.getC()<<endl;
    cout<<"Obim:"<<t.getO()<<endl;
    cout<<"Povrsina:"<<t.getP()<<endl;
    return 0;
    }

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;
}

#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;
}