Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

#include <iostream>

class Time {
private:
int hour;
int minute;
int second;

public:
// Default constructor that sets the time to midnight (0:0:0)
Time() : hour(0), minute(0), second(0) {}

// Constructor with three integer parameters (validates and sets time)


Time(int h, int m, int s) {
adjustTime(h, m, s);
}

// Static method to validate and adjust time


static Time adjustTime(int h, int m, int s) {
if (h < 0 || h > 23) {
h = h % 24;
}
if (m < 0 || m > 59) {
h += m / 60;
m = m % 60;
}
if (s < 0 || s > 59) {
m += s / 60;
s = s % 60;
}
return Time(h, m, s);
}

// Getter for the hour


int getHour() const {
return hour;
}

// Getter for the minute


int getMinute() const {
return minute;
}

// Getter for the second


int getSecond() const {
return second;
}
// Method to print the time in hour:min:sec format
void printTime() const {
std::cout << std::setw(2) << std::setfill('0') << hour << ":"
<< std::setw(2) << std::setfill('0') << minute << ":"
<< std::setw(2) << std::setfill('0') << second;
}

// Friend function to compare two times


friend bool compareTimes(const Time& time1, const Time& time2) {
if (time1.hour < time2.hour) return true;
if (time1.hour > time2.hour) return false;
if (time1.minute < time2.minute) return true;
if (time1.minute > time2.minute) return false;
return time1.second < time2.second;
}
};

int main() {
int n;
std::cin >> n;
if (n <= 0) {
std::cout << "Invalid input. Please enter a positive integer." << std::endl;
return 1;
}

Time* times = new Time[n];

for (int i = 0; i < n; ++i) {


int h, m, s;
std::cin >> h >> m >> s;
times[i] = Time::adjustTime(h, m, s);
}

// Find the earliest time


Time earliest = times[0];
for (int i = 1; i < n; ++i) {
if (compareTimes(times[i], earliest)) {
earliest = times[i];
}
}

std::cout << "The earliest time is: ";


earliest.printTime();
std::cout << std::endl;

delete[] times; // Don't forget to free the dynamic array


return 0;
}

#include <iostream>
#include <string>

class Predmet {
private:
std::string naziv;
int ocena;

public:
// Konstruktor za inicijalizaciju naziva i ocene predmeta
Predmet(const std::string& _naziv, int _ocena) : naziv(_naziv), ocena(_ocena) {}

// Getter za naziv predmeta


std::string getNaziv() const {
return naziv;
}

// Setter za ocenu predmeta


void setOcena(int _ocena) {
ocena = _ocena;
}

// Metoda za ispis predmeta na standardni izlaz


void ispisPredmeta() const {
std::cout << "Predmet: " << naziv << ", Ocena: " << ocena << std::endl;
}
};

class Ucenik {
private:
std::string ime;
std::string prezime;
int brojPredmeta;
Predmet knjizica[50];

public:
// Konstruktor sa dva parametra za ime i prezime, broj predmeta se postavlja na 0
Ucenik(const std::string& _ime, const std::string& _prezime) : ime(_ime),
prezime(_prezime), brojPredmeta(0) {}
// Metoda za dodavanje predmeta u knjizicu učenika (ako ima mesta)
void dodajPredmet(const std::string& naziv, int ocena) {
if (brojPredmeta < 50) {
knjizica[brojPredmeta] = Predmet(naziv, ocena);
brojPredmeta++;
}
}

// Metoda za izračunavanje prosečne ocene učenika


double prosecnaOcena() const {
if (brojPredmeta == 0) {
return 0.0; // Ako nema ocena, vraćamo 0
}

int sumaOcena = 0;
for (int i = 0; i < brojPredmeta; i++) {
sumaOcena += knjizica[i].getOcena();
}

return static_cast<double>(sumaOcena) / brojPredmeta;


}

// Metoda za ispis učenika na standardni izlaz


void ispisUcenika() const {
std::cout << "Ime: " << ime << ", Prezime: " << prezime << std::endl;
std::cout << "Prosecna ocena: " << prosecnaOcena() << std::endl;
std::cout << "Knjizica predmeta:" << std::endl;
for (int i = 0; i < brojPredmeta; i++) {
knjizica[i].ispisPredmeta();
}
}
};

int main() {
Ucenik ucenik("Marko", "Marković");
ucenik.dodajPredmet("Matematika", 5);
ucenik.dodajPredmet("Fizika", 4);
ucenik.dodajPredmet("Informatika", 5);

ucenik.ispisUcenika();

return 0;
}

You might also like