From 00b22d8682827a6e8d0098437559537a04236aa4 Mon Sep 17 00:00:00 2001 From: angeld55 Date: Mon, 26 Apr 2021 01:11:36 +0300 Subject: sem 9/10 --- Sem. 09/task (not finished)/Grade.cpp | 69 --------------- Sem. 09/task (not finished)/Grade.h | 26 ------ Sem. 09/task (not finished)/Student.cpp | 45 ---------- Sem. 09/task (not finished)/Student.h | 29 ------- Sem. 09/task (not finished)/UniCourse.cpp | 135 ----------------------------- Sem. 09/task (not finished)/UniCourse.h | 35 -------- Sem. 09/task (not finished)/main.cpp | 15 ---- Sem. 09/task1 (not finished)/Grade.cpp | 69 +++++++++++++++ Sem. 09/task1 (not finished)/Grade.h | 26 ++++++ Sem. 09/task1 (not finished)/Student.cpp | 45 ++++++++++ Sem. 09/task1 (not finished)/Student.h | 29 +++++++ Sem. 09/task1 (not finished)/UniCourse.cpp | 135 +++++++++++++++++++++++++++++ Sem. 09/task1 (not finished)/UniCourse.h | 35 ++++++++ Sem. 09/task1 (not finished)/main.cpp | 15 ++++ Sem. 09/task2/Queue.hpp | 91 +++++++++++++++++++ Sem. 09/task2/main.cpp | 23 +++++ 16 files changed, 468 insertions(+), 354 deletions(-) delete mode 100644 Sem. 09/task (not finished)/Grade.cpp delete mode 100644 Sem. 09/task (not finished)/Grade.h delete mode 100644 Sem. 09/task (not finished)/Student.cpp delete mode 100644 Sem. 09/task (not finished)/Student.h delete mode 100644 Sem. 09/task (not finished)/UniCourse.cpp delete mode 100644 Sem. 09/task (not finished)/UniCourse.h delete mode 100644 Sem. 09/task (not finished)/main.cpp create mode 100644 Sem. 09/task1 (not finished)/Grade.cpp create mode 100644 Sem. 09/task1 (not finished)/Grade.h create mode 100644 Sem. 09/task1 (not finished)/Student.cpp create mode 100644 Sem. 09/task1 (not finished)/Student.h create mode 100644 Sem. 09/task1 (not finished)/UniCourse.cpp create mode 100644 Sem. 09/task1 (not finished)/UniCourse.h create mode 100644 Sem. 09/task1 (not finished)/main.cpp create mode 100644 Sem. 09/task2/Queue.hpp create mode 100644 Sem. 09/task2/main.cpp diff --git a/Sem. 09/task (not finished)/Grade.cpp b/Sem. 09/task (not finished)/Grade.cpp deleted file mode 100644 index 4302166..0000000 --- a/Sem. 09/task (not finished)/Grade.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "Grade.h" - - -Grade::Grade() : taskName(nullptr) -{ - setTaskName("Unknown"); - setGrade(2); -} - -Grade::Grade(const char* taskName, size_t gradeValue) : taskName(nullptr) -{ - setTaskName(taskName); - setGrade(gradeValue); -} - -void Grade::free() -{ - delete[] taskName; -} -void Grade::copyFrom(const Grade& other) -{ - gradeValue = other.gradeValue; - - taskName = new char[strlen(other.taskName) + 1]; - strcpy(taskName, other.taskName); -} - -Grade::Grade(const Grade& other) -{ - copyFrom(other); -} - -Grade& Grade::operator=(const Grade& other) -{ - if (this != &other) - { - free(); - copyFrom(other); - } - return *this; -} -Grade::~Grade() -{ - free(); -} - -const char* Grade::getTaskName() const -{ - return taskName; -} -size_t Grade::getGrade() const -{ - return gradeValue; -} - -void Grade::setGrade(size_t grade) -{ - if (grade < 2 || grade > 6) - throw std::exception("error!"); - - gradeValue = grade; -} - -void Grade::setTaskName(const char* name) -{ - delete[] taskName; - taskName = new char[strlen(name) + 1]; - strcpy(taskName, name); -} \ No newline at end of file diff --git a/Sem. 09/task (not finished)/Grade.h b/Sem. 09/task (not finished)/Grade.h deleted file mode 100644 index d0cb2e7..0000000 --- a/Sem. 09/task (not finished)/Grade.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include - -class Grade -{ - size_t gradeValue; - char* taskName; - - void free(); - void copyFrom(const Grade& other); -public: - Grade(); - - Grade(const char* taskName, size_t gradeValue); - - Grade(const Grade& other); - Grade& operator=(const Grade& other); - ~Grade(); - - const char* getTaskName() const; - size_t getGrade() const; - - void setTaskName(const char* name); - void setGrade(size_t grade); -}; \ No newline at end of file diff --git a/Sem. 09/task (not finished)/Student.cpp b/Sem. 09/task (not finished)/Student.cpp deleted file mode 100644 index 99d26dc..0000000 --- a/Sem. 09/task (not finished)/Student.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "Student.h" - -Student::Student(const char* name, size_t fn) -{ - setName(name); - setFn(fn); -} - -void Student::setFn(size_t fn) -{ - if (fn < 80000 || fn > 83000) - throw std::exception("Invalid fn"); - - this->fn = fn; -} -void Student::setName(const char* name) -{ - if (strlen(name) > MAX_NAME_LENGTH) - throw std::exception("Name is too long!"); - - strcpy(this->name, name); -} - -size_t Student::getFN() const -{ - return fn; -} -const char* Student::getName() const -{ - return name; -} - -void Student::addGrade(const char* taskName, size_t grade) -{ - if (gradesCount == MAX_GRADES_COUNT) - throw std::exception("Error! Too many grades!"); - - Grade currentGrade(taskName, grade); - grades[gradesCount++] = currentGrade; -} - -void Student::print() const -{ - std::cout << name << " " << fn << std::endl; -} \ No newline at end of file diff --git a/Sem. 09/task (not finished)/Student.h b/Sem. 09/task (not finished)/Student.h deleted file mode 100644 index 5c09792..0000000 --- a/Sem. 09/task (not finished)/Student.h +++ /dev/null @@ -1,29 +0,0 @@ -#include "Grade.h" - -const size_t MAX_NAME_LENGTH = 30; -const size_t MAX_GRADES_COUNT = 20; -class Student -{ -public: - - Student(const char* name, size_t fn); - - void setFn(size_t fn); - void setName(const char* name); - - size_t getFN() const; - const char* getName() const; - - void addGrade(const char* taskName, size_t grade); - - - void print() const; -private: - char name[MAX_NAME_LENGTH + 1]; - size_t fn; - - Grade grades[MAX_GRADES_COUNT]; - size_t gradesCount; - - -}; \ No newline at end of file diff --git a/Sem. 09/task (not finished)/UniCourse.cpp b/Sem. 09/task (not finished)/UniCourse.cpp deleted file mode 100644 index c854b82..0000000 --- a/Sem. 09/task (not finished)/UniCourse.cpp +++ /dev/null @@ -1,135 +0,0 @@ -#include "UniCourse.h" - -UniCourse::UniCourse(const char* lecturerName) : UniCourse(lecturerName, "", "", "") -{} -UniCourse::UniCourse(const char* lecturerName, const char* assistant1Name) : UniCourse(lecturerName, assistant1Name, "", "") -{} -UniCourse::UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name) : UniCourse(lecturerName, assistant1Name, assistant2Name, "") -{} - -UniCourse::UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name, const char* assistant3Name) -{ - this->lecturerName = new char[strlen(lecturerName) + 1]; - strcpy(this->lecturerName, lecturerName); - - assistantsNames[0] = new char[strlen(assistant1Name) + 1]; - strcpy(assistantsNames[0], assistant1Name); - - assistantsNames[1] = new char[strlen(assistant2Name) + 1]; - strcpy(assistantsNames[1], assistant2Name); - - assistantsNames[2] = new char[strlen(assistant3Name) + 1]; - strcpy(assistantsNames[2], assistant3Name); - - capacity = 8; - size = 0; - - students = new Student*[capacity]; -} - - -void UniCourse::copyFrom(const UniCourse& other) -{ - lecturerName = new char[strlen(other.lecturerName) + 1]; - strcpy(lecturerName, other.lecturerName); - - for (int i = 0; i < 3; i++) - { - if (other.assistantsNames[i] != nullptr) - { - assistantsNames[i] = new char[strlen(other.assistantsNames[i]) + 1]; - strcpy(assistantsNames[i], other.assistantsNames[i]); - } - } - - - students = new Student*[other.capacity]; - - for (int i = 0; i < other.size; i++) - students[i] = new Student(*other.students[i]); - - capacity = other.capacity; - size = other.size; - -} -void UniCourse::free() -{ - delete[] lecturerName; - - for (int i = 0; i < 3; i++) - delete[] assistantsNames[i]; - - for (int i = 0; i < size; i++) - delete students[i]; - - delete[] students; -} - -UniCourse::UniCourse(const UniCourse& other) -{ - copyFrom(other); -} - -UniCourse& UniCourse::operator=(const UniCourse& other) -{ - if (this != &other) - { - free(); - copyFrom(other); - } - return *this; -} -UniCourse::~UniCourse() -{ - free(); -} -void UniCourse::addStudent(const char* name, size_t fn) -{ - if (size == capacity) - resize(); - - Student* current = new Student(name, fn); - students[size++] = current; -} - -void UniCourse::resize() -{ - Student** temp = new Student*[capacity *= 2]; - - for (int i = 0; i < size; i++) - temp[i] = students[i]; - - delete[] students; - - students = temp; -} - -size_t UniCourse::findStudentByFn(size_t fn) -{ - for (int i = 0; i < size; i++) - { - if (students[i]->getFN() == fn) - return i; - } - return -1; -} -void UniCourse::removeStudent(size_t fn) -{ - size_t indexOfStudent = findStudentByFn(fn); - if (fn == -1) - return; - - delete students[indexOfStudent]; - - for (int i = indexOfStudent; i < size - 1; i++) - students[i] = students[i + 1]; - - size--; - -} - -void UniCourse::print() -{ - for (int i = 0; i < size; i++) - students[i]->print(); -} \ No newline at end of file diff --git a/Sem. 09/task (not finished)/UniCourse.h b/Sem. 09/task (not finished)/UniCourse.h deleted file mode 100644 index fde1518..0000000 --- a/Sem. 09/task (not finished)/UniCourse.h +++ /dev/null @@ -1,35 +0,0 @@ -#include "Student.h" - -class UniCourse -{ - char* lecturerName; - char* assistantsNames[3]; - - Student** students; - size_t capacity; - size_t size; - - void copyFrom(const UniCourse& other); - void free(); - - size_t findStudentByFn(size_t fn); - - void resize(); - -public: - UniCourse(const char* lecturerName); - UniCourse(const char* lecturerName, const char* assistant1Name); - UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name); - UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name, const char* assistant3Name); - - - UniCourse(const UniCourse& other); - UniCourse& operator=(const UniCourse& other); - ~UniCourse(); - - void addStudent(const char* name, size_t fn); - void removeStudent(size_t fn); - - void print(); - -}; \ No newline at end of file diff --git a/Sem. 09/task (not finished)/main.cpp b/Sem. 09/task (not finished)/main.cpp deleted file mode 100644 index 6b727fa..0000000 --- a/Sem. 09/task (not finished)/main.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "UniCourse.h" - -int main() -{ - UniCourse u("ddz"); - - u.addStudent("ivan", 80123); - u.addStudent("angel", 80125); - u.addStudent("petur", 80126); - u.addStudent("ivan2", 81000); - //u.print(); - - u.removeStudent(80125); - u.print(); -} \ No newline at end of file diff --git a/Sem. 09/task1 (not finished)/Grade.cpp b/Sem. 09/task1 (not finished)/Grade.cpp new file mode 100644 index 0000000..4302166 --- /dev/null +++ b/Sem. 09/task1 (not finished)/Grade.cpp @@ -0,0 +1,69 @@ +#include "Grade.h" + + +Grade::Grade() : taskName(nullptr) +{ + setTaskName("Unknown"); + setGrade(2); +} + +Grade::Grade(const char* taskName, size_t gradeValue) : taskName(nullptr) +{ + setTaskName(taskName); + setGrade(gradeValue); +} + +void Grade::free() +{ + delete[] taskName; +} +void Grade::copyFrom(const Grade& other) +{ + gradeValue = other.gradeValue; + + taskName = new char[strlen(other.taskName) + 1]; + strcpy(taskName, other.taskName); +} + +Grade::Grade(const Grade& other) +{ + copyFrom(other); +} + +Grade& Grade::operator=(const Grade& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +Grade::~Grade() +{ + free(); +} + +const char* Grade::getTaskName() const +{ + return taskName; +} +size_t Grade::getGrade() const +{ + return gradeValue; +} + +void Grade::setGrade(size_t grade) +{ + if (grade < 2 || grade > 6) + throw std::exception("error!"); + + gradeValue = grade; +} + +void Grade::setTaskName(const char* name) +{ + delete[] taskName; + taskName = new char[strlen(name) + 1]; + strcpy(taskName, name); +} \ No newline at end of file diff --git a/Sem. 09/task1 (not finished)/Grade.h b/Sem. 09/task1 (not finished)/Grade.h new file mode 100644 index 0000000..d0cb2e7 --- /dev/null +++ b/Sem. 09/task1 (not finished)/Grade.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +class Grade +{ + size_t gradeValue; + char* taskName; + + void free(); + void copyFrom(const Grade& other); +public: + Grade(); + + Grade(const char* taskName, size_t gradeValue); + + Grade(const Grade& other); + Grade& operator=(const Grade& other); + ~Grade(); + + const char* getTaskName() const; + size_t getGrade() const; + + void setTaskName(const char* name); + void setGrade(size_t grade); +}; \ No newline at end of file diff --git a/Sem. 09/task1 (not finished)/Student.cpp b/Sem. 09/task1 (not finished)/Student.cpp new file mode 100644 index 0000000..99d26dc --- /dev/null +++ b/Sem. 09/task1 (not finished)/Student.cpp @@ -0,0 +1,45 @@ +#include "Student.h" + +Student::Student(const char* name, size_t fn) +{ + setName(name); + setFn(fn); +} + +void Student::setFn(size_t fn) +{ + if (fn < 80000 || fn > 83000) + throw std::exception("Invalid fn"); + + this->fn = fn; +} +void Student::setName(const char* name) +{ + if (strlen(name) > MAX_NAME_LENGTH) + throw std::exception("Name is too long!"); + + strcpy(this->name, name); +} + +size_t Student::getFN() const +{ + return fn; +} +const char* Student::getName() const +{ + return name; +} + +void Student::addGrade(const char* taskName, size_t grade) +{ + if (gradesCount == MAX_GRADES_COUNT) + throw std::exception("Error! Too many grades!"); + + Grade currentGrade(taskName, grade); + grades[gradesCount++] = currentGrade; +} + +void Student::print() const +{ + std::cout << name << " " << fn << std::endl; +} \ No newline at end of file diff --git a/Sem. 09/task1 (not finished)/Student.h b/Sem. 09/task1 (not finished)/Student.h new file mode 100644 index 0000000..5c09792 --- /dev/null +++ b/Sem. 09/task1 (not finished)/Student.h @@ -0,0 +1,29 @@ +#include "Grade.h" + +const size_t MAX_NAME_LENGTH = 30; +const size_t MAX_GRADES_COUNT = 20; +class Student +{ +public: + + Student(const char* name, size_t fn); + + void setFn(size_t fn); + void setName(const char* name); + + size_t getFN() const; + const char* getName() const; + + void addGrade(const char* taskName, size_t grade); + + + void print() const; +private: + char name[MAX_NAME_LENGTH + 1]; + size_t fn; + + Grade grades[MAX_GRADES_COUNT]; + size_t gradesCount; + + +}; \ No newline at end of file diff --git a/Sem. 09/task1 (not finished)/UniCourse.cpp b/Sem. 09/task1 (not finished)/UniCourse.cpp new file mode 100644 index 0000000..c854b82 --- /dev/null +++ b/Sem. 09/task1 (not finished)/UniCourse.cpp @@ -0,0 +1,135 @@ +#include "UniCourse.h" + +UniCourse::UniCourse(const char* lecturerName) : UniCourse(lecturerName, "", "", "") +{} +UniCourse::UniCourse(const char* lecturerName, const char* assistant1Name) : UniCourse(lecturerName, assistant1Name, "", "") +{} +UniCourse::UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name) : UniCourse(lecturerName, assistant1Name, assistant2Name, "") +{} + +UniCourse::UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name, const char* assistant3Name) +{ + this->lecturerName = new char[strlen(lecturerName) + 1]; + strcpy(this->lecturerName, lecturerName); + + assistantsNames[0] = new char[strlen(assistant1Name) + 1]; + strcpy(assistantsNames[0], assistant1Name); + + assistantsNames[1] = new char[strlen(assistant2Name) + 1]; + strcpy(assistantsNames[1], assistant2Name); + + assistantsNames[2] = new char[strlen(assistant3Name) + 1]; + strcpy(assistantsNames[2], assistant3Name); + + capacity = 8; + size = 0; + + students = new Student*[capacity]; +} + + +void UniCourse::copyFrom(const UniCourse& other) +{ + lecturerName = new char[strlen(other.lecturerName) + 1]; + strcpy(lecturerName, other.lecturerName); + + for (int i = 0; i < 3; i++) + { + if (other.assistantsNames[i] != nullptr) + { + assistantsNames[i] = new char[strlen(other.assistantsNames[i]) + 1]; + strcpy(assistantsNames[i], other.assistantsNames[i]); + } + } + + + students = new Student*[other.capacity]; + + for (int i = 0; i < other.size; i++) + students[i] = new Student(*other.students[i]); + + capacity = other.capacity; + size = other.size; + +} +void UniCourse::free() +{ + delete[] lecturerName; + + for (int i = 0; i < 3; i++) + delete[] assistantsNames[i]; + + for (int i = 0; i < size; i++) + delete students[i]; + + delete[] students; +} + +UniCourse::UniCourse(const UniCourse& other) +{ + copyFrom(other); +} + +UniCourse& UniCourse::operator=(const UniCourse& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +UniCourse::~UniCourse() +{ + free(); +} +void UniCourse::addStudent(const char* name, size_t fn) +{ + if (size == capacity) + resize(); + + Student* current = new Student(name, fn); + students[size++] = current; +} + +void UniCourse::resize() +{ + Student** temp = new Student*[capacity *= 2]; + + for (int i = 0; i < size; i++) + temp[i] = students[i]; + + delete[] students; + + students = temp; +} + +size_t UniCourse::findStudentByFn(size_t fn) +{ + for (int i = 0; i < size; i++) + { + if (students[i]->getFN() == fn) + return i; + } + return -1; +} +void UniCourse::removeStudent(size_t fn) +{ + size_t indexOfStudent = findStudentByFn(fn); + if (fn == -1) + return; + + delete students[indexOfStudent]; + + for (int i = indexOfStudent; i < size - 1; i++) + students[i] = students[i + 1]; + + size--; + +} + +void UniCourse::print() +{ + for (int i = 0; i < size; i++) + students[i]->print(); +} \ No newline at end of file diff --git a/Sem. 09/task1 (not finished)/UniCourse.h b/Sem. 09/task1 (not finished)/UniCourse.h new file mode 100644 index 0000000..fde1518 --- /dev/null +++ b/Sem. 09/task1 (not finished)/UniCourse.h @@ -0,0 +1,35 @@ +#include "Student.h" + +class UniCourse +{ + char* lecturerName; + char* assistantsNames[3]; + + Student** students; + size_t capacity; + size_t size; + + void copyFrom(const UniCourse& other); + void free(); + + size_t findStudentByFn(size_t fn); + + void resize(); + +public: + UniCourse(const char* lecturerName); + UniCourse(const char* lecturerName, const char* assistant1Name); + UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name); + UniCourse(const char* lecturerName, const char* assistant1Name, const char* assistant2Name, const char* assistant3Name); + + + UniCourse(const UniCourse& other); + UniCourse& operator=(const UniCourse& other); + ~UniCourse(); + + void addStudent(const char* name, size_t fn); + void removeStudent(size_t fn); + + void print(); + +}; \ No newline at end of file diff --git a/Sem. 09/task1 (not finished)/main.cpp b/Sem. 09/task1 (not finished)/main.cpp new file mode 100644 index 0000000..6b727fa --- /dev/null +++ b/Sem. 09/task1 (not finished)/main.cpp @@ -0,0 +1,15 @@ +#include "UniCourse.h" + +int main() +{ + UniCourse u("ddz"); + + u.addStudent("ivan", 80123); + u.addStudent("angel", 80125); + u.addStudent("petur", 80126); + u.addStudent("ivan2", 81000); + //u.print(); + + u.removeStudent(80125); + u.print(); +} \ No newline at end of file diff --git a/Sem. 09/task2/Queue.hpp b/Sem. 09/task2/Queue.hpp new file mode 100644 index 0000000..1cc14c3 --- /dev/null +++ b/Sem. 09/task2/Queue.hpp @@ -0,0 +1,91 @@ +#include + +template +class Queue +{ + T* data; + size_t capacity; + size_t count; + + size_t head; //where to get + size_t tail; //last added element; + + void resize(size_t newCapacity); +public: + Queue(); + + void enqueue(const T& obj); + T dequeue(); + + size_t getCount(); + size_t isEmpty(); +}; + +template +Queue::Queue() +{ + capacity = 4; + data = new T[capacity]; + + count = 0; + head = tail = 0; +} + + + +template +void Queue::enqueue(const T& obj) +{ + if (count == capacity) + resize(capacity * 2); + if (count != 0) + tail++; + + tail %= capacity; //so we don't get an out of range exception + + data[tail] = obj; + + count++; +} + +template +T Queue::dequeue() +{ + if (count == 0) + throw std::exception("The collection is empty!"); + + T toReturn = data[head]; + (++head) %= capacity; + + count--; + + return toReturn; +} + +template +size_t Queue::getCount() +{ + return count; +} + +template +size_t Queue::isEmpty() +{ + return getCount() == 0; +} + +template +void Queue::resize(size_t newCapacity) +{ + T* newData = new T[newCapacity]; + + for (int i = 0, ind = head; i < count; i++, (++ind) %= capacity) + newData[i] = data[ind]; + + delete[] data; + capacity = newCapacity; + data = newData; + + head = 0; + tail = count - 1; +} \ No newline at end of file diff --git a/Sem. 09/task2/main.cpp b/Sem. 09/task2/main.cpp new file mode 100644 index 0000000..e52579a --- /dev/null +++ b/Sem. 09/task2/main.cpp @@ -0,0 +1,23 @@ +#include +#include "Queue.hpp" +using namespace std; + + + +int main() +{ + Queue q; + + q.enqueue('A'); + q.enqueue('B'); + q.enqueue('C'); + q.enqueue('D'); + cout << q.dequeue() << endl; + q.enqueue('E'); + q.enqueue('F'); + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d