Untitled
unknown
plain_text
2 years ago
7.1 kB
10
Indexable
#define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #include <iostream> #include "Czas.h" using namespace std; #include "Czas.h" #include <iostream> #pragma once #include <iostream> using namespace std; class Czas { private: int sec{}, min{}, hr{}; void setSec(int sc) { sec = sc; } void setMin(int mn) { min = mn; } void setHr(int h) { hr = h; } public: int getSec() { return sec; } int getMin() { return min; } int getHr() { return hr; } void setTime(int sec_=0, int min_=0, int hr_=0); void showTime(); Czas operator+(Czas& czas); Czas operator+=(int _sec); Czas& operator=(const Czas& inny); bool operator==(Czas& czas); bool operator>(Czas& czas); bool operator<(Czas& czas); bool operator<=(Czas& czas); bool operator>=(Czas& czas); bool operator!=(Czas& czas); Czas(int sec_ = 0, int min_ = 0, int hr_ = 0); }; Czas& Czas::operator=(const Czas& inny) { if (&inny == this) { return *this; } sec = inny.sec; min = inny.min; hr = inny.hr; return *this; } Czas::Czas(int sec_, int min_, int hr_) { int bufor_min = 0, bufor_hr = 0; if (sec_ < 0 || min_ < 0 || hr_ < 0) { cout << "Podales zly czas, sprobuj ponownie!" << endl; return; } if (sec_ >= 0 && sec_ < 59) setSec(sec_); else if (sec_ > 59) bufor_min = sec_ / 60; //cout << bufor_min << endl; //cout << sec_ % 60 << endl; setSec(sec_ % 60); if ((bufor_min + min_) < 60) setMin(bufor_min + min_); else if ((bufor_min + min_) >= 60) { bufor_hr = (bufor_min + min_) / 60; //cout << bufor_hr << endl; setMin((bufor_min + min_) % 60); } setHr(bufor_hr + hr_); } bool Czas::operator!=(Czas& czas) { if ((*this == czas) == 0) return true; else return false; } bool Czas::operator<=(Czas& czas) { if ((*this > czas) == 0 || (*this == czas) == 1) return true; else return false; } bool Czas::operator>=(Czas& czas) { if ((*this > czas) == 1 || (*this == czas) == 1) return true; else return false; } bool Czas::operator<(Czas& czas) { if ((*this > czas) == 0) return true; else return false; } bool Czas::operator>(Czas& czas) { if (hr > czas.getHr()) { return true; } else if (hr < czas.getHr()) { return false; } else if (hr == czas.getHr()) { if (min > czas.getMin()) return true; else if (min < czas.getMin()) return false; else if (min == czas.getMin()) { if (sec > czas.getSec()) return true; else if (sec < czas.getSec()) return false; else if (sec == czas.getSec()) return false; } } } bool Czas::operator==(Czas& czas) { if (sec == czas.getSec() && min == czas.getMin() && hr == czas.getHr()) return true; else return false; } Czas Czas::operator+=(int _sec) { Czas temp; int temp_sec, temp_min, temp_hr = 0; if (_sec + sec >= 60) { temp_sec = (sec + _sec) % 60; if (min + ((sec + _sec) / 60) >= 60) { temp_min = ((min + ((sec + _sec) / 60)) % 60) - 1; temp_hr = hr + ((min + ((sec + _sec) / 60)) / 60) - 1; } temp_min = ((sec + _sec) / 60) - 1; } else { temp_sec = sec + _sec; temp_hr = hr; temp_min = min; } temp.setTime(temp_sec, temp_min, temp_hr); return temp; } Czas Czas::operator+(Czas& czas) { Czas temp; int temp_sec, temp_min, temp_hr; if ((sec + czas.getSec()) >= 60) { temp_sec = (sec + czas.getSec()) % 60; temp_min = (sec + czas.getSec()) / 60; } else { temp_sec = sec + czas.getSec(); temp_min = 0; } if ((temp_min + min + czas.getMin()) >= 60) { temp_hr = ((temp_min + min + czas.getMin()) / 60) + hr + czas.getHr(); //cout << temp_hr; temp_min = (temp_min + min + czas.getMin()) % 60; } else { temp_min = temp_min + min + czas.getMin(); temp_hr = hr + czas.getHr(); } temp.setTime(temp_sec, temp_min, temp_hr); return temp; } void Czas::setTime(int sec_, int min_, int hr_) { int bufor_min = 0, bufor_hr = 0; if (sec_ < 0 || min_ < 0 || hr_ < 0) { cout << "Podales zly czas, sprobuj ponownie!" << endl; return; } if (sec_ >= 0 && sec_ < 59) setSec(sec_); else if (sec_ > 59) bufor_min = sec_ / 60; //cout << bufor_min << endl; //cout << sec_ % 60 << endl; setSec(sec_ % 60); if ((bufor_min + min_) < 60) setMin(bufor_min + min_); else if ((bufor_min + min_) >= 60) { bufor_hr = (bufor_min + min_) / 60; //cout << bufor_hr << endl; setMin((bufor_min + min_) % 60); } setHr(bufor_hr + hr_); } void Czas::showTime() { cout << "Czas: " << hr << ":" << min << ":" << sec << endl; } class Harmonogram { public: Harmonogram(); void addTime(Czas& time); Czas& addTimes(); void showTimes(); ~Harmonogram(); Harmonogram& operator=(const Harmonogram& _harm); int getSize() { return size; } void setSize(int n) { size = n; } Czas& operator[](int index); Harmonogram(Harmonogram& inny); private: Czas* tab; int size; }; Harmonogram::Harmonogram(Harmonogram& inny) { size = inny.size; tab = new Czas[size]; for (int i = 0; i < size; i++) { tab[i] = inny.tab[i]; } } Czas& Harmonogram::addTimes() { int sec_=0, min_=0, hr_=0; for (int i = 0; i < getSize(); i++) { sec_ += tab[i].getSec(); min_ += tab[i].getMin(); hr_ += tab[i].getHr(); } cout << "hr: " << hr_ << " min: " << min_ << " sek: " << sec_ << endl; Czas temp; int buf_min = (sec_ / 60) + min_; temp.setTime(sec_ % 60, buf_min%60, (hr_ + (buf_min / 60))); return temp; } void Harmonogram::showTimes() { for (int i = 0; i < size; i++) { tab[i].showTime(); } } Harmonogram& Harmonogram::operator=(const Harmonogram& _harm) { if (&_harm == this) return *this; delete[]tab; size = _harm.size; tab = new Czas[size]; for (int i = 0; i < size; i++) { tab[i] = _harm.tab[i]; } return *this; } Czas& Harmonogram::operator[](int index) { if (index < 0 && index>=getSize()) { cout << "zly indeks" << endl; } return tab[index]; } void Harmonogram::addTime(Czas& time) { if (getSize() == 0) { tab = new Czas[1]; tab[0] = time; int a = 1; setSize(a); } else { Czas* temp = new Czas[size*2]; cout << size << endl; for (int i = 0; i < size; i++) { temp[i] = tab[i]; } temp[getSize()-1] = time; delete[]tab; int new_size = getSize() + 1; size = new_size; tab = temp; } } Harmonogram::~Harmonogram() { delete[] tab; } Harmonogram::Harmonogram() { size = 0; tab = nullptr; } int main() { Harmonogram H1; Czas c1(25, 58), c2(12, 11, 1), c3(8, 62, 1), c4(65, 55, 4), c5(59, 59, 2); H1.addTime(c1); //H1.addTime(c2); //H1.addTime(c3); //H1.addTime(c4); //H1.addTime(c5); H1.showTimes(); cout << H1.getSize() << endl; //Czas c6 = H1[2]; //c6.showTime(); cout << endl; Czas c7 =H1.addTimes(); c7.showTime(); _CrtDumpMemoryLeaks(); return 0; }
Editor is loading...