00001
00002
00003
00004
00005
00006
00007
00008 #include "Vector.h"
00009
00010 #include <string>
00011 #include <iostream>
00012
00013 Vector::Vector(unsigned int rozmiar)
00014 {
00015 content=new int[rozmiar];
00016 size=rozmiar;
00017 }
00018 std::string Vector::toString() const
00019 {
00020 std::string wyjscie="";
00021 if(this->size>0)
00022 {
00023 wyjscie= "[";
00024 for (unsigned int i = 0; i < this->size; ++i)
00025 {
00026 if(i!=0)
00027 wyjscie += ',';
00028 wyjscie += IntToStr(this->content[i]);
00029 }
00030 wyjscie += ']';
00031 }
00032 else
00033 return "";
00034 return wyjscie;
00035 }
00036 Vector::~Vector()
00037 {
00038
00039 DEBUG("Wywołano destruktor wektora:",this->toString())
00040 delete [] content;
00041 }
00042 Vector::Vector(const std::string& raw)
00043 {
00044 content=new int[0];
00045 size=0;
00046 this->fromText(raw);
00047 }
00048 Vector::Vector(const Vector& Wzorzec)
00049 {
00050 DEBUG("Wywołano konstruktor kopiujący dla wektora:",Wzorzec.toString())
00051 if(Wzorzec.size>0)
00052 {
00053 content=new int[Wzorzec.size];
00054 size=Wzorzec.size;
00055 for (unsigned int i = 0; i < size; ++i) {
00056 content[i]=Wzorzec.content[i];
00057 }
00058 }
00059 else
00060 {
00061 content=new int[0];
00062 size=0;
00063 }
00064 }
00065
00066 void Vector::fromText(const std::string& raw)
00067 {
00068 DEBUG("Robie wektor z ",raw)
00069 std::string rozdz,bufor;
00070 if(raw.find(',')!=std::string::npos) rozdz=',';
00071 else if (raw.find(' ')!=std::string::npos) rozdz=' ';
00072 else if (raw.length()>0)
00073 {
00074 chSize(1);
00075 content[0]=atoi(raw.c_str());
00076 return;
00077 }
00078 else
00079 {
00080 chSize(0);
00081 return;
00082 }
00083
00084 unsigned int ilosc = policz(raw,rozdz);
00085 std::string::size_type poczatek=0;
00086 std::string::size_type koniec=0;
00087 this->chSize(ilosc+1);
00088 if(raw[0]=='[')
00089 koniec=1;
00090 for (unsigned int i = 0; i <= ilosc; ++i) {
00091 poczatek=koniec;
00092 if ((koniec=raw.find(rozdz,poczatek+1))==std::string::npos)
00093 koniec=raw.length();
00094
00095
00096
00097 bufor=raw.substr(poczatek,koniec-poczatek);
00098
00099 koniec++;
00100 content[i]=(int)atof(bufor.c_str());
00101 }
00102 }
00103
00104 void Vector::chSize(unsigned int newSize)
00105 {
00106 int *temp=new int[newSize];
00107 delete [] content;
00108 content=temp;
00109 size=newSize;
00110 }
00111 Vector& Vector::operator= (const Vector& rhs)
00112 {
00113 chSize(rhs.size);
00114 for (unsigned int i = 0; i < size; ++i) {
00115 content[i]=rhs.content[i];
00116 }
00117 return *this;
00118 }
00119 Vector& Vector::operator= (const std::string& rhs)
00120 {
00121 this->fromText(rhs);
00122 return *this;
00123 }
00124 Vector Vector::operator+ (const Vector& rhs) const
00125 {
00126 if (this->size!=rhs.size)
00127 {
00128 Vector result(0);
00129 return result;
00130 }
00131
00132 else
00133 {
00134 Vector result(this->size);
00135 DEBUG("Operator + tworzy nowy wektor o rozmiarze: ",result.size)
00136 for (unsigned int i = 0; i < this->size; ++i)
00137 {
00138 result.content[i]=this->content[i]+rhs.content[i];
00139 }
00140 DEBUG("Operator+ zwraca wektor: ",result.toString())
00141 return result;
00142 }
00143
00144 }
00145 Vector& Vector::operator+=(const Vector& rhs)
00146 {
00147 *this=(*this+rhs);
00148 return *this;
00149 }
00150 Vector& Vector::operator-=(const Vector& rhs)
00151 {
00152 *this=(*this-rhs);
00153 return *this;
00154 }
00155 Vector& Vector::operator*=(const int& rhs)
00156 {
00157 *this=(*this*rhs);
00158 return *this;
00159 }
00160 Vector Vector::operator- (const Vector& rhs) const
00161 {
00162 if (this->size!=rhs.size)
00163 {
00164 Vector result(0);
00165 return result;
00166 }
00167 else
00168 {
00169 Vector result(this->size);
00170 for (unsigned int i = 0; i < this->size; ++i) {
00171 result.content[i]=this->content[i]-rhs.content[i];
00172 DEBUG("Odjalem od ",this->content[i])
00173 DEBUG("Wartosc:",rhs.content[i])
00174 DEBUG("Otrzymalem",result.content[i])
00175 }
00176
00177 return result;
00178 }
00179 }
00180 int Vector::operator* (const Vector& rhs) const
00181 {
00182 int result=0;
00183 if (this->size==rhs.size)
00184 {
00185 for (unsigned int i = 0; i < this->size; ++i)
00186 {
00187 result+=this->content[i]*rhs.content[i];
00188 }
00189 }
00190 else
00191 {
00192 return 0;
00193 }
00194 return result;
00195 }
00196 Vector Vector::operator* (const int& rhs) const
00197 {
00198 Vector result(size);
00199 for (unsigned int i = 0; i < size; ++i)
00200 {
00201 result.content[i]=this->content[i]*rhs;
00202 }
00203 return result;
00204 }
00205 Vector operator* (const int& lhs,const Vector& rhs)
00206 {
00207 Vector result(rhs.size);
00208 for (unsigned int i = 0; i < result.size; ++i)
00209 {
00210 result.content[i]=lhs*rhs.content[i];
00211 }
00212 return result;
00213 }
00214 bool Vector::operator==(const Vector& rhs) const
00215 {
00216 if(size!=rhs.size)
00217 return false;
00218 else
00219 {
00220 for (unsigned int i = 0; i < size; ++i)
00221 {
00222 if(content[i]!=rhs.content[i])
00223 return false;
00224 }
00225 return true;
00226 }
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 int& Vector::operator[](const unsigned int index)
00240 {
00241 if(index < size)
00242 return content[index];
00243 else
00244 return content[0];
00245 }
00246
00247
00248
00249
00250 unsigned int policz(std::string wCzym,std::string co)
00251 {
00252 unsigned int liczba=0;
00253 std::string::size_type pozycja=0;
00254 while((pozycja=wCzym.find(co,pozycja))!=std::string::npos)
00255 {
00256 pozycja++;
00257 liczba++;
00258 }
00259 return liczba;
00260 }
00261 std::ostream &operator<< (std::ostream &wyjscie,const Vector &wektorek)
00262 {
00263 wyjscie << wektorek.toString();
00264 return wyjscie;
00265 }
00266 std::istream &operator>> (std::istream &wejscie,Vector &wektorek)
00267 {
00268 std::string raw;
00269 std::getline(wejscie,raw);
00270 DEBUG("Wczytano: ",raw)
00271 wektorek.fromText(raw);
00272 return wejscie;
00273 }
00274
00275 std::string IntToStr(int liczba)
00276 {
00277 std::string result="";
00278 if (liczba >= 0)
00279 {
00280 do
00281 {
00282 result = (char)('0' + (liczba % 10)) +result;
00283 liczba /= 10;
00284 } while (liczba != 0);
00285 }
00286 else
00287 {
00288 do
00289 {
00290 result = (char)('0' - (liczba % 10))+result;
00291 liczba /= 10;
00292 } while (liczba != 0);
00293 result = '-'+result;
00294 }
00295 return result;
00296
00297 }
00298
00299
00300