From 0218c678e13e1cb94a352c91733cea68467dc566 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Sun, 14 Feb 2021 23:06:35 +0200 Subject: start from scratch --- Classes/Distance/Distance.cpp | 105 ------------- Classes/Distance/Distance.h | 31 ---- Classes/GradesBook(DynamicArray)/GradesBook.cpp | 91 ----------- Classes/GradesBook(DynamicArray)/GradesBook.h | 24 --- Classes/String/String.cpp | 194 ------------------------ Classes/String/String.h | 45 ------ 6 files changed, 490 deletions(-) delete mode 100644 Classes/Distance/Distance.cpp delete mode 100644 Classes/Distance/Distance.h delete mode 100644 Classes/GradesBook(DynamicArray)/GradesBook.cpp delete mode 100644 Classes/GradesBook(DynamicArray)/GradesBook.h delete mode 100644 Classes/String/String.cpp delete mode 100644 Classes/String/String.h (limited to 'Classes') diff --git a/Classes/Distance/Distance.cpp b/Classes/Distance/Distance.cpp deleted file mode 100644 index aea74a7..0000000 --- a/Classes/Distance/Distance.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "Distance.h" - -Distance::Distance() -{ - setCm(minCM); - setM(minM); - setKm(minKm); - -} -Distance::Distance(int km, int m, int cm) -{ - setCm(cm); - setM(m); - setKm(km); -} -//constructs the distance by only given cm. -Distance::Distance(int cm) -{ - int km = cm / 100000; - int rest = cm % 100000; - int m = rest / 100; - rest = rest % 100; - - setKm(km); - setM(m); - setCm(rest); - - -} -void Distance::setCm(int cm) -{ - if (cm < minCM || cm > maxCM) - cm = minCM; - this->cm = cm; -} -void Distance::setM(int m) -{ - if (m < minM || m > maxM) - m = minM; - this->m = m; -} -void Distance::setKm(int km) -{ - if (km < minKm) - km = 0; - this->km = km; -} -int Distance::getCm()const -{ - return cm; -} -int Distance::getM() const -{ - return m; -} -int Distance::getKm() const -{ - return km; -} -void Distance::Print() const -{ - std::cout << "KM: " << km << " M: " << m << " CM: " << cm << std::endl; -} -int Compare(const Distance& d1, const Distance& d2) -{ - if (d1.getKm() < d2.getKm()) - return -1; - else if (d1.getKm() > d2.getKm()) - return 1; - else - { - if (d1.getM() < d2.getM()) - return -1; - else if (d1.getM() > d2.getM()) - return 1; - else - { - if (d1.getCm() < d2.getCm()) - return -1; - else if (d1.getCm() > d2.getCm()) - return 1; - else - return 0; - } - } -} -// if its between 66km and 99,5 km -bool Distance::isGreedy() const -{ - Distance lowLimit(66, 0, 0); - Distance upLimit(99, 500, 0); - return Compare(*this, lowLimit) > 0 && Compare(*this, upLimit) < 0; -} -int Distance::convertToCm() const -{ - return cm + m * 100 + km * 100000; -} -Distance getDiff(const Distance& d1, const Distance& d2) -{ - int cm1 = d1.convertToCm(); - int cm2 = d2.convertToCm(); - int diff = abs(cm1 - cm2); - Distance result(diff); - return result; -} diff --git a/Classes/Distance/Distance.h b/Classes/Distance/Distance.h deleted file mode 100644 index dfa47d0..0000000 --- a/Classes/Distance/Distance.h +++ /dev/null @@ -1,31 +0,0 @@ -#include - -const int maxCM = 99; -const int minCM = 0; -const int maxM = 999; -const int minM = 0; -const int minKm = 0; - -class Distance -{ - int cm; - int m; - int km; - -public: - Distance(); - Distance(int km, int m, int cm); - Distance(int cm); - - int getCm() const; - int getM() const; - int getKm() const; - - void setCm(int cm); - void setM(int m); - void setKm(int km); - - void Print() const; - bool isGreedy() const; - int convertToCm() const; -}; diff --git a/Classes/GradesBook(DynamicArray)/GradesBook.cpp b/Classes/GradesBook(DynamicArray)/GradesBook.cpp deleted file mode 100644 index 0c04b83..0000000 --- a/Classes/GradesBook(DynamicArray)/GradesBook.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "GradesBook.h" - - -GradesBook::GradesBook() -{ - capacity = 4; - grades = new int[capacity]; - size = 0; -} -GradesBook::GradesBook(const GradesBook& other) -{ - cout << "Copy contr of GradesBook" << endl; - CopyFrom(other); -} -GradesBook& GradesBook::operator=(const GradesBook& other) -{ - if (this != &other) - { - Free(); - CopyFrom(other); - } - return *this; -} -GradesBook::~GradesBook() -{ - Free(); -} - - -void GradesBook::CopyFrom(const GradesBook& other) -{ - grades = new int[other.capacity]; - for (int i = 0; i < other.size; i++) - grades[i] = other.grades[i]; - capacity = other.capacity; - size = other.size; -} -void GradesBook::Free() -{ - delete[] grades; -} - - -bool GradesBook::isFull() -{ - return size == capacity; -} -void GradesBook::AddGrade(int grade) -{ - if (!isValidGrade(grade)) - return; - if (isFull()) - Resize(); - grades[size++] = grade; - -} -void GradesBook::Resize() -{ - int* temp = new int[capacity * 2]; - for (int i = 0; i < size; i++) - temp[i] = grades[i]; - capacity *= 2; - delete[] grades; - grades = temp; -} -void GradesBook::RemoveLastGrade() -{ - size--; -} -bool GradesBook::isValidGrade(int grade) -{ - return (grade >= 2 && grade <= 6); -} -int GradesBook::operator[](int index) const -{ - if (index < 0 || index >= size) - return -1; - return grades[index]; -} - -ostream& operator<<(ostream& stream, GradesBook& obj) -{ - for (int i = 0; i < obj.capacity; i++) - { - if (i < obj.size) - stream << obj.grades[i] << ' '; - else - stream << '_' << ' '; - } - return stream; -} \ No newline at end of file diff --git a/Classes/GradesBook(DynamicArray)/GradesBook.h b/Classes/GradesBook(DynamicArray)/GradesBook.h deleted file mode 100644 index c9ba8e9..0000000 --- a/Classes/GradesBook(DynamicArray)/GradesBook.h +++ /dev/null @@ -1,24 +0,0 @@ -class GradesBook -{ - int* grades; - int size; - int capacity; - - bool isValidGrade(int grade); - void Resize(); - void CopyFrom(const GradesBook& other); - void Free(); -public: - GradesBook(); - GradesBook(const GradesBook& other); - GradesBook& operator=(const GradesBook& other); - ~GradesBook(); - - bool isFull(); - void AddGrade(int grade); - void RemoveLastGrade(); - - int operator[](int index) const; - friend ostream& operator<<(ostream& stream, GradesBook& obj); - -}; diff --git a/Classes/String/String.cpp b/Classes/String/String.cpp deleted file mode 100644 index f4f567b..0000000 --- a/Classes/String/String.cpp +++ /dev/null @@ -1,194 +0,0 @@ -#include "String.h" - -void String::CopyFrom(const String& other) { - - str = new char[strlen(other.str) + 1]; - strcpy(str, other.str); - - len = other.len; -} - -void String::Free() { - delete[] str; -} - -String::String() { - - str = new char[1]; - str[0] = '\0'; - len = 0; -} - -inline String::String(char ch) -{ - str = new char[2]; - str[0] = ch; - str[1] = '\0'; - len = 1; -} - -String::String(const char* str) { - - len = strlen(str); - - this->str = new char[len + 1]; - strcpy(this->str, str); -} - -String::String(const String& other) { - CopyFrom(other); -} - -String& String::operator=(const String& other) { - - if (this != &other) { - Free(); - CopyFrom(other); - } - return *this; -} - -String::~String() { - Free(); -} - -int String::getLenght() const { - return len; -} - -String String::SubString(int start) { - - if (start < 0 || start > len) - throw std::logic_error("Invalid index.\n"); - - char* newStr = new char[len - start + 2]; - int j = 0; - for (int i = start; i < len; i++) - newStr[j++] = str[i]; - newStr[j] = '\0'; - - String newString(newStr); - delete[] newStr; - - return newString; -} - -String String::SubString(int start, int end) { - - if (start < 0 || start > len || end <= 0 || end > len) - throw std::logic_error("Invalid index.\n"); - - char* newStr = new char[end - start + 2]; - int j = 0; - for (int i = start; i <= end; i++) - newStr[j++] = str[i]; - newStr[j] = '\0'; - - String newString(newStr); - delete[] newStr; - - return newString; -} - -int String::IndexOf(const String& other, int index) { - - if (index < 0) - throw std::logic_error("Invalid index.\n"); - - int iter = 0; - for (int i = index; i <= len; i++) { - - if (iter == other.len) - return i - other.len; - if (str[i] != other.str[iter]) - iter = 0; - else - iter++; - } - return -1; -} - -int String::IndexOf(const char* str, int index) { - - String searched(str); - return IndexOf(searched, index); -} - -int String::CountOcurences(String str) -{ - int count = 0; - int i = IndexOf(str, 0); - while (i != -1) - { - count++; - i = IndexOf(str, i + 1); - } - return count; -} - -inline int String::GetOcurence(String str, int num) -{ - int count = 0; - int i = IndexOf(str, 0); - while (i != -1) - { - count++; - if (count == num) - return i; - i = IndexOf(str, i + 1); - } - return count; -} - -const char& String::operator[](int index) const { - - if (index >= len) - throw std::out_of_range("Out of range!"); - return str[index]; -} - -char& String::operator[](int index) { - return str[index]; -} - -String& String::operator+=(const String& other) { - - char* temp = str; - str = new char[len + other.len + 1]; - strcpy(str, temp); - strcat(str, other.str); - len += other.len; - delete[] temp; - return *this; -} - -bool operator==(const String& lhs, const String& rhs) { - return !strcmp(lhs.str, rhs.str); -} - -bool operator!=(const String& lhs, const String& rhs) { - return strcmp(lhs.str, rhs.str); -} - -std::ostream& operator<<(std::ostream& os, const String& obj) { - return os << obj.str; -} - -std::istream& operator>>(std::istream& is, String& obj) { - - char buff[MAX_SIZE]; - is.getline(buff, MAX_SIZE); - obj.str = new char[strlen(buff) + 1]; - strcpy(obj.str, buff); - - return is; -} - - -const String operator+(const String& lhs, const String& rhs) { - if (lhs.getLenght() == 0) - return rhs; - else if (rhs.getLenght() == 0) - return lhs; - return String(lhs) += rhs; -} diff --git a/Classes/String/String.h b/Classes/String/String.h deleted file mode 100644 index 37bbc6d..0000000 --- a/Classes/String/String.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef STRING_HDR -#define STRING_HDR - -#include -#include - -const int MAX_SIZE = 1024; - -class String { - -private: - char* str; - int len; - - void CopyFrom(const String& other); - void Free(); - -public: - String(); - String(char ch); - String(const char* str); - String(const String& other); - String& operator=(const String& other); - ~String(); - - int getLenght() const; - String SubString(int start); - String SubString(int start, int end); - int IndexOf(const String& other, int index); //return the index of the first encounter of other in the object - int IndexOf(const char* str, int index); //reusing IndexOf(const String& other, int index) - int CountOcurences(String str); // How many times a string is found as substring - int GetOcurence(String str, int i); //Get if the i-th substring (count start from 1) - - const char& operator[](int index) const; - char& operator[](int index); - String& operator+=(const String& other); //return the concatenation of the object with other - friend bool operator==(const String& lhs, const String& rhs); //return true if two strings are equivalent - friend bool operator!=(const String& lhs, const String& rhs); //return true if two strings are not equivalent - friend std::ostream& operator<<(std::ostream& os, const String& obj); - friend std::istream& operator>>(std::istream& is, String& obj); - - String* Split(String str); -}; -const String operator+(const String& lhs, const String& rhs); -#endif // !STRING_HDR \ No newline at end of file -- cgit v1.3.1-23-gd82d