consolidate all repos to one for archive
This commit is contained in:
24
semester_2/programiranje_2/naloga1102/Address.cpp
Normal file
24
semester_2/programiranje_2/naloga1102/Address.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#include "Address.h"
|
||||
|
||||
Address::Address(std::string street, std::string post, std::string country) :
|
||||
street(street), post(post), country(country) {}
|
||||
|
||||
std::string Address::toString() const {
|
||||
return street + "," + post + "," + country;
|
||||
}
|
||||
|
||||
void Address::setStreet(const std::string &street) {
|
||||
Address::street = street;
|
||||
}
|
||||
|
||||
void Address::setPost(const std::string &post) {
|
||||
Address::post = post;
|
||||
}
|
||||
|
||||
void Address::setCountry(const std::string &country) {
|
||||
Address::country = country;
|
||||
}
|
28
semester_2/programiranje_2/naloga1102/Address.h
Normal file
28
semester_2/programiranje_2/naloga1102/Address.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#ifndef NALOGA1101_ADDRESS_H
|
||||
#define NALOGA1101_ADDRESS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Address {
|
||||
private:
|
||||
std::string street, post, country;
|
||||
|
||||
public:
|
||||
Address() = default;
|
||||
Address(std::string street, std::string post, std::string country);
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
void setStreet(const std::string &street);
|
||||
|
||||
void setPost(const std::string &post);
|
||||
|
||||
void setCountry(const std::string &country);
|
||||
};
|
||||
|
||||
|
||||
#endif //NALOGA1101_ADDRESS_H
|
25
semester_2/programiranje_2/naloga1102/Date.cpp
Normal file
25
semester_2/programiranje_2/naloga1102/Date.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "Date.h"
|
||||
|
||||
Date::Date(unsigned int day, unsigned int month, unsigned int year) : day(day), month(month), year(year) {}
|
||||
|
||||
std::string Date::toString() const {
|
||||
return std::to_string(day) + "." + std::to_string(month) + "." + std::to_string(year);
|
||||
}
|
||||
|
||||
Date Date::getDateFromString(std::string date) {
|
||||
|
||||
for (int i = 0; i < date.size(); ++i) {
|
||||
if ((date[i] < '0' or date[i] > '9') and date[i] != '.') throw UnparseableDateException(date);
|
||||
}
|
||||
|
||||
std::string tmp;
|
||||
unsigned int dd[3] = {0,0,0};
|
||||
std::stringstream stram(date);
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
getline (stram,tmp, '.');
|
||||
dd[i] = std::stoi(tmp);
|
||||
}
|
||||
|
||||
return {dd[0], dd[1], dd[2]};
|
||||
}
|
22
semester_2/programiranje_2/naloga1102/Date.h
Normal file
22
semester_2/programiranje_2/naloga1102/Date.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef NALOGA0201_DATE_H
|
||||
#define NALOGA0201_DATE_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include "UnparseableDateException.h"
|
||||
|
||||
class Date {
|
||||
private:
|
||||
unsigned int day, month, year;
|
||||
public:
|
||||
Date() = default;
|
||||
|
||||
Date(unsigned int day, unsigned int month, unsigned int year);
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
static Date getDateFromString(std::string date);
|
||||
};
|
||||
|
||||
#endif //NALOGA0201_DATE_H
|
38
semester_2/programiranje_2/naloga1102/Log.cpp
Normal file
38
semester_2/programiranje_2/naloga1102/Log.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#include "Log.h"
|
||||
#include <ctime>
|
||||
|
||||
Log::Log(LogType type) {
|
||||
file.open(fileName,std::ios_base::out | std::ios_base::app);
|
||||
time_t now = time(0);
|
||||
tm* strucTime = localtime(&now);
|
||||
|
||||
std::string time;
|
||||
time = std::to_string(strucTime->tm_year + 1900) + "-" +
|
||||
std::to_string(strucTime->tm_mon + 1) + "-" +
|
||||
std::to_string(strucTime->tm_mday) + " " +
|
||||
std::to_string(strucTime->tm_hour) + ":" +
|
||||
std::to_string(strucTime->tm_min) + ":" +
|
||||
std::to_string(strucTime->tm_sec) + " ";
|
||||
operator<<(time + GetStringLogType(type));
|
||||
}
|
||||
|
||||
Log::~Log() {
|
||||
file.close();
|
||||
}
|
||||
|
||||
std::string Log::GetStringLogType(LogType type) {
|
||||
switch (type) {
|
||||
case LogType::ERROR:
|
||||
return "[ERROR] ";
|
||||
case LogType::INFO:
|
||||
return "[INFO] ";
|
||||
case LogType::DEBUG:
|
||||
return "[DEBUG] ";
|
||||
}
|
||||
return "ERROR";
|
||||
}
|
||||
|
35
semester_2/programiranje_2/naloga1102/Log.h
Normal file
35
semester_2/programiranje_2/naloga1102/Log.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#ifndef NALOGA1102_LOG_H
|
||||
#define NALOGA1102_LOG_H
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
enum LogType{
|
||||
DEBUG, INFO, ERROR, WARNING
|
||||
};
|
||||
|
||||
class Log {
|
||||
private:
|
||||
std::ofstream file;
|
||||
std::string fileName = "log.txt";
|
||||
public:
|
||||
|
||||
Log(LogType type);
|
||||
|
||||
~Log();
|
||||
|
||||
std::string GetStringLogType(LogType type);
|
||||
|
||||
template<typename T>
|
||||
Log& operator<<(const T &msg){
|
||||
file << msg;
|
||||
return *this;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
#endif //NALOGA1102_LOG_H
|
29
semester_2/programiranje_2/naloga1102/README.md
Normal file
29
semester_2/programiranje_2/naloga1102/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
Vzemite nalogo 11.1.
|
||||
|
||||
Kadar je koda v produkciji in želimo kasneje pregledati ali je med izvajanjem prišlo do napake, se poslužimo shranjevanja informacij o napakah v datoteko (angl. log file(https://en.wikipedia.org/wiki/Logging_(software))) . Pri tej nalogi bomo v ta namen naredili razred Log.
|
||||
|
||||
Primer:
|
||||
|
||||
int main() {
|
||||
Log(LogType::INFO) << "This is additional message. We can also put multiple << and other types, not just strings e.g. " << 5 << "\n" ;
|
||||
std::vector<std::shared_ptr<Student>> students = Student::LoadFromFile("students.csv");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Znotraj datoteke log.txt:
|
||||
|
||||
[INFO] This is additional message. We can also put multiple << and other types, not just strings e.g. 5
|
||||
[ERROR] Unparseable date: "6.12.199a"!
|
||||
|
||||
Ustvarite enum class LogType z vrednostmi DEBUG, INFO, WARN in ERROR.
|
||||
Napišite razred Log, ki ima:
|
||||
razredno spremenljivko file (ofstream)
|
||||
konstantno razredno spremenljivko fileName (string), ki jo kar nastavite na log.txt
|
||||
razredno metodo GetStringLogType(LogType type), ki vrača string za podani LogType.
|
||||
konstruktor s parametrom type, ki je tipa LogType. Znotraj konstruktorja odprite datoteko in vanjo zapišite za kateri klic se je šlo, torej npr. Log(LogType::Error) bo v datoteko zapisalo [ERROR]. Pri tem uporabite razredno metodo GetStringLogType.
|
||||
destruktor, ki zapre datoteko
|
||||
prekrite operator <<, vendar naj bo Log &operator<<(const T &msg). Bodite pozorni na to, da lahko prejmemo sporočilo poljubnega tipa (torej gre za šablono). Znotraj te metode zapišemo v datoteko prejeto sporočilo. Na koncu vrnemo kazalec *this.
|
||||
Pri metodi LoadFromFile znotraj try-catch bloka, dodajte zapisovanje napake v datoteko v primeru napačnega formata datuma.
|
||||
Ustvarite še eno izjemo po lastni izbiri in jo uporabite na ustreznih mestih v vaši kodi. Primer naj bo smiseln.
|
||||
Poiščite še 3 smiselne primere za LogType, da bo v datoteko zapisalo [WARN].
|
||||
Demonstrirajte delovanje razreda Log in lastne izjeme.
|
66
semester_2/programiranje_2/naloga1102/Student.cpp
Normal file
66
semester_2/programiranje_2/naloga1102/Student.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#include "Student.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Student::Student(unsigned int id, std::string name, std::string surname, Date date, Address address) :
|
||||
id(id), name(name), surname(surname), dateOfBirth(date), address(address) {}
|
||||
|
||||
std::string Student::toString() const {
|
||||
return std::to_string(id) + "," + name + "," + surname + "," + dateOfBirth.toString() + "," + address.toString() + "\n";
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Student>> Student::LoadFromFile(const std::string &fileName) {
|
||||
|
||||
std::vector<std::shared_ptr<Student>> ret;
|
||||
std::ifstream file (fileName);
|
||||
std::string line;
|
||||
|
||||
unsigned int id;
|
||||
std::string name, surname;
|
||||
Date dateOfBirth;
|
||||
Address address;
|
||||
|
||||
while (std::getline(file,line)){
|
||||
std::stringstream word(line);
|
||||
std::string tmp;
|
||||
|
||||
std::getline(word,tmp,',');
|
||||
id = std::stoi(tmp);
|
||||
std::getline(word,tmp,',');
|
||||
name = tmp;
|
||||
std::getline(word,tmp,',');
|
||||
surname = tmp;
|
||||
|
||||
std::getline(word,tmp,',');
|
||||
try {
|
||||
dateOfBirth = Date::getDateFromString(tmp);
|
||||
}catch (UnparseableDateException e){
|
||||
Log(LogType::INFO) << e.toString() << "\n";
|
||||
dateOfBirth = {0,0,0};
|
||||
}
|
||||
|
||||
std::getline(word,tmp,',');
|
||||
address.setStreet(tmp);
|
||||
std::getline(word,tmp,',');
|
||||
address.setPost(tmp);
|
||||
std::getline(word,tmp,',');
|
||||
address.setCountry(tmp);
|
||||
std::shared_ptr<Student> tmppoint = std::make_shared<Student>(id,name,surname,dateOfBirth,address);
|
||||
ret.push_back(tmppoint);
|
||||
}
|
||||
file.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Student::SaveToFile(const std::vector<std::shared_ptr<Student>> &students, const std::string &fileName) {
|
||||
std::ofstream file (fileName);
|
||||
file.clear();
|
||||
for (int i = 0; i < students.size(); ++i) {
|
||||
file << students[i]->toString();
|
||||
}
|
||||
file.close();
|
||||
}
|
33
semester_2/programiranje_2/naloga1102/Student.h
Normal file
33
semester_2/programiranje_2/naloga1102/Student.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#ifndef NALOGA1101_STUDENT_H
|
||||
#define NALOGA1101_STUDENT_H
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "Address.h"
|
||||
#include "Date.h"
|
||||
#include "Log.h"
|
||||
|
||||
|
||||
class Student {
|
||||
private:
|
||||
unsigned int id;
|
||||
std::string name, surname;
|
||||
Date dateOfBirth;
|
||||
Address address;
|
||||
public:
|
||||
Student(unsigned int, std::string, std::string, Date, Address);
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
static std::vector<std::shared_ptr<Student>> LoadFromFile(const std::string &filename);
|
||||
|
||||
static void SaveToFile(const std::vector<std::shared_ptr<Student>> &students, const std::string &fileName);
|
||||
};
|
||||
|
||||
|
||||
#endif //NALOGA1101_STUDENT_H
|
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#include "UnparseableDateException.h"
|
||||
|
||||
UnparseableDateException::UnparseableDateException(std::string message) : message(message){}
|
||||
|
||||
const char* UnparseableDateException::what() const noexcept {
|
||||
std::string ret = "Unparseable date: " + message;
|
||||
char* c = const_cast<char *>(ret.c_str());
|
||||
return c;
|
||||
}
|
||||
|
||||
std::string UnparseableDateException::toString() {
|
||||
return "Unparseable date: " + message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#ifndef NALOGA1101_UNPARSEABLEDATEEXCEPTION_H
|
||||
#define NALOGA1101_UNPARSEABLEDATEEXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
|
||||
class UnparseableDateException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
explicit UnparseableDateException(std::string message);
|
||||
|
||||
const char* what() const noexcept override;
|
||||
|
||||
std::string toString();
|
||||
};
|
||||
|
||||
|
||||
#endif //NALOGA1101_UNPARSEABLEDATEEXCEPTION_H
|
20
semester_2/programiranje_2/naloga1102/main.cpp
Normal file
20
semester_2/programiranje_2/naloga1102/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include "Student.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
|
||||
std::vector<std::shared_ptr<Student>> test;
|
||||
|
||||
test = Student::LoadFromFile("students.csv");
|
||||
|
||||
for (int i = 0; i < test.size(); ++i) {
|
||||
std::cout << test[i]->toString();
|
||||
}
|
||||
|
||||
Student::SaveToFile(test,"test.csv");
|
||||
|
||||
Log(LogType::ERROR) << "This is additional message. We can also put multiple << and other types, not just strings e.g. " << "\n" ;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user