consolidate all repos to one for archive
This commit is contained in:
12
semester_2/programiranje_2/naloga0202/README.md
Normal file
12
semester_2/programiranje_2/naloga0202/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
Vzemite vašo rešitev naloge 1.2.
|
||||
Preučite in uporabite zgled ter predelajte nalogo 1.2 tako, da uporabite (https://en.cppreference.com/w/cpp/numeric/random/random_device) naključnih števil za generiranje objektov popite vode (mesec naj bo 3, leto 2022, dnevi pa od 1 do 30). Namesto polja uporabite (https://en.cppreference.com/w/cpp/container/vector).
|
||||
Napišite razred WaterIntake, ki bo imel naslednje instančne privatne lastnosti:
|
||||
day: unsigned int,
|
||||
month: unsigned int,
|
||||
year: unsigned int,
|
||||
quantity: float.
|
||||
Razred naj vsebuje konstruktor s 4 argumenti.
|
||||
Napišite javne metode: get (vrne podatek) za vsako lastnost posebej in set (nastavi podatek) samo za lastnost quantity.
|
||||
Razredu dodajte metodo toString, ki vrne vse podatke objekta.
|
||||
Razredu dodajte metodo addQuantity, ki prejme realno vrednost, za katero poveča quantity.
|
||||
Preuredite glavni program iz naloge 1.2 tako, da bo vector vseboval kazalce na objekte razreda WaterIntake (zgled.cpp).
|
37
semester_2/programiranje_2/naloga0202/WaterIntake.cpp
Normal file
37
semester_2/programiranje_2/naloga0202/WaterIntake.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "WaterIntake.h"
|
||||
#include <iostream>
|
||||
|
||||
WaterIntake::WaterIntake() : day(0), month(0), year(0), quantity(0.0) {}
|
||||
|
||||
WaterIntake::WaterIntake(unsigned int day, unsigned int month, unsigned int year, float quantity) : day(day), month(month), year(year), quantity(quantity) {
|
||||
}
|
||||
|
||||
WaterIntake::~WaterIntake() {}
|
||||
|
||||
unsigned int WaterIntake::getDay() {
|
||||
return day;
|
||||
}
|
||||
|
||||
unsigned int WaterIntake::getMonth() {
|
||||
return month;
|
||||
}
|
||||
|
||||
unsigned int WaterIntake::getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
float WaterIntake::getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
std::string WaterIntake::toString() {
|
||||
return "day: " + std::to_string(day) + "\n month: " + std::to_string(month) + "\n year: " + std::to_string(year) + "\n quantity: " + std::to_string(quantity) + "\n";
|
||||
}
|
||||
|
||||
void WaterIntake::addQuantity(float addQuantity) {
|
||||
quantity = quantity + addQuantity;
|
||||
}
|
||||
|
||||
bool WaterIntake::isNormal() {
|
||||
return 2.0 <= quantity && quantity <= 2.5;
|
||||
}
|
33
semester_2/programiranje_2/naloga0202/WaterIntake.h
Normal file
33
semester_2/programiranje_2/naloga0202/WaterIntake.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef NALOGA0102_WATERINTAKE_H
|
||||
#define NALOGA0102_WATERINTAKE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class WaterIntake {
|
||||
private:
|
||||
unsigned int day, month, year;
|
||||
float quantity;
|
||||
public:
|
||||
WaterIntake();
|
||||
|
||||
WaterIntake(unsigned int, unsigned int, unsigned int, float);
|
||||
|
||||
~WaterIntake();
|
||||
|
||||
unsigned int getDay();
|
||||
|
||||
unsigned int getMonth();
|
||||
|
||||
unsigned int getYear();
|
||||
|
||||
float getQuantity();
|
||||
|
||||
std::string toString();
|
||||
|
||||
void addQuantity(float);
|
||||
|
||||
bool isNormal();
|
||||
|
||||
};
|
||||
|
||||
#endif //NALOGA0102_WATERINTAKE_H
|
140
semester_2/programiranje_2/naloga0202/naloga0202.cpp
Normal file
140
semester_2/programiranje_2/naloga0202/naloga0202.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include "WaterIntake.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void menu() {
|
||||
cout << "============================" << endl;
|
||||
cout << "=========== MENU ===========" << endl;
|
||||
cout << "============================" << endl;
|
||||
cout << "1 ... GENERATE WATER INTAKES" << endl;
|
||||
cout << "2 ... PRINT WATER INTAKES" << endl;
|
||||
cout << "3 ... AVERAGE" << endl;
|
||||
cout << "4 ... DAYS UNDER AVERAGE" << endl;
|
||||
cout << "5 ... THE MOST WATER" << endl;
|
||||
cout << "6 ... IN LIMIT" << endl;
|
||||
cout << "7 ... ALL THE WATER" << endl;
|
||||
cout << "0 ... EXIT" << endl;
|
||||
cout << "============================" << endl;
|
||||
cout << "Select: ";
|
||||
}
|
||||
|
||||
void fillArray(vector<WaterIntake*> &waterIntakes, const unsigned int size) {
|
||||
float randNumber;
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
randNumber = (rand() / (RAND_MAX + 0.0)) * 3 + 0.5;
|
||||
randNumber = round(10 * randNumber) / 10;
|
||||
waterIntakes.push_back(new WaterIntake(i,2,2022,randNumber));
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(const vector<WaterIntake*> &waterIntakes) {
|
||||
for (unsigned int i = 0; i < waterIntakes.size(); i++)
|
||||
cout << waterIntakes[i]->toString() << std::endl;
|
||||
}
|
||||
|
||||
float sum(const vector<WaterIntake*> &waterIntakes) {
|
||||
float liters = 0;
|
||||
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
|
||||
liters += waterIntakes[i]->getQuantity();
|
||||
}
|
||||
return liters;
|
||||
}
|
||||
|
||||
float average(const vector<WaterIntake*> &waterIntakes) {
|
||||
float liters = sum(waterIntakes);
|
||||
liters = liters / waterIntakes.size();
|
||||
liters = round(10 * liters) / 10;
|
||||
return liters;
|
||||
}
|
||||
|
||||
void under_average(const vector<WaterIntake*> &waterIntakes) {
|
||||
float avg = average(waterIntakes);
|
||||
unsigned int dni = 0;
|
||||
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
|
||||
if (avg > waterIntakes[i]->getQuantity()) {
|
||||
cout << waterIntakes[i]->toString();
|
||||
dni++;
|
||||
}
|
||||
}
|
||||
cout << dni << " days are under average \n";
|
||||
}
|
||||
|
||||
void max_day(const vector<WaterIntake*> &waterIntakes) {
|
||||
float max_drank = waterIntakes[0]->getQuantity();
|
||||
unsigned int day;
|
||||
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
|
||||
if (max_drank < waterIntakes[i]->getQuantity()) {;
|
||||
day = i;
|
||||
}
|
||||
}
|
||||
cout << "On day \n" << waterIntakes[day]->toString();
|
||||
}
|
||||
|
||||
void in_limit(const vector<WaterIntake*> &waterIntakes) {
|
||||
unsigned int day = 0;
|
||||
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
|
||||
if (waterIntakes[i]->isNormal()) {
|
||||
cout << waterIntakes[i]->toString();
|
||||
day++;
|
||||
}
|
||||
}
|
||||
cout << day << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const unsigned int days = 30;
|
||||
vector<WaterIntake*> waterIntakes;
|
||||
float tmp;
|
||||
srand(time(nullptr));
|
||||
|
||||
fillArray(waterIntakes, days);
|
||||
|
||||
bool running = true;
|
||||
int selection;
|
||||
|
||||
do {
|
||||
menu();
|
||||
cin >> selection;
|
||||
cout << endl;
|
||||
switch (selection) {
|
||||
case 1:
|
||||
fillArray(waterIntakes, days);
|
||||
break;
|
||||
case 2:
|
||||
printArray(waterIntakes);
|
||||
break;
|
||||
case 3:
|
||||
tmp = average(waterIntakes);
|
||||
cout << "On average you drank " << tmp << "L a day";
|
||||
break;
|
||||
case 4:
|
||||
under_average(waterIntakes);
|
||||
break;
|
||||
case 5:
|
||||
max_day(waterIntakes);
|
||||
break;
|
||||
case 6:
|
||||
in_limit(waterIntakes);
|
||||
break;
|
||||
case 7:
|
||||
tmp = sum(waterIntakes);
|
||||
cout << "Vsa voda popita" << tmp;
|
||||
break;
|
||||
case 0:
|
||||
running = false;
|
||||
break;
|
||||
default:
|
||||
cout << "Wrong selection!" << endl;
|
||||
break;
|
||||
}
|
||||
cout << endl;
|
||||
} while (running);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//isNormal bool med
|
59
semester_2/programiranje_2/naloga0202/zgled.cpp
Normal file
59
semester_2/programiranje_2/naloga0202/zgled.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void menu() {
|
||||
cout << "============================" << endl;
|
||||
cout << "=========== MENU ===========" << endl;
|
||||
cout << "============================" << endl;
|
||||
cout << "1 ... GENERATE WATER INTAKES" << endl;
|
||||
cout << "2 ... PRINT WATER INTAKES" << endl;
|
||||
cout << "0 ... EXIT" << endl;
|
||||
cout << "============================" << endl;
|
||||
cout << "Select: ";
|
||||
}
|
||||
|
||||
void fillVector(vector<WaterIntake*> &waterIntakes, const unsigned int size) {
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
waterIntakes.push_back(new WaterIntake(3, 3, 2022, (float)(rand() % 31 + 5)/10.f));
|
||||
}
|
||||
}
|
||||
|
||||
void printVector(const vector<WaterIntake*> &waterIntakes) {
|
||||
for (unsigned int i = 0; i < waterIntakes.size(); i++)
|
||||
cout << waterIntakes[i]->toString() << ((i < waterIntakes.size() - 1) ? ", " : ".") << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const unsigned int days = 30;
|
||||
vector<WaterIntake*> waterIntakes;
|
||||
|
||||
srand(time(nullptr));
|
||||
|
||||
bool running = true;
|
||||
int selection;
|
||||
|
||||
do {
|
||||
menu();
|
||||
cin >> selection;
|
||||
switch (selection) {
|
||||
case 1:
|
||||
fillVector(waterIntakes, days);
|
||||
break;
|
||||
case 2:
|
||||
printVector(waterIntakes);
|
||||
break;
|
||||
case 0:
|
||||
running = false;
|
||||
break;
|
||||
default:
|
||||
cout << "Wrong selection!" << endl;
|
||||
break;
|
||||
}
|
||||
cout << endl;
|
||||
} while (running);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user