Lauda

Untitled

Mar 4th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. // main.cpp
  2. #include "base.hpp"
  3.  
  4. void printFMRadio(const FMRadio &rf)
  5. {
  6.         system("cls");
  7.  
  8.     cout << "*** FM Radio je u stanju: ";
  9.     switch(rf.getState())
  10.     {
  11.         case sON:
  12.             cout << "ON! ***" << endl;
  13.             break;
  14.         case sOFF:
  15.             cout << "OFF! ***" << endl;
  16.             break;
  17.         case sOUT:
  18.             cout << "OUT! ***" << endl;
  19.             break;
  20.         default:
  21.             cout << "N/A ***" << endl;
  22.             break;
  23.     }
  24.  
  25.     cout << "Frekvencija je: " << rf.getFQ() << " MHz" << endl;
  26.     cout << "Jacina zvuka je: " << rf.getVol() << endl;
  27.     cout << endl;
  28.  
  29. }
  30.  
  31. char meni()
  32. {
  33.     char odg;
  34.  
  35.     do
  36.     {
  37.         cout << "Izabrerite opciju :" << endl;
  38.         cout << "1. Ukljuci FMRadio" << endl;
  39.         cout << "2. Iskljuci FMRadio" << endl;
  40.         cout << "3. Pokvari FMRadio" << endl;
  41.         cout << "4. Popravi FMRadio" << endl;
  42.         cout << "5. Povecaj frekvenciju" << endl;
  43.         cout << "6. Smanji frekvenciju" << endl;
  44.         cout << "7. Povecaj jacinu zvuka" << endl;
  45.         cout << "8. Smanji jacinu zvuka" << endl;
  46.         cout << "9. Kraj rada" << endl;
  47.         cin >> odg;
  48.     }
  49.     while (odg < '1' || odg > '9');
  50.  
  51.     return odg;
  52. }
  53.  
  54. int main()
  55. {
  56.     FMRadio f;
  57.     char ch;
  58.  
  59.     do
  60.     {
  61.         ch = meni();
  62.         switch(ch)
  63.         {
  64.             case '1':
  65.             if (f.turnOn())
  66.                 cout << "Operacija izvrsena!" << endl;
  67.             else
  68.                 cout << "Operacija nije izvrsena!" << endl;
  69.                 printFMRadio(f);
  70.                 break;
  71.  
  72.             case '2':
  73.                 if (f.turnOff())
  74.                     cout << "Operacija izvrserna!" << endl;
  75.                 else
  76.                     cout << "Operacija nije izvrsena!" << endl;
  77.                     printFMRadio(f);
  78.                     break;
  79.  
  80.             case '3':
  81.                 if (f.turnOut())
  82.                     cout << "Operacija izvrsena!" << endl;
  83.                 else
  84.                     cout << "Operacija nije izvrsena!" << endl;
  85.                     printFMRadio(f);
  86.                     break;
  87.  
  88.                 case '4':
  89.                     if (f.repair())
  90.                         cout << "Operacija izvrsena!" << endl;
  91.                     else
  92.                         cout << "Operacija nije izvrsena!" << endl;
  93.                         printFMRadio(f);
  94.                         break;
  95.  
  96.                 case '5':
  97.                     if (f.incF())
  98.                         cout << "Operacija izvrsena!" << endl;
  99.                     else
  100.                         cout << "Operacija nije izvrsena!" << endl;
  101.                         printFMRadio(f);
  102.                         break;
  103.  
  104.                 case '6':
  105.                     if (f.decF())
  106.                         cout << "Operacija izvrsena!" << endl;
  107.                     else
  108.                         cout << "Operacija nije izvrsena!" << endl;
  109.                         printFMRadio(f);
  110.                         break;
  111.  
  112.                 case '7':
  113.                     if (f.incV())
  114.                         cout << "Operacija izvrsena!" << endl;
  115.                     else
  116.                         cout << "Operacija nije izvrsena!" << endl;
  117.                         printFMRadio(f);
  118.                         break;
  119.  
  120.                 case '8':
  121.                     if (f.decV())
  122.                         cout << "Operacija izvrsena!" << endl;
  123.                     else
  124.                         cout << "Operacija nije izvrsena!" << endl;
  125.                         printFMRadio(f);
  126.                         break;
  127.         }
  128.     }
  129.     while (ch != '9');
  130.  
  131.     return 0;
  132.  
  133. }
  134.  
  135. // radio.hpp
  136. #ifndef RADIO_HPP_INCLUDED
  137. #define RADIO_HPP_INCLUDED
  138.  
  139. #include <iostream>
  140. #include <cstdlib>
  141. #include <math.h>
  142.  
  143. using namespace std;
  144.  
  145. // Config:
  146. #define MINF 87.5
  147. #define MAXF 108.0
  148. #define MINV 0
  149. #define MAXV 20
  150. #define PV 1
  151. #define PF 0.5
  152.  
  153. enum stanje
  154. {
  155.     sON, sOFF, sOUT
  156. };
  157.  
  158. class FMRadio
  159. {
  160.     private:
  161.         stanje state;
  162.         double FQ;
  163.         int vol;
  164.  
  165.     public:
  166.         // Konstruktor + Konstruktor kopije!
  167.         FMRadio();
  168.         FMRadio(const FMRadio&);
  169.  
  170.         // Metode!
  171.         stanje getState() const;
  172.         int getVol() const;
  173.         double getFQ() const;
  174.         bool turnOn();
  175.         bool turnOff();
  176.         bool turnOut();
  177.         bool repair();
  178.         bool incV();
  179.         bool decV();
  180.         bool incF();
  181.         bool decF();
  182.  
  183. };
  184.  
  185.  
  186.  
  187. #endif // RADIO_HPP_INCLUDED
  188.  
  189. // radio.cpp
  190. #include "radio.hpp"
  191.  
  192. FMRadio :: FMRadio()
  193. {
  194.     state = sOFF;
  195.     FQ = MINF;
  196.     vol = MINV;
  197. }
  198.  
  199. FMRadio :: FMRadio(const FMRadio &rf)
  200. {
  201.     state = rf.state;
  202.     FQ = rf.FQ;
  203.     vol = rf.vol;
  204. }
  205.  
  206. stanje FMRadio :: getState() const
  207. {
  208.     return state;
  209. }
  210.  
  211. double FMRadio :: getFQ() const
  212. {
  213.     return FQ;
  214. }
  215.  
  216. int FMRadio :: getVol() const
  217. {
  218.     return vol;
  219. }
  220.  
  221. bool FMRadio :: turnOn()
  222. {
  223.     if (state == sOFF)
  224.     {
  225.         state = sON;
  226.         return true;
  227.     }
  228.     else
  229.         return false;
  230. }
  231.  
  232. bool FMRadio :: turnOff()
  233. {
  234.     if (state == sON)
  235.     {
  236.         state = sOFF;
  237.         return true;
  238.     }
  239.     else
  240.         return false;
  241. }
  242.  
  243. bool FMRadio :: turnOut()
  244. {
  245.     if (state == sON || state == sOFF)
  246.     {
  247.         state = sOUT;
  248.         FQ = -1;
  249.         vol = -1;
  250.         return true;
  251.     }
  252.     else
  253.         return false;
  254. }
  255.  
  256. bool FMRadio :: repair()
  257. {
  258.     if (state == sOUT)
  259.     {
  260.         state = sOFF;
  261.         return true;
  262.     }
  263.     else
  264.         return false;
  265. }
  266.  
  267. bool FMRadio :: incV()
  268. {
  269.     if (state == sON && vol+PV <= MAXV)
  270.     {
  271.         vol+=PV;
  272.         return true;
  273.     }
  274.     else
  275.         return false;
  276. }
  277.  
  278. bool FMRadio :: decV()
  279. {
  280.     if (state == sON && vol-PV >= MINV)
  281.     {
  282.         vol-=PV;
  283.         return true;
  284.     }
  285.     else
  286.         return false;
  287. }
  288.  
  289. bool FMRadio :: incF()
  290. {
  291.     if (state == sON && FQ+PF <= MAXF)
  292.     {
  293.         FQ+=PF;
  294.         return true;
  295.     }
  296.     else
  297.         return false;
  298. }
  299.  
  300. bool FMRadio :: decF()
  301. {
  302.     if (state == sON && FQ-PF >= MINF)
  303.     {
  304.         FQ-=PF;
  305.         return true;
  306.     }
  307.     else
  308.         return false;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment