140 lines
4.0 KiB
C++
140 lines
4.0 KiB
C++
#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
|