consolidate all repos to one for archive
This commit is contained in:
24
semester_2/programiranje_2/naloga1101/Address.cpp
Normal file
24
semester_2/programiranje_2/naloga1101/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/naloga1101/Address.h
Normal file
28
semester_2/programiranje_2/naloga1101/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
|
29
semester_2/programiranje_2/naloga1101/Date.cpp
Normal file
29
semester_2/programiranje_2/naloga1101/Date.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "Date.h"
|
||||
|
||||
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if(dd[0] > months[dd[1] - 1] or dd[1] > 12) throw InvalidDateException(date);
|
||||
|
||||
return {dd[0], dd[1], dd[2]};
|
||||
}
|
24
semester_2/programiranje_2/naloga1101/Date.h
Normal file
24
semester_2/programiranje_2/naloga1101/Date.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef NALOGA0201_DATE_H
|
||||
#define NALOGA0201_DATE_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include "UnparseableDateException.h"
|
||||
#include "InvalidDateException.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
|
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by Nik on 23/05/2022.
|
||||
//
|
||||
|
||||
#include "InvalidDateException.h"
|
||||
|
||||
InvalidDateException::InvalidDateException(std::string message) : message("InvalidDateException: " + message) {
|
||||
|
||||
}
|
||||
|
||||
const char *InvalidDateException::what() const noexcept {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
std::string InvalidDateException::toString() {
|
||||
return message;
|
||||
}
|
23
semester_2/programiranje_2/naloga1101/InvalidDateException.h
Normal file
23
semester_2/programiranje_2/naloga1101/InvalidDateException.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by Nik on 23/05/2022.
|
||||
//
|
||||
|
||||
#ifndef NALOGA1101_INVALIDDATEEXCEPTION_H
|
||||
#define NALOGA1101_INVALIDDATEEXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
class InvalidDateException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
explicit InvalidDateException(std::string message);
|
||||
|
||||
const char* what() const noexcept override;
|
||||
|
||||
std::string toString();
|
||||
};
|
||||
|
||||
|
||||
#endif //NALOGA1101_INVALIDDATEEXCEPTION_H
|
37
semester_2/programiranje_2/naloga1101/README.md
Normal file
37
semester_2/programiranje_2/naloga1101/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
Prenesite si datoteko students.csv v kateri se nahaja seznam študentov. V datoteki je v vsaki posamezni vrstici zapisan študent v obliki id,name,surname,dateOfBirth,street,post,country. Datum je zapisan v formatu day.month.year.
|
||||
|
||||
Primer ene vrstice:
|
||||
|
||||
100432031,Simon,Novak,3.4.2001,Partizanska cesta 1,1000 Ljubljana,Slovenija
|
||||
|
||||
Napišite razred Address, ki ima:
|
||||
instančne spremenljivke street, post, country in vse naj bodo tipa string
|
||||
konstruktor s 3 parametri (za vsako instančno spremenljivko)
|
||||
metodo toString
|
||||
|
||||
Uporabite razred Date iz predhodnih nalog. Razred naj ima:
|
||||
instančne spremenljivke day, month, year in vse naj bodo tipa int
|
||||
konstruktor s tremi paramteri (za vsako instančno spremenljivko)
|
||||
metode get za vsako instančno spremenljivko
|
||||
metodo toString ter dodatno:
|
||||
razredo metodo (static) GetDateFromString, ki kot edini parameter prejme datum v obliki niza in ga vrne kot Date. V metodi prožite/vržite (throw) izjemo (UnparseableDateException), če podan niz, ki vsebuje datum, ni v pravem formatu. Izjemo npr. prožite, če datum ne vsebuje točno dve piki in če je namesto številke zapisana črka.
|
||||
|
||||
Napišite razred Student, ki ima:
|
||||
instančne spremenljivke id (unsigned int), name (string), surname (string), dateOfBirth (Date), address (Address)
|
||||
konstruktor s 4 parametri (za vsako instančno spremenljivko)
|
||||
metodo toString
|
||||
razredno (static) metodo std::vector<std::shared_ptr<Student>> LoadFromFile(const std::string &filename), ki iz datoteke prebere seznam študentov, jih shrani v vektor in le-tega vrne. Pri branju posameznega študenta iz datoteke v primeru napačnega formata datuma ulovite izjemo UnparseableDateException (try - catch blok)
|
||||
razredno (static) metodo void SaveToFile(const std::vector<std::shared_ptr<Student>> &students, const std::string &fileName), ki v datoteko zapiše prejeti seznam študentov. Pri tem zapišite v formatu, da boste lahko z LoadFromFile prebrali.
|
||||
|
||||
Napišite izjemo (razred) UnparseableDateException, ki naj deduje iz sistemskega razreda std::exception. Potrebno je povoziti (angl. override) metodo const char* what() const noexcept iz nadrazreda exception. Razredu UnparseableDateException dodajte še lastnost std::string message in konstruktor, ki kot parameter prejme (napačen) datum v obliki niza in v spremenljivko message shranimo npr. niz Unparseable date: "21.5a.2020".
|
||||
|
||||
Pomagajte si z naslednjo povezavo: http://www.cplusplus.com/doc/tutorial/exceptions/.
|
||||
|
||||
V glavnem programu najprej iz datoteke preberite študente in vektor s študenti izpišite v konzolo.
|
||||
|
||||
V nalogi uporabljate pametne kazalce za ustvarjanje objektov shared_ptr iz standarda C++11. (https://en.cppreference.com/w/cpp/memory/shared_ptr)
|
||||
|
||||
Pomoč za delo z datotekami:
|
||||
Za delo z datotekami si pomagajte z naslednjo povezavo: http://www.cplusplus.com/doc/tutorial/files/. Glede na to, da so podatki shranjeni v csv (angl. comma-separated values) formatu, si lahko pomagate z metodo getline(istream &is, string &str, char delim), kjer parameter is predstavlja objekt istream, iz katerega preberemo podatke, parameter str je spremenljivka, v katero se shrani prebran niz in parameter delim predstavlja ločilo, do katerega bo (trenutni) niz prebran. V vašem primeru bo prvi parameter predstavljal vhodno datoteko, drugi parameter bo spremenljivka, v katero boste shranili prebrano vrednost, tretja vrednost pa bo znak vejica (,).
|
||||
|
||||
students.csv
|
64
semester_2/programiranje_2/naloga1101/Student.cpp
Normal file
64
semester_2/programiranje_2/naloga1101/Student.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#include "Student.h"
|
||||
#include <fstream>
|
||||
#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;
|
||||
|
||||
Address address;
|
||||
|
||||
while (std::getline(file,line)){
|
||||
std::stringstream word(line);
|
||||
std::string tmp;
|
||||
try {
|
||||
std::getline(word,tmp,',');
|
||||
id = std::stoi(tmp);
|
||||
std::getline(word,tmp,',');
|
||||
name = tmp;
|
||||
std::getline(word,tmp,',');
|
||||
surname = tmp;
|
||||
std::getline(word,tmp,',');
|
||||
Date dateOfBirth = Date::getDateFromString(tmp);
|
||||
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);
|
||||
|
||||
}catch (UnparseableDateException e){
|
||||
std::cout << e.what() << "\n";
|
||||
}catch (InvalidDateException e){
|
||||
std::cout << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Student::SaveToFile(const std::vector<std::shared_ptr<Student>> &students, const std::string &fileName) {
|
||||
std::ofstream file (fileName);
|
||||
for (int i = 0; i < students.size(); ++i) {
|
||||
file << students[i]->toString();
|
||||
}
|
||||
file.close();
|
||||
}
|
32
semester_2/programiranje_2/naloga1101/Student.h
Normal file
32
semester_2/programiranje_2/naloga1101/Student.h
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// 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"
|
||||
|
||||
|
||||
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,20 @@
|
||||
//
|
||||
// Created by Nik on 20/05/2022.
|
||||
//
|
||||
|
||||
#include "UnparseableDateException.h"
|
||||
#include <cstring>
|
||||
|
||||
UnparseableDateException::UnparseableDateException(std::string message) : message("Unparseable date: " + message){}
|
||||
|
||||
const char* UnparseableDateException::what() const noexcept {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
std::string UnparseableDateException::toString() {
|
||||
return 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
|
19
semester_2/programiranje_2/naloga1101/main.cpp
Normal file
19
semester_2/programiranje_2/naloga1101/main.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#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");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
6
semester_2/programiranje_2/naloga1101/students.csv
Normal file
6
semester_2/programiranje_2/naloga1101/students.csv
Normal file
@@ -0,0 +1,6 @@
|
||||
100132321,Marko,Leska,3.4.2002,Smetanova cesta 1,2000 Maribor,Slovenija
|
||||
100432031,Simon,Novak,3.4.2001,Partizanska cesta 1,1000 Ljubljana,Slovenija
|
||||
100532028,Mitja,Novak,12.1.2001,Vrazova ulica 1,9000 Murska Sobota,Slovenija
|
||||
100212029,Milica,Radojev,60.4.2001,Malesa ulica 1,116417 Beograd,Srbija
|
||||
100456020,Ana,Gard,6.13.1999,Testna ulica 5,2000 Maribor,Slovenija
|
||||
100456021,Eva,Gard,6.12.199a,Testna ulica 5,2000 Maribor,Slovenija
|
|
Reference in New Issue
Block a user