Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #include <fstream>
- #define DG 0.0
- #define GG 10.0
- using namespace std;
- // 1. zadatak
- class Image {
- private:
- int mHeight;
- int mWidth;
- int mBytesPerPixel;
- public:
- Image();
- Image(int, int, int);
- void setHeight(int);
- void setWidth(int);
- void setBPP(int);
- int getHeight() const;
- int getWidth() const;
- int getBPP() const;
- void scale(double);
- double getSizeKB();
- double getSizeKB(double);
- };
- void Image::setHeight(int height) {
- mHeight = height;
- }
- void Image::setWidth(int width) {
- mWidth = width;
- }
- void Image::setBPP(int bytesperpixel) {
- mBytesPerPixel = bytesperpixel;
- }
- int Image::getHeight() const { return mHeight; }
- int Image::getWidth() const { return mWidth; }
- int Image::getBPP() const { return mBytesPerPixel; }
- // 2. zadatak
- Image::Image() : mHeight(0), mWidth(0), mBytesPerPixel(0) {}
- Image::Image(int height, int width, int bytesperpixel) : mHeight(height), mWidth(width), mBytesPerPixel(bytesperpixel) {}
- // 3. zadatak
- void Image::scale(double sc) {
- if (sc < 0.1) {
- return;
- }
- mHeight *= sc;
- mWidth *= sc;
- }
- // 4. zadatak
- double Image::getSizeKB() {
- return ((mHeight * mWidth) * mBytesPerPixel) / 1024;
- }
- double Image::getSizeKB(double p) {
- if (!(p >= 10.0 && p <= 90.0)) {
- p = 0;
- }
- return getSizeKB() - (getSizeKB() * (p / 100));
- }
- // 5. zadatak
- bool funkcija(Image* values, int size, double storage) {
- double max = 0;
- int idx;
- for (int i = 0; i < size; i++) {
- if (values[i].getSizeKB() > max) {
- max = values[i].getSizeKB();
- idx = i;
- }
- }
- if (values[idx].getSizeKB() <= storage) {
- return true;
- }
- else {
- return false;
- }
- }
- int main() {
- Image lena(1024, 1024, 3), anna;
- double compressPercentage = 50.0;
- std::cout << "Size: " << lena.getSizeKB() << ", compressed: " << lena.getSizeKB(compressPercentage) << std::endl;
- Image* values = new Image[3];
- values[0] = Image(1024, 1024, 5);
- values[1] = Image(264, 264, 2);
- values[2] = Image(512, 512, 3);
- // ...
- bool state = funkcija(values, 3, 13588);
- if (state) {
- std::cout << "Enough!" << endl;
- }
- else {
- std::cout << "Error!" << endl;
- }
- return 0;
- }
- // 6. zadatak
- class PriceHistory {
- friend bool operator< (const PriceHistory&, const PriceHistory&);
- friend std::ostream& operator<< (std::ostream&, const PriceHistory&);
- private:
- double *mPrices;
- int mCount;
- public:
- PriceHistory(int, double);
- PriceHistory(const PriceHistory&);
- double getAveragePrice() const;
- int Trend() const;
- string toString() const;
- };
- PriceHistory::PriceHistory(int count, double price) : mCount(count) {
- mPrices = new double[mCount];
- for (int i = 0; i < mCount; i++) {
- mPrices[i] = price;
- }
- }
- // 7. zadatak
- void run() {
- PriceHistory ikeaSofa(10, 3420.0);
- PriceHistory* primaSofa = new PriceHistory(ikeaSofa);
- delete primaSofa;
- }
- PriceHistory::PriceHistory(const PriceHistory& ref) : mCount(ref.mCount) {
- mPrices = new double[mCount];
- for (int i = 0; i < mCount; i++) {
- mPrices[i] = ref.mPrices[i];
- }
- }
- // 8. zadatak
- int main() {
- PriceHistory appleStock(6, 74545), googleStock(10, 41656);
- if (appleStock < googleStock) {
- std::cout << appleStock << " - Invest in apple.";
- }
- else {
- std::cout << googleStock << " - Invest in google.";
- }
- return 0;
- }
- double PriceHistory::getAveragePrice() const{
- double sum = 0;
- for (int i = 0; i < mCount; i++) {
- sum += mPrices[i];
- }
- return sum / mCount;
- }
- bool operator< (const PriceHistory& lhs, const PriceHistory& rhs)
- {
- return lhs.getAveragePrice() < rhs.getAveragePrice();
- }
- std::ostream& operator<< (std::ostream& OutputStream, const PriceHistory& rhs)
- {
- for (int i = 0; i < rhs.mCount; i++) {
- OutputStream << rhs.mPrices[i] << "\t";
- }
- return OutputStream;
- }
- // 9. zadatak
- int PriceHistory::Trend() const {
- int up = 0;
- int down = 0;
- for (int i = 0; i < mCount - 1; i++) {
- if (mPrices[i] < mPrices[i + 1]) {
- up++;
- }
- else if (mPrices[i] > mPrices[i + 1]) {
- down++;
- }
- }
- if (up > down) {
- return 1;
- }
- else if (down > up) {
- return -1;
- }
- else {
- return 0;
- }
- }
- // 10. zadatak
- void Save(string fileName, PriceHistory prices) {
- ofstream output(fileName, ios::app);
- if (output.is_open() == false) {
- cout << "Error!" << endl;
- }
- else {
- output << prices << "\n";
- // output << prices.toString() << "\n";
- }
- output.close();
- }
- int main() {
- PriceHistory appleStock(6, 74545);
- Save("dordan.txt", appleStock);
- return 0;
- }
- // 11. zadatak
- class Sensor {
- private:
- std::string mName;
- bool mIsActive;
- double mPreviousRead;
- public:
- Sensor(std::string);
- double getCurrentRead() const;
- void TurnON_OFF();
- };
- Sensor::Sensor(std::string name) : mName(name), mIsActive(false) {
- mPreviousRead = getCurrentRead();
- }
- double Sensor::getCurrentRead() const {
- double random = DG + (float)rand() * (GG-DG) / (float)RAND_MAX;
- return random;
- }
- void Sensor::TurnON_OFF() { mIsActive = !mIsActive; }
- int main() {
- std::srand((unsigned int)std::time(0));
- return 0;
- }
- // 12. zadatak
- // ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement