consolidate all repos to one for archive

This commit is contained in:
2025-01-28 13:46:42 +01:00
commit a6610fbc7a
5350 changed files with 2705721 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
Napišite razred Time, ki naj ima:
privatne instančne spremenljivke:
hour,
minute in
second.
razredne spremenljivke:
maxHour, ki je konstanta in predstavlja maksimalno vrednost, in sicer 24
noonHour, ki je konstanta in predstvalja opoldne, in sicer ima vrednost 12
javni konstruktor s 3 parametri (pri tem pazite, da nastavite vrednosti le, če je čas veljaven, sicer nastavite vse vrednosti na 0),
javno metodo toString, ki vrne čas kot string, in sicer v formatu hh:mm:ss (npr. 14:05:30),
javno metodo toString12HourFormat, ki vrne čas kot string, vendar je čas prilagojen za 12-urni časovni sistem. Torej je potrebno ustrezno spremeniti čas in na koncu dodati še "AM"/"PM". Format pri tem zapisu je hh:mm:ss XM (npr. 18:03:01 --> 06:03:01 PM),
javno razredno metodo isTimeValid(unsigned int hour, unsigned int minute, unsigned int second), ki preveri, ali je možen čas glede na podane podatke. Potrebno je upoštevati maksimalne vrednosti, in sicer 23:59:59. Primer: isTimeValid(14, 3, 43) vrne true, isTimeValid(16, 67, 91) vrne false.
javno razredno metodo parse(std::string time), ki prejme čas v obiki niza (v formatu hh:mm:ss), razbere posamezne vrednosti in vrne čas tipa Time.
V glavnem programu prikažite delovanje vseh metod. Ustvarite vsaj 5 različnih objektov razreda Time in na ta način preverite, ali metode pokrivajo vse možne scenarije.
Ostali napotki pri reševanju naloge:
Če potrebujete kako metodo get ali set (za posamezno instančno spremenljivko), si jo zapišite.
V nalogi uporabite kazalec this na vseh mestih, kjer je smiselno.
V nalogi uporabite določilo const pri vseh metodah, kjer je smiselno.
substr: https://cplusplus.com/reference/string/string/substr/

View File

@@ -0,0 +1,62 @@
#include "Time.h"
#include <string>
Time::Time() : hour(0), minute(0), second(0) {}
Time::Time(unsigned int hour) : Time(hour, 0, 0) {}
Time::Time(unsigned int hour, unsigned int minute, unsigned int second) : hour(hour), minute(minute), second(second) {
if (hour >= MAX_HOUR || minute >= 60 || second >= 60) {
this->hour = 0;
this->minute = 0;
this->second = 0;
}
}
std::string Time::toString() const {
return std::to_string(hour) + ":" + std::to_string(minute) + ":" + std::to_string(second);
}
std::string Time::toString12HourFormat() const {
std::string ret;
if (hour > NOON_HOUR) ret = std::to_string(hour - 12); //" PM";
if (hour == NOON_HOUR) ret = std::to_string(hour); //" PM";
if (hour < NOON_HOUR) ret = std::to_string(hour); // " AM";
if (hour == 0) ret = std::to_string(12); //AM
ret = ret + ":" + std::to_string(minute) + ":" + std::to_string(second);
return ret;
}
bool Time::isTimeValid(unsigned int hour, unsigned int minute, unsigned int second) {
return !(hour >= MAX_HOUR || minute >= 60 || second >= 60);
}
Time Time::parse(const std::string &time) {
unsigned int h = std::stoi(time.substr(0, 2));
unsigned int m = std::stoi(time.substr(3, 2));
unsigned int s = std::stoi(time.substr(6, 2));
return {h, m, s};
}
const Time* Time::maxTime(const Time *time1, const Time *time2) {
unsigned int timeSec1 = time1->second + time1->minute * 60 + time1->hour * 3600;
unsigned int timeSec2 = time2->second + time2->minute * 60 + time2->hour * 3600;
return (timeSec1 > timeSec2) ? time1 : time2;
}
unsigned int Time::getHour() const {
return hour;
}
unsigned int Time::getMinute() const {
return minute;
}
unsigned int Time::getSecond() const {
return second;
}

View File

@@ -0,0 +1,39 @@
#ifndef NALOGA0301_TIME_H
#define NALOGA0301_TIME_H
#include <string>
/*#define MAX_HOUR 24
#define NOON_HOUR 12*/
class Time {
private:
unsigned int hour, minute, second;
public:
const static unsigned int MAX_HOUR = 24;
const static unsigned int NOON_HOUR = 12;
Time();
explicit Time(unsigned int hour);//time(hour,0,0);
Time(unsigned int hour, unsigned int minute, unsigned int second);
std::string toString() const;
std::string toString12HourFormat() const;
static bool isTimeValid(unsigned int hour, unsigned int minute, unsigned int second);
static Time parse(const std::string &time);
static const Time *maxTime(const Time *time1, const Time *time2);
unsigned int getHour() const;
unsigned int getMinute() const;
unsigned int getSecond() const;
};
#endif //NALOGA0301_TIME_H

View File

@@ -0,0 +1,38 @@
#include <iostream>
#include "Time.h"
#include <string>
int main() {
std::cout << "Hello, World!" << std::endl;
Time a;
Time b(1, 45, 55);
Time c(20);
Time e(1, 65, 55);
Time f(25);
std::cout << a.toString() << "\n";
std::cout << b.toString() << "\n";
std::cout << c.toString() << "\n";
std::cout << e.toString() << "\n";
std::cout << f.toString() << "\n\n";
std::cout << a.toString12HourFormat() << "\n";
std::cout << b.toString12HourFormat() << "\n";
std::cout << c.toString12HourFormat() << "\n";
std::cout << e.toString12HourFormat() << "\n";
std::cout << f.toString12HourFormat() << "\n\n";
std::string tim1 = "01:42:48";
a = Time::parse(tim1);
tim1 = "01:62:48";
e = Time::parse(tim1);
std::cout << a.toString() << "\n";
std::cout << e.toString() << "\n\n";
std::cout << Time::isTimeValid(50, 22, 65)<< "\n";
const Time *g = new Time(20,56,5);
const Time *h = new Time(18,4,6);
std::cout << Time::maxTime(g,h)->toString();
return 0;
}