diff --git a/feladat5/UnitTest1/UnitTest1.vcxproj b/feladat5/UnitTest1/UnitTest1.vcxproj
index 8e72091..96c458e 100644
--- a/feladat5/UnitTest1/UnitTest1.vcxproj
+++ b/feladat5/UnitTest1/UnitTest1.vcxproj
@@ -77,6 +77,7 @@
true
+ C:\Users\danit\Desktop\2026-27-cpp-homework\feladat5\feladat5;$(IncludePath)
true
@@ -142,7 +143,8 @@
Windows
- $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)
+ C:\Users\danit\Desktop\2026-27-cpp-homework\feladat5\feladat5\x64\Debug;$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)
+ CMyString;%(AdditionalDependencies)
@@ -179,6 +181,11 @@
+
+
+ {30d9e2e3-47ad-44f5-b8ba-07a26ba06d5e}
+
+
diff --git a/feladat5/feladat5.slnx b/feladat5/feladat5.slnx
index df0acdf..1864a84 100644
--- a/feladat5/feladat5.slnx
+++ b/feladat5/feladat5.slnx
@@ -3,5 +3,6 @@
-
+
+
diff --git a/feladat5/feladat5/CMyString.cpp b/feladat5/feladat5/CMyString.cpp
index 57d372d..6de9bf9 100644
--- a/feladat5/feladat5/CMyString.cpp
+++ b/feladat5/feladat5/CMyString.cpp
@@ -1,6 +1,32 @@
// Tóth Dániel Mátyás | IFPQYY | v1
#include "CMyString.h"
#include "CMyStringException.h"
+#include
+
+static int strLength(const char* str) {
+ if (str == nullptr)
+ {
+ return 0;
+ }
+ int cnt = 0;
+ while (str[cnt])
+ {
+ cnt++;
+ }
+ return cnt;
+}
+
+static void CopyStr(char* dest, const char* src, size_t length) {
+ if (dest == nullptr || src == nullptr) {
+ throw new bad_exception;
+ }
+ size_t i;
+ for (i = 0; i < length; i++)
+ {
+ dest[i] = src[i];
+ }
+ dest[i] = '\0';
+}
CMyString::CMyString() {
m_nDataLength = 0;
@@ -23,20 +49,22 @@ CMyString::CMyString(const char* psz) {
m_nAllocLength = m_nDataLength + 1;
}
m_pchData = new char[m_nAllocLength];
- int i = 0;
- while (i < m_nAllocLength)
- {
- m_pchData[i] = psz[i];
- i++;
- }
- m_pchData[i] = '\0';
+ CopyStr(m_pchData, psz, m_nDataLength);
}
-CMyString::CMyString(char ch, size_t repeat = 1) {
- m_nDataLength = repeat;
- m_nAllocLength = repeat + 1;
+CMyString::CMyString(char ch, size_t repeat) {
+ if (ch == '\0')
+ {
+ m_nDataLength = 0;
+ m_nAllocLength = 1;
+ }
+ else
+ {
+ m_nDataLength = repeat;
+ m_nAllocLength = repeat + 1;
+ }
m_pchData = new char[m_nAllocLength];
int i = 0;
- while (i < repeat)
+ while (i < m_nDataLength)
{
m_pchData[i] = ch;
i++;
@@ -56,55 +84,153 @@ CMyString::CMyString(const CMyString& str) {
m_pchData = new char[m_nAllocLength];
int i = 0;
const char* cont = str.c_str();
- while (cont[i])
+ while (cont[i]) // will never be a nullptr
{
m_pchData[i] = cont[i];
i++;
}
- m_pchData[i] = '\0';
+ m_pchData[i] = '\0'; //i will never go over nAllocLength
}
CMyString::~CMyString() {
delete m_pchData;
}
size_t CMyString::size() const {
-
+ return m_nDataLength;
}
size_t CMyString::capacity() const {
-
+ return m_nDataLength;
}
const char* CMyString::c_str() const {
-
+ return m_pchData;
}
void CMyString::clear() {
-
+ m_nDataLength = 0;
+ m_pchData[0] = '\0';
}
char CMyString::getat(size_t index) const {
-
+ if (index >= m_nDataLength)
+ {
+ throw new CMyStringException(CMyStringException::ErrOutOfRange);
+ }
+ return m_pchData[index];
}
void CMyString::setat(size_t index, char ch) {
-
+ if (index >= m_nDataLength)
+ {
+ throw new CMyStringException(CMyStringException::ErrOutOfRange);
+ }
+ if (ch == '\0')
+ {
+ throw new CMyStringException(CMyStringException::ErrInvalidChar);
+ }
+ m_pchData[index] = ch;
}
void CMyString::reserve(size_t newcapacity) {
-
+ if (newcapacity < m_nAllocLength)
+ {
+ throw new CMyStringException(CMyStringException::ErrUnexpected);
+ }
+ char* tmp = m_pchData;
+ m_pchData = new char[newcapacity];
+ CopyStr(m_pchData, tmp, m_nDataLength);
+ delete tmp;
}
void CMyString::erase(size_t pos, size_t count) {
-
+ if (pos >= m_nDataLength)
+ {
+ throw new CMyStringException(CMyStringException::ErrOutOfRange);
+ }
+ if (pos + count > m_nDataLength)
+ {
+ count = m_nDataLength - pos;
+ }
+ for (size_t i = 0; i < count; i++)
+ {
+ m_pchData[pos + i] = '\0';
+ }
}
bool CMyString::empty() const {
return m_nDataLength == 0;
}
void CMyString::display() const {
-
+ if (!empty())
+ {
+ cout << m_pchData;
+ }
}
void CMyString::shrink_to_fill() {
-
+ m_nAllocLength = m_nDataLength + 1;
+ char* tmp = m_pchData;
+ m_pchData = new char[m_nAllocLength];
+ CopyStr(m_pchData, tmp, m_nDataLength);
+ delete tmp;
}
void CMyString::reverse() {
-
+ if (!empty())
+ {
+ for (size_t i = 0; i < m_nDataLength / 2; i++)
+ {
+ int j = m_nDataLength - i - 1;
+ char tmp = m_pchData[i];
+ m_pchData[i] = m_pchData[j];
+ m_pchData[j] = tmp;
+ }
+ }
}
-void CMyString::append(const char* psz, unsigned offset = 0, unsigned count = -0) {
-
+void CMyString::append(const char* psz, unsigned offset, unsigned count) {
+ if (psz == nullptr || psz == m_pchData)
+ {
+ return;
+ }
+ int len = strLength(psz);
+ if (count == ~0)
+ {
+ count = len;
+ }
+ if (len < (count - offset))
+ {
+ throw new CMyStringException(CMyStringException::ErrOutOfRange);
+ }
+ if (count + m_nDataLength > m_nAllocLength)
+ {
+ char* tmp = m_pchData;
+ m_nAllocLength = (count + m_nDataLength) * 2;
+ m_pchData = new char[m_nAllocLength];
+ CopyStr(m_pchData, tmp, m_nDataLength);
+ delete tmp;
+ }
+ int i = m_nDataLength;
+ int j = offset;
+ while (j < count)
+ {
+ m_pchData[i++] = psz[j++];
+ }
+ m_nDataLength = i;
+ m_pchData[i] = '\0';
}
CMyString& CMyString::operator=(const CMyString& str) {
-
+ if (m_pchData != str.c_str())
+ {
+ if (!str.empty())
+ {
+ if (str.capacity() > m_nAllocLength)
+ {
+ m_nAllocLength = str.capacity();
+ char* cpy = m_pchData;
+ m_pchData = new char[m_nAllocLength];
+ delete cpy;
+ }
+ CopyStr(m_pchData, str.c_str(), m_nDataLength);
+ m_nDataLength = str.size();
+ }
+ else
+ {
+ m_nDataLength = 0;
+ m_nAllocLength = 1;
+ delete m_pchData;
+ m_pchData = new char[m_nAllocLength];
+ m_pchData[0] = '\0';
+ }
+ }
+ return *this;
}
\ No newline at end of file
diff --git a/feladat5/feladat5/CMyString.h b/feladat5/feladat5/CMyString.h
index e080569..5b98f3d 100644
--- a/feladat5/feladat5/CMyString.h
+++ b/feladat5/feladat5/CMyString.h
@@ -19,7 +19,7 @@ public:
void display() const;
void shrink_to_fill();
void reverse();
- void append(const char* psz, unsigned offset = 0, unsigned count = -0);
+ void append(const char* psz, unsigned offset = 0, unsigned count = ~0);
CMyString& operator=(const CMyString& str);
private:
char* m_pchData;
diff --git a/feladat5/feladat5/CMyStringException.cpp b/feladat5/feladat5/CMyStringException.cpp
index 90cf5db..51fe31b 100644
--- a/feladat5/feladat5/CMyStringException.cpp
+++ b/feladat5/feladat5/CMyStringException.cpp
@@ -1,2 +1,36 @@
// Tóth Dániel Mátyás | IFPQYY | v1
#include "CMyStringException.h"
+
+static int strLength(const char* str) {
+ if (str == nullptr)
+ {
+ return 0;
+ }
+ int cnt = 0;
+ while (str[cnt])
+ {
+ cnt++;
+ }
+ return cnt;
+}
+
+static void CopyStr(char* dest, const char* src, size_t length) {
+ if (dest == nullptr || src == nullptr) {
+ return;
+ }
+ size_t i;
+ for (i = 0; i < length; i++)
+ {
+ dest[i] = src[i];
+ }
+ dest[i] = '\0';
+}
+
+CMyStringException::CMyStringException(const char* input) {
+ int len = strLength(input);
+ msg = unique_ptr(new char[len+1]);
+ CopyStr(msg.get(), input, len);
+}
+const char* CMyStringException::what() {
+ return msg.get();
+}
\ No newline at end of file
diff --git a/feladat5/feladat5/CMyStringException.h b/feladat5/feladat5/CMyStringException.h
index d07b91e..166c391 100644
--- a/feladat5/feladat5/CMyStringException.h
+++ b/feladat5/feladat5/CMyStringException.h
@@ -1,9 +1,15 @@
// Tóth Dániel Mátyás | IFPQYY | v1
#pragma once
+#include
+using namespace std;
class CMyStringException {
public:
- CMyStringException(char* input);
+ CMyStringException(const char* input);
const char* what(void);
+ static constexpr const char* ErrOutOfRange = "Hiba: index hiba!";
+ static constexpr const char* ErrInvalidChar = "Hiba: érvénytelen karakter!";
+ static constexpr const char* ErrCount = "Hiba: karakterek száma 0!";
+ static constexpr const char* ErrUnexpected = "Hiba: Nem várt hiba!";
private:
- char* msg;
+ unique_ptr msg;
};
\ No newline at end of file
diff --git a/feladat5/feladat5/feladat5.vcxproj b/feladat5/feladat5/feladat5.vcxproj
index edc7e49..20049a7 100644
--- a/feladat5/feladat5/feladat5.vcxproj
+++ b/feladat5/feladat5/feladat5.vcxproj
@@ -17,7 +17,6 @@
Release
x64
-
18.0
@@ -53,27 +52,24 @@
true
Unicode
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Level3
@@ -84,10 +80,10 @@
Console
- true
+ true
- true
+ true
@@ -102,10 +98,10 @@
Console
- true
+ true
- true
+ true
@@ -118,10 +114,10 @@
Console
- true
+ true
- true
+ true
@@ -136,15 +132,22 @@
Console
- true
+ true
- true
+ true
-
-
+
+
+
+
+
+
+
+
+
-
+
\ No newline at end of file
diff --git a/feladat5/feladat5/feladat5.vcxproj.filters b/feladat5/feladat5/feladat5.vcxproj.filters
index a8a6563..7f21630 100644
--- a/feladat5/feladat5/feladat5.vcxproj.filters
+++ b/feladat5/feladat5/feladat5.vcxproj.filters
@@ -14,4 +14,23 @@
rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
+
+
+ Source Files
+
+
+ Source Files
+
+
+ Source Files
+
+
+
+
+ Header Files
+
+
+ Header Files
+
+
\ No newline at end of file