commit 45fd3d0f659c901c8b0b2f669896575c87140f6c Author: marcin Date: Sat Mar 30 22:30:16 2019 +0100 initial commit diff --git a/README b/README new file mode 100644 index 0000000..3c7b144 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +Miłosz Makowski + +Zadania + info milosz.faculty.wmi.amu.edu.pl + +Zaliczenie: Klokwium + projekt (2 języki) \ No newline at end of file diff --git a/logs/log1.txt b/logs/log1.txt new file mode 100644 index 0000000..877d834 --- /dev/null +++ b/logs/log1.txt @@ -0,0 +1,9 @@ +cpp1 +cpp2 +ilovecpp +linuxoverwindows +123 +456 +789 +abc +qwe diff --git a/logs/log2.txt b/logs/log2.txt new file mode 100644 index 0000000..877d834 --- /dev/null +++ b/logs/log2.txt @@ -0,0 +1,9 @@ +cpp1 +cpp2 +ilovecpp +linuxoverwindows +123 +456 +789 +abc +qwe diff --git a/zad1_1.py b/zad1_1.py new file mode 100644 index 0000000..1bc11e7 --- /dev/null +++ b/zad1_1.py @@ -0,0 +1,16 @@ +class Przedmiot(): + def __init__(self, subject): + self.name=subject + + def __str__(self): + return self.name + +subjects=[] +subjects.append(Przedmiot("Maths")) +subjects.append(Przedmiot("Physics")) +subjects.append(Przedmiot("PE")) +subjects.append(Przedmiot("Chemistry")) +subjects.append(Przedmiot("Computer Science")) + +for subject in subjects: + print(subject) \ No newline at end of file diff --git a/zad1_2.py b/zad1_2.py new file mode 100644 index 0000000..ab7eb9f --- /dev/null +++ b/zad1_2.py @@ -0,0 +1,25 @@ +class Vector2D(): + def __init__(self, xCoord, yCoord): + self.x=xCoord + self.y=yCoord + + def add(self, vec): + return Vector2D(self.x+vec.x, self.y+vec.y) + + def multiply(self, vec): + return (float(self.x*vec.x) + float(self.y*vec.y)) + + def __str__(self): + return ("[" + str(self.x) + ", " + str(self.y) + "]") + +# test data + +vec1=Vector2D(-3, 1.4) +vec2=Vector2D(2, 2.7) +vec3=vec1.add(vec2) + +vec4=vec1.multiply(vec2) + +print(vec3) +print(vec4) + diff --git a/zad2_1.py b/zad2_1.py new file mode 100644 index 0000000..64dd8ab --- /dev/null +++ b/zad2_1.py @@ -0,0 +1,56 @@ +class Produkt(): + def __init__(self, id, name, price): + self.id=id + self.name=name + self.price=price + def __str__(self): + return str(self.id) + ' Nazwa: ' + self.name + '\n' + 'Cena: ' + str(self.price) + 'PLN' + +class Koszyk(): + def __init__(self, clientName): + self.clientName=clientName + self.due=0 + self.products=[] + def dodajProdukt(self, produkt): + self.products.append(produkt) + self.due+=produkt.price + self.due=round(self.due, 2) + print('Dodano produkt do koszyka') + def lacznyKoszt(self): + return "Do zapłaty: " + str(self.due) + def __str__(self): + return ('Koszyk użytkownika ' + self.clientName + + '\nProdukty w koszyku:\n' + + '\n'.join(map(str, self.products))) + +products=[Produkt(1, 'Jabłko', 3.19), + Produkt(2, 'Pomarańcza', 2.59), + Produkt(3, 'Banan', 5.99), + Produkt(4, 'Śliwka', 4.89)] + +print("Wprowadź nazwę użytkownika: ", end='') +username=input() +koszyk=Koszyk(username) +print("Witaj " + username + "!") +print("Lista dostępnych produktów:") +for product in products: + print(product) +print("Aby dodać produkt do koszyka, wprowadź jego numer. Wpisz '0' aby zobaczyć zawartość swojego koszyka.") +while True: + while True: + choice=int(input()) + if choice > len(products) or choice < 0: + print("Nie ma produktu o takim numerze.") + continue + elif choice==0: + print(koszyk) + break + else: + koszyk.dodajProdukt(products[choice-1]) + print(koszyk.lacznyKoszt()) + print("Chcesz zakończyć zakupy i zapłacić? [Y/n]", end=' ') + wantExit=input() + if wantExit=='Y' or wantExit=='y': + print("Zapłacono " + str(koszyk.due) + '.') + print("Dziękujemy za zakupy!") + break \ No newline at end of file diff --git a/zad2_2 b/zad2_2 new file mode 100755 index 0000000..231571d Binary files /dev/null and b/zad2_2 differ diff --git a/zad2_2.cpp b/zad2_2.cpp new file mode 100644 index 0000000..970dd37 --- /dev/null +++ b/zad2_2.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +using namespace std; + +class Produkt{ + public: + float price; + Produkt(int iId, string iName, float iPrice){ + id=iId; + name=iName; + price=iPrice; + } + string description(){ + stringstream s; + s << id << " Nazwa: " << name << "\n" << "Cena: " + << price << "PLN" << endl; + return s.str(); + } + private: + int id; + string name; +}; + +class Koszyk{ + public: + float due; + Koszyk(string iClientName){ + clientName=iClientName; + due=0; + } + void dodajProdukt(Produkt produkt){ + products.push_back(produkt); + due+=produkt.price; + cout << "Dodano produkt do koszyka" << endl; + } + string lacznyKoszt(){ + stringstream s; + s << "Do zapłaty: " << due << "PLN.\n"; + return s.str(); + } + string description(){ + stringstream s; + s << "Koszyk użytkownika " << clientName << "\nProdukty w koszyku\n"; + for(Produkt x : products) + s << x.description(); + s << endl; + return s.str(); + } + private: + string clientName; + vector products; + +}; + +int main(){ + vector products; + products.push_back(new Produkt(1, "Jabłko", 3.19)); + products.push_back(new Produkt(2, "Pomarańcza", 2.59)); + products.push_back(new Produkt(3, "Banan", 5.99)); + products.push_back(new Produkt(4, "Śliwka", 4.89)); + + cout << "Wprowadź nazwę użytkownika: " << endl; + string username; + cin >> username; + Koszyk* koszyk = new Koszyk(username); + cout << "Witaj " << username << "!\n" + << "Lista dostępnych produktów:\n"; + for(Produkt *p : products) + cout << p->description(); + cout << "Aby dodać produkt do koszyka, wprowadź jego numer. Wpisz '0' aby zobaczyć zawartość swojego koszyka.\n"; + while(true){ + while(true){ + string choice; + cin >> choice; + int choiceNumber=stoi(choice, nullptr, 10); + if(choiceNumber > products.size()){ + cout << "Nie ma produktu o takim numerze.\n"; + continue; + } else if(choiceNumber==0){ + cout << koszyk->description(); + break; + } else + koszyk->dodajProdukt(*products[choiceNumber-1]); + } + cout << koszyk->lacznyKoszt() << "\nChcesz zakończyć zakupy i zapłacić? [Y/n]\n"; + string wantExit; + cin >> wantExit; + if(wantExit[0]=='Y' || wantExit[0]=='y'){ + cout << "Zapłacono " << koszyk->due << ".\nDziękujemy za zakupy!"; + break; + } + } +} + diff --git a/zad3_1 b/zad3_1 new file mode 100755 index 0000000..078107e Binary files /dev/null and b/zad3_1 differ diff --git a/zad3_1.cpp b/zad3_1.cpp new file mode 100644 index 0000000..fca5a8f --- /dev/null +++ b/zad3_1.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +using namespace std; + +class Logger{ + public: + virtual void log(string textToLog)=0; + virtual void del(){}; + + vector history; + + protected: + virtual void history_add(string textToLog){ + history.push_back(textToLog); + } +}; + +class FileLogger : public Logger{ + public: + FileLogger(string filePath){ + logFile.open(filePath); + } + void log(string textToLog){ + history_add(textToLog); + logFile << textToLog << "\n"; + } + void del(){ + logFile.close(); + } + private: + ofstream logFile; +}; + +class StdoutLogger : public Logger{ + public: + void log(string textToLog){ + history_add(textToLog); + cout << textToLog << "\n"; + } +}; + +void saveLogs(vector &logs, string text){ + for(Logger* l : logs) + l->log(text); +} + +void stopLogging(vector &logs){ + for(Logger* l : logs) + l->del(); +} + +int main(void){ + vector logs={new FileLogger("/home/marcin/Documents/Coding/POB_2019/logs/log1.txt"), + new FileLogger("/home/marcin/Documents/Coding/POB_2019/logs/log2.txt"), + new StdoutLogger()}; + vector texts={"cpp1", "cpp2", "ilovecpp", "linuxoverwindows"}; + for(string text : texts) + saveLogs(logs, text); + stopLogging(logs); + +} + + + diff --git a/zad3_1.py b/zad3_1.py new file mode 100644 index 0000000..2923f1a --- /dev/null +++ b/zad3_1.py @@ -0,0 +1,50 @@ +import os + +class Logger(): + def __init__(self): + self.history=[] + def history_add(self, textToLog): + self.history.append(textToLog) + def log(self, textToLog): + self.history_add(textToLog) + def __del__(self): + pass + +class FileLogger(Logger): + def __init__(self, filePath): + self.history=[] + self.logFile=open(filePath, "a+") + def log(self, textToLog): + self.history_add(textToLog) + self.logFile.write(textToLog + "\n") + def __del__(self): + self.logFile.close() + +class StdoutLogger(Logger): + def log(self, textToLog): + self.history_add(textToLog) + print(textToLog) + + +def saveLogs(loggers, text): + for log in loggers: + log.log(text) + +def stopLogging(loggers): + for log in loggers: + del log + + +dir=os.path.join(os.path.dirname(__file__), "logs") + +loggers=[StdoutLogger(), + FileLogger(os.path.join(dir, "log1.txt")), + FileLogger(os.path.join(dir, "log2.txt"))] + +texts=["123", "456", "789", + "abc", "qwe"] + +for line in texts: + saveLogs(loggers, line) + +stopLogging(loggers) \ No newline at end of file diff --git a/zad5_1 b/zad5_1 new file mode 100755 index 0000000..34225a5 Binary files /dev/null and b/zad5_1 differ diff --git a/zad5_1.cpp b/zad5_1.cpp new file mode 100644 index 0000000..2c66fff --- /dev/null +++ b/zad5_1.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include + +using namespace std; + +class Person; +class Vehicle; + +class Entity { + private: + static int nextId; + protected: + static vector entities; + int id; + public: + Entity(){ + id=nextId++; + entities.push_back(this); + } + static Entity* getEntityById(int _id){ + for(Entity *entity : entities){ + if (entity->id==_id) + return entity; + } + cout << "Could not find any entity with id " << _id << ".\n"; + return nullptr; + } + int getId(){ + return id; + } + virtual ~Entity(){ + for(unsigned int i=0; i<=entities.size(); i++){ + if (entities[i]->id==id) + entities.erase(entities.begin()+i); + } + } +}; + +int Entity::nextId=0; +vector Entity::entities; + +class Vehicle : public Entity { + friend class Person; + private: + set idsInside; + const short unsigned int limit=5; + public: + void addPerson(Person &person); + int occupiedSpaces(){ + return idsInside.size(); + } + void whoIsInside(){ + cout << "People inside car " << id << ":\n"; + if(idsInside.size()==0) + return; + for(int pid : idsInside){ + cout << pid << endl; + } + return; + } + virtual ~Vehicle(); +}; + +class Person : public Entity { + friend class Vehicle; + private: + int vehicleId; + bool isInVehicle; + public: + Person(){ + vehicleId=-1; + isInVehicle=false; + } + void getOut(){ + if(vehicleId==-1){ + cout << "This person is not inside any vehicle.\n"; + return; + } + Vehicle* vehicle=(Vehicle*)getEntityById(vehicleId); + vehicle->idsInside.erase(vehicle->idsInside.find(id)); + vehicleId=-1; + isInVehicle=false; + } + virtual ~Person(){ + getOut(); + } +}; + +void Vehicle::addPerson(Person &person){ + if(idsInside.size()>=limit) { + cout << "This vehicle has no more room for another passenger.\n"; + return; + } else if (person.isInVehicle==true){ + cout << "The person is alredy in some other vehicle.\n"; + return; + } else { + idsInside.insert(person.getId()); + person.isInVehicle=true; + person.vehicleId=id; + } +} + +Vehicle::~Vehicle(){ + for(int id : idsInside){ + Person* personInside=(Person*)getEntityById(id); + personInside->getOut(); + } +} + +int main(){ + vector people; + for(int i=0; i<10; i++) + people.push_back(new Person); + Vehicle* car1=new Vehicle(); + for(Person *newguy : people) + car1->addPerson(*newguy); + delete people[0]; + people.erase(people.begin()); + car1->whoIsInside(); + delete car1; + Vehicle* car2=new Vehicle(); + for(Person *newguy : people) + car2->addPerson(*newguy); + car2->whoIsInside(); +} \ No newline at end of file