Advertisement
amarek

OOP 1. kolokvij (26.11.2018.)

Nov 18th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cmath>
  6. #include <fstream>
  7.  
  8. #define DG 0.0
  9. #define GG 10.0
  10.  
  11. using namespace std;
  12.  
  13. // 1. zadatak
  14. class Image {
  15. private:
  16.     int mHeight;
  17.     int mWidth;
  18.     int mBytesPerPixel;
  19. public:
  20.     Image();
  21.     Image(int, int, int);
  22.     void setHeight(int);
  23.     void setWidth(int);
  24.     void setBPP(int);
  25.     int getHeight() const;
  26.     int getWidth() const;
  27.     int getBPP() const;
  28.     void scale(double);
  29.     double getSizeKB();
  30.     double getSizeKB(double);
  31. };
  32.  
  33. void Image::setHeight(int height) {
  34.     mHeight = height;
  35. }
  36.  
  37. void Image::setWidth(int width) {
  38.     mWidth = width;
  39. }
  40.  
  41. void Image::setBPP(int bytesperpixel) {
  42.     mBytesPerPixel = bytesperpixel;
  43. }
  44.  
  45. int Image::getHeight() const { return mHeight; }
  46. int Image::getWidth() const { return mWidth; }
  47. int Image::getBPP() const { return mBytesPerPixel; }
  48.  
  49. // 2. zadatak
  50. Image::Image() : mHeight(0), mWidth(0), mBytesPerPixel(0) {}
  51.  
  52. Image::Image(int height, int width, int bytesperpixel) : mHeight(height), mWidth(width), mBytesPerPixel(bytesperpixel) {}
  53.  
  54. // 3. zadatak
  55. void Image::scale(double sc) {
  56.     if (sc < 0.1) {
  57.         return;
  58.     }
  59.  
  60.     mHeight *= sc;
  61.     mWidth *= sc;
  62. }
  63.  
  64. // 4. zadatak
  65. double Image::getSizeKB() {
  66.     return ((mHeight * mWidth) * mBytesPerPixel) / 1024;
  67. }
  68.  
  69. double Image::getSizeKB(double p) {
  70.     if (!(p >= 10.0 && p <= 90.0)) {
  71.         p = 0;
  72.     }
  73.  
  74.     return getSizeKB() - (getSizeKB() * (p / 100));
  75. }
  76.  
  77. // 5. zadatak
  78. bool funkcija(Image* values, int size, double storage) {
  79.     double max = 0;
  80.     int idx;
  81.  
  82.     for (int i = 0; i < size; i++) {
  83.         if (values[i].getSizeKB() > max) {
  84.             max = values[i].getSizeKB();
  85.             idx = i;
  86.         }
  87.     }
  88.  
  89.     if (values[idx].getSizeKB() <= storage) {
  90.         return true;
  91.     }
  92.     else {
  93.         return false;
  94.     }
  95. }
  96.  
  97. int main() {
  98.     Image lena(1024, 1024, 3), anna;
  99.     double compressPercentage = 50.0;
  100.     std::cout << "Size: " << lena.getSizeKB() << ", compressed: " << lena.getSizeKB(compressPercentage) << std::endl;
  101.  
  102.     Image* values = new Image[3];
  103.     values[0] = Image(1024, 1024, 5);
  104.     values[1] = Image(264, 264, 2);
  105.     values[2] = Image(512, 512, 3);
  106.     // ...
  107.  
  108.     bool state = funkcija(values, 3, 13588);
  109.     if (state) {
  110.         std::cout << "Enough!" << endl;
  111.     }
  112.     else {
  113.         std::cout << "Error!" << endl;
  114.     }
  115.  
  116.     return 0;
  117. }
  118.  
  119. // 6. zadatak
  120. class PriceHistory {
  121.     friend bool operator< (const PriceHistory&, const PriceHistory&);
  122.     friend std::ostream& operator<< (std::ostream&, const PriceHistory&);
  123. private:
  124.     double *mPrices;
  125.     int mCount;
  126. public:
  127.     PriceHistory(int, double);
  128.     PriceHistory(const PriceHistory&);
  129.     double getAveragePrice() const;
  130.     int Trend() const;
  131.     string toString() const;
  132. };
  133.  
  134. PriceHistory::PriceHistory(int count, double price) : mCount(count) {
  135.     mPrices = new double[mCount];
  136.     for (int i = 0; i < mCount; i++) {
  137.         mPrices[i] = price;
  138.     }
  139. }
  140.  
  141. // 7. zadatak
  142. void run() {
  143.     PriceHistory ikeaSofa(10, 3420.0);
  144.     PriceHistory* primaSofa = new PriceHistory(ikeaSofa);
  145.     delete primaSofa;
  146. }
  147.  
  148. PriceHistory::PriceHistory(const PriceHistory& ref) : mCount(ref.mCount) {
  149.     mPrices = new double[mCount];
  150.     for (int i = 0; i < mCount; i++) {
  151.         mPrices[i] = ref.mPrices[i];
  152.     }
  153. }
  154.  
  155. // 8. zadatak
  156. int main() {
  157.     PriceHistory appleStock(6, 74545), googleStock(10, 41656);
  158.     if (appleStock < googleStock) {
  159.         std::cout << appleStock << " - Invest in apple.";
  160.     }
  161.     else {
  162.         std::cout << googleStock << " - Invest in google.";
  163.     }
  164.  
  165.     return 0;
  166. }
  167.  
  168. double PriceHistory::getAveragePrice() const{
  169.     double sum = 0;
  170.     for (int i = 0; i < mCount; i++) {
  171.         sum += mPrices[i];
  172.     }
  173.  
  174.     return sum / mCount;
  175. }
  176.  
  177. bool operator< (const PriceHistory& lhs, const PriceHistory& rhs)
  178. {
  179.     return lhs.getAveragePrice() < rhs.getAveragePrice();
  180. }
  181.  
  182. std::ostream& operator<< (std::ostream& OutputStream, const PriceHistory& rhs)
  183. {
  184.     for (int i = 0; i < rhs.mCount; i++) {
  185.         OutputStream << rhs.mPrices[i] << "\t";
  186.     }
  187.  
  188.     return OutputStream;
  189. }
  190.  
  191. // 9. zadatak
  192. int PriceHistory::Trend() const {
  193.     int up = 0;
  194.     int down = 0;
  195.  
  196.     for (int i = 0; i < mCount - 1; i++) {
  197.         if (mPrices[i] < mPrices[i + 1]) {
  198.             up++;
  199.         }
  200.         else if (mPrices[i] > mPrices[i + 1]) {
  201.             down++;
  202.         }
  203.     }
  204.  
  205.     if (up > down) {
  206.         return 1;
  207.     }
  208.     else if (down > up) {
  209.         return -1;
  210.     }
  211.     else {
  212.         return 0;
  213.     }
  214. }
  215.  
  216. // 10. zadatak
  217. void Save(string fileName, PriceHistory prices) {
  218.     ofstream output(fileName, ios::app);
  219.     if (output.is_open() == false) {
  220.         cout << "Error!" << endl;
  221.     }
  222.     else {
  223.         output << prices << "\n";
  224.         // output << prices.toString() << "\n";
  225.     }
  226.     output.close();
  227. }
  228.  
  229. int main() {
  230.     PriceHistory appleStock(6, 74545);
  231.     Save("dordan.txt", appleStock);
  232.  
  233.     return 0;
  234. }
  235.  
  236. // 11. zadatak
  237. class Sensor {
  238. private:
  239.     std::string mName;
  240.     bool mIsActive;
  241.     double mPreviousRead;
  242. public:
  243.     Sensor(std::string);
  244.     double getCurrentRead() const;
  245.     void TurnON_OFF();
  246. };
  247.  
  248. Sensor::Sensor(std::string name) : mName(name), mIsActive(false) {
  249.     mPreviousRead = getCurrentRead();
  250. }
  251.  
  252. double Sensor::getCurrentRead() const {
  253.     double random = DG + (float)rand() * (GG-DG) / (float)RAND_MAX;
  254.     return random;
  255. }
  256.  
  257. void Sensor::TurnON_OFF() { mIsActive = !mIsActive; }
  258.  
  259. int main() {
  260.     std::srand((unsigned int)std::time(0));
  261.  
  262.     return 0;
  263. }
  264.  
  265. // 12. zadatak
  266. // ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement