diff options
added bar task
20 files changed, 697 insertions, 15 deletions
diff --git a/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp new file mode 100644 index 0000000..ddd890e --- /dev/null +++ b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp @@ -0,0 +1,19 @@ +#pragma once +#include "AlcoholDrink.h" + +AlcoholDrink::AlcoholDrink():alcP(5) {} + +AlcoholDrink::AlcoholDrink(const MyString& name, int ml, int alcP) :Drink(name, ml) +{ + setAlcP(alcP); +} +void AlcoholDrink::setAlcP(int alcP) +{ + if (alcP < 5 || alcP > 98) + alcP = 50; + this->alcP = alcP; +} +int AlcoholDrink::getAlcP() const +{ + return alcP; +}
\ No newline at end of file diff --git a/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h new file mode 100644 index 0000000..3029580 --- /dev/null +++ b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h @@ -0,0 +1,15 @@ +#pragma once +#include "../Drink/Drink.h" +#include "../Queue/Queue.hpp" +#include "../Pair/Pair.hpp" + +class AlcoholDrink : public Drink +{ + int alcP; +public: + AlcoholDrink(); + AlcoholDrink(const MyString& name, int ml, int alcP); + + void setAlcP(int alcP); + int getAlcP() const; +};
\ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Bar/Bar.cpp b/Sem. 11/Examples/Bar/Bar/Bar.cpp new file mode 100644 index 0000000..2d20195 --- /dev/null +++ b/Sem. 11/Examples/Bar/Bar/Bar.cpp @@ -0,0 +1,53 @@ +#include "Bar.h" + +void Bar::addAlcoholDrink(const AlcoholDrink& dr, size_t count) +{ + if (count > 0) + alcoholDrinks.push(Pair<AlcoholDrink, size_t>(dr, count)); +} +void Bar::addDrink(const Drink& dr, size_t count) +{ + if (count > 0) + drinks.push(Pair<Drink, size_t >(dr, count)); +} + +Drink Bar::getDrink() +{ + + if (drinks.isEmpty()) + throw std::logic_error("Empty drinks collection"); + + Drink toReturn = drinks.peek().getFirst(); + + stats.mlSold += toReturn.getMl(); + stats.soldDrinks++; + + drinks.peek().setSecond(drinks.peek().getSecond() - 1); + if (drinks.peek().getSecond() == 0) + drinks.pop(); + + return toReturn; +} + +AlcoholDrink Bar::getAlcDrink() +{ + if (alcoholDrinks.isEmpty()) + throw std::logic_error("Empty alc. drinks collection"); + + AlcoholDrink toReturn = alcoholDrinks.peek().getFirst(); + + stats.mlSold += toReturn.getMl(); + stats.soldAlcoholDrinks++; + + alcoholDrinks.peek().setSecond(alcoholDrinks.peek().getSecond() - 1); + if (alcoholDrinks.peek().getSecond() == 0) + alcoholDrinks.pop(); + + return toReturn; + +} + +const Bar::Statistics& Bar::getStats() const +{ + return stats; +}
\ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Bar/Bar.h b/Sem. 11/Examples/Bar/Bar/Bar.h new file mode 100644 index 0000000..4bf5513 --- /dev/null +++ b/Sem. 11/Examples/Bar/Bar/Bar.h @@ -0,0 +1,31 @@ +#pragma once +#include "../Queue/Queue.hpp" +#include "../Pair/Pair.hpp" +#include "../AlcoholDrink/AlcoholDrink.h" + +class Bar +{ + MyQueue<Pair<AlcoholDrink, size_t>> alcoholDrinks; + MyQueue<Pair<Drink, size_t>> drinks; + +public: + struct Statistics + { + size_t soldDrinks; + size_t soldAlcoholDrinks; + size_t mlSold; + } stats; + + + Bar() = default; + + void addAlcoholDrink(const AlcoholDrink& dr, size_t count = 1); + void addDrink(const Drink& dr, size_t count = 1); + + + Drink getDrink(); + AlcoholDrink getAlcDrink(); + + const Statistics& getStats() const; + +}; diff --git a/Sem. 11/Examples/Bar/Drink/Drink.cpp b/Sem. 11/Examples/Bar/Drink/Drink.cpp new file mode 100644 index 0000000..4bb921f --- /dev/null +++ b/Sem. 11/Examples/Bar/Drink/Drink.cpp @@ -0,0 +1,62 @@ +#include "Drink.h" + +Drink::Drink() : Drink("Unknown", 0) +{} + +const MyString& Drink::getName() const +{ + return name; +} +int Drink::getMl() const +{ + return ml; +} + +bool isLower(char ch) +{ + return ch >= 'a' && ch <= 'z'; +} + +bool areAllLowerCase(const char* str) +{ + while (*str) + { + if (!isLower(*str)) + return false; + str++; + } + return true; +} + +bool isUpper(char ch) +{ + return ch >= 'A' && ch <= 'Z'; +} + +bool isNameValid(const char* name) +{ + return isUpper(*name) && areAllLowerCase(name + 1); +} + +void Drink::setName(const MyString& name) +{ + if (isNameValid(name.c_str())) + this->name = name; +} +void Drink::setMl(int ml) +{ + const int MAX_ML = 1000; + const int MIN_ML = 100; + + ml = MIN_ML; + if (MIN_ML <= ml && ml <= MAX_ML) + this->ml = ml; + +} + +Drink::Drink(const MyString& name, int ml) +{ + setName(name); + setMl(ml); +} + diff --git a/Sem. 11/Examples/Bar/Drink/Drink.h b/Sem. 11/Examples/Bar/Drink/Drink.h new file mode 100644 index 0000000..d48a725 --- /dev/null +++ b/Sem. 11/Examples/Bar/Drink/Drink.h @@ -0,0 +1,21 @@ +#pragma once +#include "../MyString/MyString.h" + +class Drink +{ +public: + + Drink(); + Drink(const MyString& name, int ml); + + const MyString& getName() const; + int getMl() const; + + void setName(const MyString& name); + void setMl(int ml); + +private: + MyString name; + int ml; + +};
\ No newline at end of file diff --git a/Sem. 11/Examples/Bar/MyString/MyString.cpp b/Sem. 11/Examples/Bar/MyString/MyString.cpp new file mode 100644 index 0000000..3e60278 --- /dev/null +++ b/Sem. 11/Examples/Bar/MyString/MyString.cpp @@ -0,0 +1,142 @@ +#include "MyString.h" +#include <cstring> +#include <iostream> + +void MyString::copyFrom(const MyString& other) +{ + str = new char[strlen(other.str) + 1]; + strcpy(str, other.str); + size = other.size; +} +void MyString::free() +{ + delete[] str; +} + +MyString::MyString() +{ + str = new char[1]; + str[0] = '\0'; + size = 0; +} + +MyString::MyString(const char* data) +{ + if(data == nullptr) + { + str = new char[1]; + str[0] = '\0'; + size = 0; + } + else + { + size = strlen(data); + str = new char[size + 1]; + strcpy(str, data); + } +} + +MyString::MyString(const MyString& other) +{ + copyFrom(other); +} +MyString& MyString::operator=(const MyString& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +MyString::~MyString() +{ + free(); +} + + +size_t MyString::getSize() const +{ + return size; +} +void MyString::concat(const MyString& other) +{ + char* temp = new char[getSize() + other.getSize() + 1]; + strcpy(temp, str); + strcat(temp, other.str); + + delete[] str; + str = temp; + size = size + other.getSize(); +} + +const char* MyString::c_str() const +{ + return str; +} + +MyString& MyString::operator+=(const MyString& other) +{ + concat(other); + return *this; +} + +MyString operator+(const MyString& lhs, const MyString& rhs) +{ + MyString copyOfLeft(lhs); + copyOfLeft += rhs; + return copyOfLeft; +} + +std::ostream& operator<<(std::ostream& stream, const MyString& str) +{ + stream << str.str; + return stream; +} + +std::istream& operator>>(std::istream& stream, MyString& str) +{ + delete[] str.str; + char buff[1024]; + stream >> buff; + + str.size = strlen(buff); + str.str = new char[str.size + 1]; + strcpy(str.str, buff); + + return stream; +} + +bool operator==(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) == 0; +} +bool operator<=(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) <= 0; + +} +bool operator<(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) < 0; + +} + +MyString::MyString(MyString&& otherString) +{ + str = otherString.str; + size = otherString.size; + otherString.str = nullptr; +} + +MyString& MyString::operator=(MyString&& otherString) +{ + if (this != &otherString) + { + free(); + str = otherString.str; + otherString.str = nullptr; + size = otherString.size; + } + return *this; +} diff --git a/Sem. 11/Examples/Bar/MyString/MyString.h b/Sem. 11/Examples/Bar/MyString/MyString.h new file mode 100644 index 0000000..b37733a --- /dev/null +++ b/Sem. 11/Examples/Bar/MyString/MyString.h @@ -0,0 +1,44 @@ +#pragma once + +#include <fstream> +class MyString +{ + char* str; + size_t size; + + void copyFrom(const MyString& other); + void free(); + +public: + MyString(); + MyString(const char*); + + MyString(const MyString&); + + MyString(MyString&&); //move constructor that accepts rvalue; + + MyString& operator=(const MyString& other); + MyString& operator=(MyString&& other); //move op= that accepts rvalue; + + ~MyString(); + + size_t getSize() const; + void concat(const MyString& other); + + const char* c_str() const; + + MyString& operator+=(const MyString& other); + + friend std::ostream& operator<<(std::ostream& stream, const MyString& str); + friend std::istream& operator>>(std::istream& stream, MyString& str); + +}; + +MyString operator+(const MyString& lhs, const MyString& rhs); + +bool operator==(const MyString& lhs, const MyString& rhs); +bool operator<=(const MyString& lhs, const MyString& rhs); +bool operator<(const MyString& lhs, const MyString& rhs); + + +//MyString name = "sdfsdf"
\ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Pair/Pair.hpp b/Sem. 11/Examples/Bar/Pair/Pair.hpp new file mode 100644 index 0000000..5f83d0c --- /dev/null +++ b/Sem. 11/Examples/Bar/Pair/Pair.hpp @@ -0,0 +1,43 @@ +#pragma once + +template<typename T, typename D> +class Pair +{ + T first; + mutable D second; // For Drinks task!! +public: + Pair() = default; + Pair(const T& first, const D& second); + const T& getFirst() const; + const D& getSecond() const; + + void setFirst(const T& newValue); + void setSecond(const D& newValue) const ; +}; + +template<typename T, typename D> +Pair<T,D>::Pair(const T& first, const D& second) : first(first), second(second) +{} + +template<typename T, typename D> +const T& Pair<T, D>::getFirst() const +{ + return first; +} +template<typename T, typename D> +const D& Pair<T, D>::getSecond() const +{ + return second; +} + +template<typename T, typename D> +void Pair<T, D>::setFirst(const T& newValue) +{ + first = newValue; +} + +template<typename T, typename D> +void Pair<T, D>::setSecond(const D& newValue) const +{ + second = newValue; +}
\ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Queue/Queue.hpp b/Sem. 11/Examples/Bar/Queue/Queue.hpp new file mode 100644 index 0000000..5c9af06 --- /dev/null +++ b/Sem. 11/Examples/Bar/Queue/Queue.hpp @@ -0,0 +1,151 @@ +#pragma once +#include <iostream> +#include <exception> +template <typename T> +class MyQueue +{ + T* data; + size_t capacity; + size_t size; + + size_t get; + size_t put; + + void resize(); + + void copyFrom(const MyQueue<T>& other); + void free(); +public: + MyQueue(); + + MyQueue(const MyQueue<T>& other); + MyQueue<T>& operator=(const MyQueue<T>& other); + + void push(const T& obj); + void push(T&& obj); //позволява и местене + void pop(); + + const T& peek() const; + bool isEmpty() const; + + ~MyQueue(); + +}; + +template <typename T> +MyQueue<T>::MyQueue() +{ + capacity = 4; + data = new T[capacity]; + size = 0; + get = put = 0; +} + +template <typename T> +void MyQueue<T>::push(const T& obj) +{ + if (size == capacity) + resize(); + + data[put] = obj; //operator= + (++put) %= capacity; + size++; +} + +template <typename T> +void MyQueue<T>::push(T&& obj) +{ + if (size == capacity) + resize(); + + data[put] = std::move(obj); //move operator = + (++put) %= capacity; + size++; +} + +template <typename T> +bool MyQueue<T>::isEmpty() const +{ + return size == 0; +} + + +template <typename T> +const T& MyQueue<T>::peek() const +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + + return data[get]; +} + +template <typename T> +void MyQueue<T>::pop() +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + (++get) %= capacity; + size--; +} + +template <typename T> +void MyQueue<T>::resize() +{ + T* resizedData = new T[capacity * 2]; + for (size_t i = 0; i < size; i++) + { + resizedData[i] = data[get]; + (++get) %= capacity; + } + capacity *= 2; + delete[] data; + data = resizedData; + get = 0; + put = size; +} + + +template <typename T> +void MyQueue<T>::copyFrom(const MyQueue<T>& other) +{ + data = new T[other.capacity]; + for (int i = 0; i < other.capacity; i++) + data[i] = other.data[i]; + + get = other.get; + put = other.put; + + size = other.size; + capacity = other.capacity; + +} + +template <typename T> +void MyQueue<T>::free() +{ + delete[] data; +} + +template <typename T> +MyQueue<T>::MyQueue(const MyQueue<T>& other) +{ + copyFrom(other); + +} + +template <typename T> +MyQueue<T>& MyQueue<T>::operator=(const MyQueue<T>& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} + +template <typename T> +MyQueue<T>::~MyQueue() +{ + free(); +} diff --git a/Sem. 11/Examples/Bar/Source2.cpp b/Sem. 11/Examples/Bar/Source2.cpp new file mode 100644 index 0000000..e27b025 --- /dev/null +++ b/Sem. 11/Examples/Bar/Source2.cpp @@ -0,0 +1,23 @@ +#include <iostream> +#include "Bar/Bar.h" + +int main() +{ + Bar b; + + b.addDrink(Drink("Coke", 330), 2); //2x Coca cola 330 ml + + b.addDrink(Drink("Fanta", 500)); //1x Fanta 500 ml + + b.addAlcoholDrink(AlcoholDrink("Zagorka", 500, 5)); //1x Zagorka, 5% alc, 500 ml + + std::cout << b.getAlcDrink().getName() << std::endl; // Zagorka + + std::cout << b.getDrink().getName() << std::endl; // Coke + std::cout << b.getDrink().getName() << std::endl; // Coke + std::cout << b.getDrink().getName() << std::endl; // Fanta + + std::cout<< b.getStats().soldAlcoholDrinks << std::endl; // 1 + std::cout << b.getStats().soldDrinks << std::endl; // 3 + +}
\ No newline at end of file diff --git a/Sem. 11/Examples/Optional+pair.txt b/Sem. 11/Examples/Optional+pair.txt deleted file mode 100644 index a230c1e..0000000 --- a/Sem. 11/Examples/Optional+pair.txt +++ /dev/null @@ -1 +0,0 @@ -в sem10 са. diff --git a/Sem. 11/Examples/Person/Person.cpp b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp index 439dc2b..a0cc714 100644 --- a/Sem. 11/Examples/Person/Person.cpp +++ b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp @@ -60,8 +60,6 @@ void Person::setName(const char* name) void Person::setAge(int age) { - if(age < 0 || age > 120) - age = 40; this->age = age; } diff --git a/Sem. 11/Examples/Person/Person.h b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.h index 826b671..826b671 100644 --- a/Sem. 11/Examples/Person/Person.h +++ b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.h diff --git a/Sem. 11/Examples/Person, Student, Teacher/Source.cpp b/Sem. 11/Examples/Person, Student, Teacher/Source.cpp new file mode 100644 index 0000000..c2908c4 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Source.cpp @@ -0,0 +1,21 @@ +#include <iostream> +#include "Person/Person.h" +#include "Student/Student.h" +#include "Teacher/Teacher.h" + +int main() +{ + char subjects[3][20] = { "English", "Mathematics", "Physics" }; + + char** subjectsPointers = new char* [3]; + subjectsPointers[0] = subjects[0]; + subjectsPointers[1] = subjects[1]; + subjectsPointers[2] = subjects[2]; + + Teacher t("Prof. Ivanov", 33, subjectsPointers, 3); + + std::cout << t.getName() << std::endl; + + delete[] subjectsPointers; //only the pointers - not the data! + +} diff --git a/Sem. 11/Examples/Student/Student.cpp b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp index 8b5b04c..a79d1fd 100644 --- a/Sem. 11/Examples/Student/Student.cpp +++ b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp @@ -1,4 +1,4 @@ -#include "Student.h" + #include "Student.h" Student::Student(const char* name, int age, size_t fn) : Person(name, age) { diff --git a/Sem. 11/Examples/Student/Student.h b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h index d34763d..3e89213 100644 --- a/Sem. 11/Examples/Student/Student.h +++ b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h @@ -4,7 +4,6 @@ class Student : public Person { size_t fn = 0; - public: Student() = default; Student(const char* name, int age, size_t fn); diff --git a/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp new file mode 100644 index 0000000..d1b6bca --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp @@ -0,0 +1,55 @@ +#include "Teacher.h" + +Teacher::Teacher(const char* name, int age, const char* const* subjects, size_t subjectsCount) : Person(name, age) +{ + this->subjects = new char* [subjectsCount]; + + for (size_t i = 0; i < subjectsCount; i++) + { + this->subjects[i] = new char[strlen(subjects[i]) + 1]; + strcpy(this->subjects[i], subjects[i]); + } + this->subjectsCount = subjectsCount; +} + +void Teacher::free() +{ + for (size_t i = 0; i < subjectsCount; i++) + delete[] subjects[i]; + delete[] subjects; +} +void Teacher::copyFrom(const Teacher& other) +{ + subjects = new char* [other.subjectsCount]; + + for (size_t i = 0; i < other.subjectsCount; i++) + { + subjects[i] = new char[strlen(other.subjects[i]) + 1]; + strcpy(subjects[i], other.subjects[i]); + } + + subjectsCount = other.subjectsCount; +} + +Teacher::Teacher(const Teacher& other) : Person(other) +{ + copyFrom(other); //only the teacher stuff is copied +} + +Teacher& Teacher::operator=(const Teacher& other) +{ + if (this != &other) + { + Person::operator=(other); //delete data of person + copy the data from person + free(); //teacher + copyFrom(other); //teacher + } + return *this; +} + + + +Teacher::~Teacher() +{ + free(); +} // destr person diff --git a/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h new file mode 100644 index 0000000..5ef0bd1 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h @@ -0,0 +1,16 @@ +#include "../Person/Person.h" + +class Teacher : public Person +{ + char** subjects; + size_t subjectsCount; + + void free(); + void copyFrom(const Teacher& other);// само нещата, които са от Teacher +public: + Teacher(const char* name, int age, const char* const * subjects, size_t subjectsCount); + + Teacher(const Teacher& other); + Teacher& operator=(const Teacher& other); + ~Teacher(); +};
\ No newline at end of file diff --git a/Sem. 11/Examples/Source2.cpp b/Sem. 11/Examples/Source2.cpp deleted file mode 100644 index b6196dd..0000000 --- a/Sem. 11/Examples/Source2.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include <iostream> -#include "Student/Student.h" - - - -int main() -{ - Student s2("Fsdf", 34,4); - -} |
