hi guys
as u wish here are some codes that can we use ale tommorows lab. take this and learn
// po9-przeciazanie_operatorow-Kanal_sciekowy.cpp #include "stdafx.h" #include <conio.h> #include <string> class Sciek { public : double *kanaly; unsigned int n; Sciek(const unsigned int N); ~Sciek(); operator std::string() const; double operator[](const int i) const; double & operator[](const int i); double & operator+=(const Sciek &RHS); double & operator-=(const Sciek &RHS); double operator+(const Sciek &RHS) const; double operator-(const Sciek &RHS) const; }; Sciek::Sciek(const unsigned int N) { if(N > 100){ n = 100; }else{ if (N < 1) n = 1; else n = N; } kanaly = new double[n]; for(unsigned int i = 0; i<n; ++i) { kanaly[i] = 0.0; } } Sciek::~Sciek() { delete[] kanaly; } Sciek::operator std::string() const { char buff[1000]=""; // powiedzmy, że żaden przepływ nie przekroczy 9 cyfr, trochu na przecinki char buff2[10]; for(unsigned int i=0; i<n; ++i){ sprintf(buff2, ", %.2lf", kanaly[i]); // wpierdalamy na bufor(miejsca po przecinku mozecie wyjebac, jak chcecie) strcat(buff ,buff2); } return std::string(buff); // zwracamy jako stringa } double Sciek::operator[](const int i) const { if(i>n-1) return kanaly[n-1]; if(i<0) return kanaly[0]; return kanaly[i]; } double & Sciek::operator[](const int i) { if(i>n-1) return kanaly[n-1]; if(i<0) return kanaly[0]; return kanaly[i]; } double & Sciek::operator+=(const Sciek &RHS) { double suma=0.0; for(int i = 0; i<n; ++i) suma += kanaly[i]; for(int i = 0; i<RHS.n; ++i) suma += RHS.kanaly[i]; return suma; } double & Sciek::operator-=(const Sciek &RHS) { double suma=0.0; for(int i = 0; i<n; ++i) suma += kanaly[i]; for(int i = 0; i<RHS.n; ++i) suma -= RHS.kanaly[i]; return suma; } double Sciek::operator+(const Sciek &RHS) const { double suma=0.0; for(int i = 0; i<n; ++i) suma += kanaly[i]; for(int i = 0; i<RHS.n; ++i) suma += RHS.kanaly[i]; return suma; } double Sciek::operator-(const Sciek &RHS) const { double suma=0.0; for(int i = 0; i<n; ++i) suma += kanaly[i]; for(int i = 0; i<RHS.n; ++i) suma -= RHS.kanaly[i]; return suma; } int _tmain(int argc, _TCHAR* argv[]) { Sciek kanal1(5), kanal2(2); kanal1[0] = 0.5; kanal1[1] = 100.0; kanal1[2] = 200.0; kanal1[3] = 300.0; kanal1[4] = 400.0; kanal2[0] = 10.1; kanal2[1] = 20.1; printf("Sciek pierwszy - kanal1[0] : %.2lf\n", kanal1[0]); printf("Sciek pierwszy : %s\n", ((std::string)kanal1).c_str() ); printf("Sciek drugi : %s\n", ((std::string)kanal2).c_str() ); printf("Suma : %2.2lf\n", kanal1+kanal2); printf("Roznica : %2.2lf\n", kanal1-kanal2); printf("Suma : %2.2lf\n", kanal1+=kanal2); printf("Roznica : %2.2lf\n", kanal1-=kanal2); _getch(); return 0; }
//============================
#include "stdafx.h" #include <conio.h> #include <string> class Wysypisko { public : double *pryzma; unsigned int n; Wysypisko(const unsigned int N); ~Wysypisko(); operator std::string() const; double operator[](const int i) const; double & operator[](const int i); double & operator+=(const Wysypisko &RHS); double & operator-=(const Wysypisko &RHS); double operator+(const Wysypisko &RHS) const; double operator-(const Wysypisko &RHS) const; }; Wysypisko::Wysypisko(const unsigned int N) { if(N > 100){ n = 100; }else{ if (N < 1) n = 1; else n = N; } pryzma = new double[n]; for(unsigned int i = 0; i<n; ++i) { pryzma[i] = 0.0; } } Wysypisko::~Wysypisko() { delete[] pryzma; } Wysypisko::operator std::string() const { char buff[1000]=""; // powiedzmy, że żaden przepływ nie przekroczy 9 cyfr, trochu na przecinki char buff2[10]; for(unsigned int i=0; i<n; ++i){ sprintf(buff2, ", %.2lf", pryzma[i]); // wpierdalamy na bufor(miejsca po przecinku mozecie wyjebac, jak chcecie) strcat(buff ,buff2); } return std::string(buff); // zwracamy jako stringa } double Wysypisko::operator[](const int i) const { if(i>n-1) return pryzma[n-1]; if(i<0) return pryzma[0]; return pryzma[i]; } double & Wysypisko::operator[](const int i) { if(i>n-1) return pryzma[n-1]; if(i<0) return pryzma[0]; return pryzma[i]; } double & Wysypisko::operator+=(const Wysypisko &RHS) { double suma=0.0; for(int i = 0; i<n; ++i) suma += pryzma[i]; for(int i = 0; i<RHS.n; ++i) suma += RHS.pryzma[i]; return suma; } double & Wysypisko::operator-=(const Wysypisko &RHS) { double suma=0.0; for(int i = 0; i<n; ++i) suma += pryzma[i]; for(int i = 0; i<RHS.n; ++i) suma -= RHS.pryzma[i]; return suma; } double Wysypisko::operator+(const Wysypisko &RHS) const { double suma=0.0; for(int i = 0; i<n; ++i) suma += pryzma[i]; for(int i = 0; i<RHS.n; ++i) suma += RHS.pryzma[i]; return suma; } double Wysypisko::operator-(const Wysypisko &RHS) const { double suma=0.0; for(int i = 0; i<n; ++i) suma += pryzma[i]; for(int i = 0; i<RHS.n; ++i) suma -= RHS.pryzma[i]; return suma; } int _tmain(int argc, _TCHAR* argv[]) { Wysypisko w1(5), w2(2); w1[0] = 0.5; w1[1] = 100.0; w1[2] = 200.0; w1[3] = 300.0; w1[4] = 400.0; w2[0] = 10.1; w2[1] = 20.1; printf("Wysypisko pierwsze - w1[0] : %.2lf\n", w1[0]); printf("Wysypisko pierwsze : %s\n", ((std::string)w1).c_str() ); printf("Wysypisko drugie : %s\n", ((std::string)w2).c_str() ); printf("Suma : %2.2lf\n", w1+w2); printf("Roznica : %2.2lf\n", w1-w2); printf("Suma : %2.2lf\n", w1+=w2); printf("Roznica : %2.2lf\n", w1-=w2); _getch(); return 0; }
//===============
#include "stdafx.h" #include <conio.h> #include <string>classMotocykl{public:std::stringmarka,model;intpoj,masa,pas[3];Motocykl(conststd::stringMARKA,conststd::stringMODEL,constintPOJ,constintMASA,constintP1,constintP2,constintP3);~Motocykl();operatorstd::string()const;operatorint()const;intoperator[](constinti)const;int&operator[](constinti);booloperator==(constMotocykl&m)const;booloperator!=(constMotocykl&m)const;booloperator<(constMotocykl&m)const;booloperator<=(constMotocykl&m)const;booloperator>(constMotocykl&m)const;booloperator>=(constMotocykl&m)const;};Motocykl::Motocykl(conststd::stringMARKA,conststd::stringMODEL,constintPOJ,constintMASA,constintP1=0,constintP2=0,constintP3=0):marka(MARKA),model(MODEL),poj(POJ),masa(MASA){pas[0]=P1;pas[1]=P2;pas[2]=P3;}Motocykl::~Motocykl(){}Motocykl::operatorstd::string()const{charbuf[10];sprintf(buf,"%-9d",poj);std::stringstr;returnstr.append(marka).append(", ").append(model).append(", ").append(buf);}Motocykl::operatorint()const{returnmasa+pas[0]+pas[1]+pas[2];}intMotocykl::operator[](constinti)const{if(i>3)returnpas[2];if(i<1)returnpas[0];returnpas[i-1];}int&Motocykl::operator[](constinti){if(i>3)returnpas[2];if(i<1)returnpas[0];returnpas[i-1];}boolMotocykl::operator<(constMotocykl&m)const{returnmasa+pas[0]+pas[1]+pas[2]<m.masa+m.pas[0]+m.pas[1]+m.pas[2];}boolMotocykl::operator<=(constMotocykl&m)const{returnmasa+pas[0]+pas[1]+pas[2]<=m.masa+m.pas[0]+m.pas[1]+m.pas[2];}boolMotocykl::operator>(constMotocykl&m)const{returnmasa+pas[0]+pas[1]+pas[2]>m.masa+m.pas[0]+m.pas[1]+m.pas[2];}boolMotocykl::operator>=(constMotocykl&m)const{returnmasa+pas[0]+pas[1]+pas[2]<=m.masa+m.pas[0]+m.pas[1]+m.pas[2];}boolMotocykl::operator==(constMotocykl&m)const{return!(marka.compare(m.marka)||model.compare(m.model)||poj==m.poj);}boolMotocykl::operator!=(constMotocykl&m)const{return(marka.compare(m.marka)||model.compare(m.model)||poj==m.poj);}int_tmain(intargc,_TCHAR*argv[]){Motocyklm1("marka1","model1",1000,123,60,70);m1[3]=80;Motocyklm2("marka2","model2",2000,211,90);printf("Motocykl pierwszy : %s\n",((std::string)m1).c_str());printf("Motocykl pierwszy - masa: %d\n",(int)m1);printf("Motocykl pierwszy - pasazer 1: %d\n",m1[1]);printf("Motocykl pierwszy - pasazer 2: %d\n",m1[2]);printf("Motocykl pierwszy - pasazer 3: %d\n",m1[3]);printf("Motocykl drugi : %s\n",((std::string)m2).c_str());printf("Motocykl drugi - masa: %d\n",(int)m2);printf("m1>m2? %d\n",m1>m2);printf("m1>=m2? %d\n",m1>=m2);printf("m1<m2? %d\n",m1<m2);printf("m1<=m2? %d\n",m1<=m2);printf("m1==m2? %d\n",m1==m2);printf("m1!=m2? %d\n",m1!=m2);_getch();return0;}
//=========================
#include "stdafx.h" #include <conio.h> #include <iostream> #include <string> class Calkowita { private: int liczba, min, max; public: Calkowita(const int LICZBA, const int MIN, const int MAX); Calkowita(const Calkowita &L); operator int() const; operator std::string() const; Calkowita & operator=(const Calkowita &L); Calkowita & operator+=(const Calkowita &L); Calkowita & operator-=(const Calkowita &L); Calkowita & operator*=(const Calkowita &L); Calkowita & operator/=(const Calkowita &L); Calkowita operator+(const Calkowita &L) const; Calkowita operator-(const Calkowita &L) const; Calkowita operator*(const Calkowita &L) const; Calkowita operator/(const Calkowita &L) const; bool operator==(const Calkowita &L) const; bool operator!=(const Calkowita &L) const; bool operator<(const Calkowita &L) const; bool operator>(const Calkowita &L) const; bool operator<=(const Calkowita &L) const; bool operator>=(const Calkowita &L) const; Calkowita & operator++(); //pre Calkowita & operator--(); Calkowita operator++(int); //post Calkowita operator--(int); Calkowita operator[](const unsigned int Indeks) const; Calkowita & operator[](const unsigned int Indeks); }; Calkowita::Calkowita(const int LICZBA, const int MIN, const int MAX): liczba(LICZBA), min(MIN), max(MAX) {} Calkowita::Calkowita(const Calkowita &L): liczba(L.liczba), min(L.min), max(L.max) {} Calkowita::operator int() const { return liczba; } Calkowita::operator std::string() const { char buf[64]; sprintf(buf, "%i z [%i...%i]", liczba, min, max); return std::string(buf); } Calkowita & Calkowita::operator=(const Calkowita &L) { liczba = L.liczba; return *this; } Calkowita & Calkowita::operator+=(const Calkowita &L) { liczba += L.liczba; return *this; } Calkowita & Calkowita::operator-=(const Calkowita &L) { liczba -= L.liczba; return *this; } Calkowita & Calkowita::operator*=(const Calkowita &L) { liczba *= L.liczba; return *this; } Calkowita & Calkowita::operator/=(const Calkowita &L) { liczba /= L.liczba; return *this; } Calkowita Calkowita::operator+(const Calkowita &L) const { return Calkowita(liczba + L.liczba, min, max); } Calkowita Calkowita::operator-(const Calkowita &L) const { return Calkowita(liczba - L.liczba, min, max); } Calkowita Calkowita::operator*(const Calkowita &L) const { return Calkowita(liczba * L.liczba, min, max); } Calkowita Calkowita::operator/(const Calkowita &L) const { return Calkowita(liczba / L.liczba, min, max); } bool Calkowita::operator==(const Calkowita &L) const { return (liczba == L.liczba); } bool Calkowita::operator!=(const Calkowita &L) const { return (liczba != L.liczba); } bool Calkowita::operator<(const Calkowita &L) const { return (liczba < L.liczba); } bool Calkowita::operator>(const Calkowita &L) const { return (liczba > L.liczba); } bool Calkowita::operator<=(const Calkowita &L) const { return (liczba <= L.liczba); } bool Calkowita::operator>=(const Calkowita &L) const { return (liczba >= L.liczba); } Calkowita & Calkowita::operator++() // Preinkrementacja. { if(liczba < max)++liczba; return *this; } Calkowita & Calkowita::operator--() // Predekrementacja. { if(liczba > min) --liczba; return *this; } Calkowita Calkowita::operator++(int) // Postinkrementacja. { Calkowita poprz(*this); if(liczba < max)++liczba; return poprz; } Calkowita Calkowita::operator--(int) // Postdekrementacja. { Calkowita poprz(*this); if(liczba > min) --liczba; return poprz; } Calkowita Calkowita::operator[](const unsigned int Indeks) const { return *( this + Indeks*sizeof(Calkowita) ); } Calkowita & Calkowita::operator[](const unsigned int Indeks) { return *( this + Indeks*sizeof(Calkowita) ); } int _tmain(int argc, _TCHAR* argv[]) { Calkowita A(100, -1000, +1000); std::cout<<"A = "<<A<<std::endl; Calkowita B(200, -1000, +1000); std::cout<<"B = "<<B<<std::endl; Calkowita C(300, -1000, +1000); std::cout<<"C = "<<C<<std::endl; Calkowita D = A; std::cout<<"Calkowita D = A;"<<std::endl; std::cout<<"D = "<<D<<std::endl;++D; std::cout<<"++D;"<<std::endl; std::cout<<"D = "<<D<<std::endl; D++; std::cout<<"D++;"<<std::endl; std::cout<<"D = "<<D<<std::endl; Calkowita E(A); std::cout<<"Calkowita E(A);"<<std::endl; std::cout<<"E = "<<E<<std::endl; --E; std::cout<<"--E;"<<std::endl; std::cout<<"E = "<<E<<std::endl; E--; std::cout<<"E--;"<<std::endl; std::cout<<"E = "<<E<<std::endl; int F = A; std::cout<<"int F = A;"<<std::endl; std::cout<<"F = "<<F<<std::endl; std::string G = A; std::cout<<"std::string G = A;"<<std::endl; std::cout<<"G = "<<G<<std::endl; C = B = A; std::cout<<"C = B = A;"<<std::endl; std::cout<<"A = "<<A<<std::endl; std::cout<<"B = "<<B<<std::endl; std::cout<<"C = "<<C<<std::endl; std::cout<<"A + B = "<<A + B<<std::endl; std::cout<<"A * 3 = "<<A * 3<<std::endl; std::cout<<"(A == B) = "<<(A == B)<<std::endl; std::cout<<"(A != B / C) = "<<(A != B / C)<<std::endl; std::cout<<"(A > B - C) = "<<(A > B - C)<<std::endl; Calkowita T[5] = { A, B, C, D, E}; std::cout<<"Calkowita T[5] = { A, B, C, D, E};"<<std::endl; for(int i=0; i<5; ++i) std::cout<<"T["<<i<<"] = "<<T[i]<<std::endl; T[0] = Calkowita(0, -9, 9); std::cout<<"T[0] = Calkowita(0, -9, 9);"<<std::endl; std::cout<<"T[0] = "<<T[0]<<std::endl; T[1] += T[4]; std::cout<<"T[1] += T[4];"<<std::endl; std::cout<<"T[1] = "<<T[1]<<std::endl; T[2] -= T[3]; std::cout<<"T[2] -= T[3];"<<std::endl; std::cout<<"T[2] = "<<T[2]<<std::endl; _getch(); return 0; }
silesian university of technology katowice