initial commit

This commit is contained in:
marcin 2019-03-30 22:30:16 +01:00
commit 45fd3d0f65
13 changed files with 461 additions and 0 deletions

5
README Normal file
View File

@ -0,0 +1,5 @@
Miłosz Makowski
Zadania + info milosz.faculty.wmi.amu.edu.pl
Zaliczenie: Klokwium + projekt (2 języki)

9
logs/log1.txt Normal file
View File

@ -0,0 +1,9 @@
cpp1
cpp2
ilovecpp
linuxoverwindows
123
456
789
abc
qwe

9
logs/log2.txt Normal file
View File

@ -0,0 +1,9 @@
cpp1
cpp2
ilovecpp
linuxoverwindows
123
456
789
abc
qwe

16
zad1_1.py Normal file
View File

@ -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)

25
zad1_2.py Normal file
View File

@ -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)

56
zad2_1.py Normal file
View File

@ -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

BIN
zad2_2 Executable file

Binary file not shown.

97
zad2_2.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
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<Produkt> products;
};
int main(){
vector<Produkt*> 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;
}
}
}

BIN
zad3_1 Executable file

Binary file not shown.

66
zad3_1.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Logger{
public:
virtual void log(string textToLog)=0;
virtual void del(){};
vector<string> 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<Logger*> &logs, string text){
for(Logger* l : logs)
l->log(text);
}
void stopLogging(vector<Logger*> &logs){
for(Logger* l : logs)
l->del();
}
int main(void){
vector<Logger*> 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<string> texts={"cpp1", "cpp2", "ilovecpp", "linuxoverwindows"};
for(string text : texts)
saveLogs(logs, text);
stopLogging(logs);
}

50
zad3_1.py Normal file
View File

@ -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)

BIN
zad5_1 Executable file

Binary file not shown.

128
zad5_1.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <sstream>
using namespace std;
class Person;
class Vehicle;
class Entity {
private:
static int nextId;
protected:
static vector<Entity*> 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*> Entity::entities;
class Vehicle : public Entity {
friend class Person;
private:
set<int> 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<Person*> 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();
}