#include "intVektor.h" #include #include #include #include "exceptions.h" //Public intVektor::intVektor() { try { head = createNewNodes(0); } catch(outOfMemory &e) { throw; } sIze = 0; } intVektor::intVektor(int startSize) { assert(startSize > 0); try { head = createNewNodes(startSize); } catch(outOfMemory &e) { throw; } sIze = startSize; } intVektor::intVektor(const intVektor& rh) { sIze = rh.sIze; try { head = createNewNodes(sIze); } catch(outOfMemory &e) { throw; } intNode * currentPos = head; intNode * currentRh = rh.head; while(currentPos) { currentPos->data = currentRh->data; currentPos = currentPos->next; currentRh = currentRh->next; } } intVektor::~intVektor() { while(sIze > 0) removeLast(); } intVektor& intVektor::operator=(const intVektor& rh) { if(this != &rh) { if(sIze < rh.sIze) { try { extendArray(rh.sIze - sIze); } catch(outOfMemory &e) { throw; } } else if(sIze > rh.sIze) while(sIze > rh.sIze) removeLast(); intNode * currentPos = head; intNode * currentRh = rh.head; for(int i = 0; currentPos != 0; i++) { currentPos->data = currentRh->data; currentPos = currentPos->next; currentRh = currentRh->next; } } return *this; } int& intVektor::operator[](const int position) const { assert(0 <= position && position < sIze); intNode * currentPos = head; for(int i = 0; i < position; i++) currentPos = currentPos->next; return currentPos->data; } const intVektor intVektor::operator+(const intVektor& rh) { assert(sIze == rh.sIze); intNode * currentPos = head; intNode * currentRh = rh.head; intVektor temp(rh.sIze); for(int i = 0; currentPos != 0; i++) { temp[i] = currentPos->data + currentRh->data; currentPos = currentPos->next; currentRh = currentRh->next; } return temp; } int intVektor::size() { return sIze; } //Private intVektor::intNode* intVektor::createNewNodes(int numberOfNodes) { intNode * first = 0; intNode * here; for(int i = 0; i < numberOfNodes; i++) { intNode * newNode; try { newNode = new intNode(); } catch(std::bad_alloc& e) { intNode* hairAirJagNext = first; while(hairAirJagNext != 0) { intNode* hairAirJagNu = hairAirJagNext; hairAirJagNext = hairAirJagNext->next; delete hairAirJagNu; } throw outOfMemory(); } newNode->data = 0; if(i == 0) { first = newNode; here = first; here->next = 0; } else { here->next = newNode; here = newNode; here->next = 0; } } return first; } void intVektor::extendArray(int numberOfNodes) { intNode * newChain; try { newChain = createNewNodes(numberOfNodes); } catch(outOfMemory& e) { throw; } intNode * currentPos = head; if(head == 0) head = newChain; else { while(currentPos->next != 0) currentPos = currentPos->next; currentPos->next = newChain; } sIze += numberOfNodes; } void intVektor::removeLast() { intNode * CurrentPos = head; intNode * nearlyLast; if(sIze != 1) while(CurrentPos != 0) { if(CurrentPos->next != 0) nearlyLast = CurrentPos; CurrentPos = CurrentPos->next; } else nearlyLast = head; delete CurrentPos; nearlyLast->next = 0; sIze--; } //Other operators std::ostream& operator<<(std::ostream& os, intVektor rh) { os << "("; for(int i = 0; i < rh.size(); i++) os << "[" << rh[i] << "]"; os << ")"; return os; } std::istream& operator>>(std::istream& is, intVektor& rh) { is.exceptions(std::istream::failbit | std::istream::badbit); std::string workString = ","; std::string inString; int pos = -1; int length = 0; int i = 0; try { is >> inString; } catch(std::ios_base::failure& e) { is.clear(); throw streamError(); } workString.append(inString); while(((pos = workString.find( ",", pos + 1)) != workString.npos) && (i < rh.size())) { if((length = workString.find( ",", pos + 1)) == workString.npos) length = workString.size(); std::string stringTemp = workString.substr(pos + 1,length); int intTemp = atoi(stringTemp.data()); rh[i] = intTemp; i++; } return is; }