Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
du
#include <iostream> // zacatek CVector.h #ifndef __CVECTOR_H__ #define __CVECTOR_H__ using TType = double; using std::cout; using std::endl; enum class CError { EOk = 0, ESize, EMemory, ENotAllocated, EFull, EBadParam, EIntegrity, }; class CVector { size_t iSize; // size_t iCapacity; // inicializaci provadi default c'tor TType* iData; // const size_t iID; // promenna static nepatri objektu, ale sdili ho cela trida - patri vsem objektum tridy static size_t iTotalCounter; // toto jsou deklarace, ne definice (ty jsou v .cpp souboru) static size_t iLivingCounter; // public: // default c'tor CVector(): iSize(0), iCapacity(0), iData(nullptr), iID(iTotalCounter++) { cout << "default c'tor #" << iID << '\n'; ++iLivingCounter; } // copy c'tors //CVector(const CVector& aVec) = default; // melka kopie, umi sam prekladac a nesmi byt pouzito, pokud dana struktura/trida obsahuje pointer CVector(const CVector& aVec); // hluboka kopie CVector(size_t aSize, size_t aCapacity, TType* iData); // conv (conversion) c'tors CVector(size_t aSize); CVector(size_t aSize, double aVal); //CVector(const char* aStr); // move c'tor //CVector(CVector && aVec); inline TType& At(size_t aIndex) { CheckIndex(aIndex); if(!iData) throw CError::ENotAllocated; return iData[aIndex]; } //void SetAt(size_t aIndex, TType aVal); <-- nahrazuje metoda At() //void Alloc(size_t aSize); <-- nahrazuji konstruktory inline void Dealloc() // metoda volana v destruktoru { delete[] iData; iSize = iCapacity = 0; iData = nullptr; } void Add(const CVector& aVec1, const CVector& aVec2); void Add(const CVector& aVec, TType aVal = 0); // implicitni parametr se uvadi pouze v .h souboru void Dump(const char* aStr = nullptr) const; ~CVector() // d'tor, pokud existuje, je volan automaticky { Dealloc(); cout << "d'tor #" << iID << '\n'; --iLivingCounter; } size_t Size() const { return iSize; } // static metody, ktere jsou pristupne cele tride, ale vuci okoli jsou privatni static size_t Living() { return iLivingCounter; } static size_t Total() { return iTotalCounter; } private: // privatni metody - pristupne pouze metodam tridy void CheckIndex(size_t aIndex) const { if(iSize <= aIndex) throw CError::ESize; } bool Equal(const CVector& aVector) const; inline bool Compatible(const CVector& aVector) const { if(iData && aVector.iData && iSize == aVector.iSize) return true; return false; } // tato metoda my smysl predevsim pro ladeni... inline bool IntegrityCheck() const { if((!iData && iCapacity == 0 && iSize == 0) || (iData && iSize <= iCapacity)) return true; return false; } }; /* CVector */ #endif // !__CVECTOR_H__ // konec CVector.h // zacatek CVector.cpp #include <iostream> #include <sstream> //#include "CVector.h" size_t CVector::iTotalCounter = 0; // definice s inicializacemi static promennych size_t CVector::iLivingCounter = 0; // // copy c'tor #1 CVector::CVector(const CVector& aVec): iID(iTotalCounter++) { cout << "copy c'tor #" << iID << '\n'; iSize = aVec.iSize; iCapacity = aVec.iCapacity; //iData = aVec.iData; <-- POZOR, vznik dangling pointeru!!! iData = new TType[aVec.iCapacity]; for(size_t i = 0; i < iSize; ++i) iData[i] = aVec.iData[i]; ++iLivingCounter; } // copy c'tor #2 CVector::CVector(size_t aSize, size_t aCapacity, TType* aData): iID(iTotalCounter++) { cout << "copy c'tor #" << iID << '\n'; iSize = aSize; iCapacity = aCapacity; iData = new TType[aCapacity]; for(size_t i = 0; i < iSize; ++i) iData[i] = aData[i]; ++iLivingCounter; } // conv c'tor #1 // vytvori prazdny vektor o poctu prvku aSize, prvky inicializovany na 0 CVector::CVector(size_t aSize): iID(iTotalCounter++) { cout << "conv c'tor #" << iID << '\n'; iSize = aSize; iCapacity = aSize; iData = new TType[aSize]; for(size_t i = 0; i < iSize; ++i) iData[i] = 0; ++iLivingCounter; } /* // conv c'tor #2 (BPC-PPC-cv05, 1:32:00) --- PREDELAT CVector::CVector(size_t aSize, double aVal): iSize(1), iCapacity(1), iID(iTotalCounter++) { cout << "conv c'tor #" << iID << '\n'; iSize = aSize; iCapacity = aSize; iData = new TType[iSize]; for(size_t i = 0; i < iSize; ++i) iData[i] = TType (aVal); ++iLivingCounter; } */ /* // conv c'tor #3 // konverzni konstruktor z retezce - DODELAT CVector::CVector(const char* aStr): iID(iTotalCounter++) // v6("5;10 [1 2 3.4 5.6 45.67]") { // dodefinovat iSize, ICapacity, IData cout << "conv c'tor #" << iID << '\n'; std::istringstream input(aStr); size_t i1, i2; char c1, c2, c3; if(aStr) cout << aStr << ": "; else { cout << "{ "; for(size_t i = 0; i < iSize; ++i) cout << iData[i] << ", "; cout << "}\n"; } } */ // move c'tor /* CVector::CVector(CVector && aVec): iSize(aVec.iSize), iCapacity(aVec.iCapacity), iData(aVec.iData), iID(iTotalCounter++) { cout << "move c'tor #" << iID << '\n'; aVec.iSize = 0; aVec.iCapacity = 0; aVec.iData = nullptr; ++iLivingCounter; } */ /* void CVector::SetAt(size_t aIndex, TType aVal) { CheckIndex(aIndex); iData[aIndex] = aVal; } void CVector::Alloc(size_t aSize) { if(iData) throw CError::EFull; if(aSize == 0) { iCapacity = 0; iSize = 0; return; } iData = new TType[aSize]; if(!iData) throw CError::EMemory; iSize = aSize; iCapacity = aSize; for(size_t i = 0; i < iCapacity; ++i) iData[i] = 0; } */ void CVector::Dump(const char* aStr) const // prints the vector { if(aStr) cout << aStr << ": "; if(!IntegrityCheck()) { cout << "#" << iID << " " << "Bad integrity check!" << '\n'; return; } if(!iData) throw CError::ENotAllocated; else { cout << "{ "; for(size_t i = 0; i < iSize; ++i) cout << iData[i] << ", "; cout << "}\n"; } } void CVector::Add(const CVector& aVec1, const CVector& aVec2) { // Input vectors are empty if(!aVec1.iData || !aVec2.iData || aVec1.iSize == 0 || aVec2.iSize == 0) throw CError::ENotAllocated; // Input vectors are compatible if(!aVec1.Compatible(aVec2)) throw CError::EBadParam; // Result vector is not allocated if(!iData) { iData = new TType[aVec1.iSize]; if(!iData) throw CError::EMemory; iCapacity = aVec1.iCapacity; iSize = aVec1.iSize; } // Result vector has different size than input vectors or is unallocated if(!Compatible(aVec1) || !Compatible(aVec2)) { delete[] iData; iData = new TType[aVec1.iSize]; if(!iData) throw CError::EMemory; iCapacity = aVec1.iCapacity; iSize = aVec1.iSize; } for(size_t i = 0; i < iSize; ++i) iData[i] = aVec1.iData[i] + aVec2.iData[i]; } void CVector::Add(const CVector& aVec, TType aVal) { if(aVec.iSize != iSize) throw CError::EBadParam; for(size_t i = 0; i < aVec.iSize; ++i) iData[i] = aVec.iData[i] + aVal; } bool CVector::Equal(const CVector& aVec) const { if(Compatible(aVec)) for(size_t i = 0; i < aVec.iSize; i++) if(this->iData[i] != aVec.iData[i]) return false; return true; } // konec CVector.cpp // zacatek main.cpp #include <iostream> //#include "CVector.h" //#include "C:\GitHub\BPC-PPC\checker\check.h" #define TOSTR(a) (#a) const char* ErrorStr[] = { TOSTR(EOk), TOSTR(ESize), TOSTR(EMemory), TOSTR(ENotAllocated), TOSTR(EFull), TOSTR(EBadParam), TOSTR(EIntegrity) }; int main(int argc, char** argv) { { try { // using of constructors: CVector v(10), v1(10), v2(v1.Size()); // conversion c'tors CVector v3(v1); // copy c'tor CVector v4; // default c'tor // konverzni konstruktor z double, alokuje misto pro jeden prvek a s danou hodnotou. CVector v5(5.1); // konstruktor ze tri prvku: iSize, iCapacity, hodnoty v poli double InitPole[6] = { 1, 2, 3.4, 5.6, 45.67, 0 }; CVector v6(6,20,InitPole); // konverzni c’tor z retezce – format jako u metody Dump(). //CVector v7("5;10 [1 2 3.4 5.6 45.67]"); v5.Dump("v5"); v6.Dump("v6"); //v7.Dump("v7"); v.At(2) = 5.6; // metoda At() pouzita k vlozeni hodnoty 5.0 na 3. pozici vektoru 'v' // metody At() pouzita k ziskani hodnoty z 10. pozice vektoru 'v' cout << "The value obtained with the At method: " << v.At(2) << '\n' << '\n'; for(size_t i = 0; i < 10; ++i) { v1.At(i) = TType(i); v2.At(i) = TType(10) + i; } v1.Dump("v1"); v2.Dump("v2"); v3.Dump("v3"); cout << '\n'; v3.Add(v1, v2); // v3 = v1 + v2 v.Add(v1, 3); // v = v1 + 3 v4.Add(v2, v3); // v4 je pred pouzitim metody prazdny, metoda Add ho prizpusobi vektorum v2 a v3 v.Dump("v"); v1.Dump("v1"); v2.Dump("v2"); v3.Dump("v3"); v4.Dump("v4"); cout << '\n'; } catch(enum class CError& exception) { // static_cast<type>(prom) je konverze z typu promenne 'prom' na typ 'type' -> index typu size_t pro pole ErrorStr[] cout << "Exception thrown: " << ErrorStr[static_cast<size_t>(exception)] << '\n'; } catch(...) { cout << "General exception!\n"; } } cout << "V programu je nyni zivych: " << CVector::Living() << " instanci tridy CVector\n"; cout << "V programu bylo celkem: " << CVector::Total() << " instanci tridy CVector\n"; return(0); } // konec main.cpp
run
|
edit
|
history
|
help
0
hgh
bad_cast
solution_problem4
#21
Two-phase sample with VC++ 2015
hangman
hangman
1
Rounding in C++
Dices by GOOSE