Facebook
From Adam Musiał, 7 Years ago, written in C++.
This paste is a reply to Kolokwium PK3 from Adam Musiał - go back
Embed
Viewing differences between Kolokwium PK3 and Kolokwium PK3 v2
// Tablica_Dynamiczna_kolos_PK.cpp : Defines the entry point for the console application.
//

//
 
#include "stdafx.h"
#include 


 
class Zespolona
{
                double rzeczywista;
        
rzeczywista;
        
double urojona;

public:

        
urojona;
 
public:
 
        
Zespolona():rzeczywista(0), urojona(0) {}
        
{}
        
//nie trzeba tworzyć konstruktorów kopiującego i przenoszącego, bo nie operujemy na 
        
na
        
//strukturach dynamicznych, gdzie obie struktury miałyby te same wskaźnik
        
wskaźnik
        
Zespolona(double r, double u): rzeczywista(r), urojona(u) {}

        
{}
 
        
double getR() const { return rzeczywista; }
        
}
        
double getU() const { return urojona; }

        
}
 
        
void setR(const double r):rzeczywista(r) {}
        
{}
        
void setU(const double u):urojona(u) {}
        
        
{}
       
        
friend std::istream& operator >>(std::istream& s, Zespolona& z)
        {
                
z)
        {
                
s >> z.rzeczywista >> z.urojona;
                                return s;
        }

        
s;
        }
 
        
bool operator <(const Zespolona& z) const //ten const to tego, że nie zmieniamy this
        {
                
this
        {
                
return rzeczywista*rzeczywista + urojona*urojona
                        
urojona*urojona
                        
< z.rzeczywista*z.rzeczywista + z.urojona*z.urojona;
                }
};

};
 
class Tab
{
protected:
                //Zespolona** tablica; dla tablic dynamicznych wsk
        
wsk
        
Zespolona* tablica;
        
tablica;
        
unsigned int rozmiar;
public:
                Tab(): tablica(nullptr), rozmiar(0) {}
        ~Tab()
        {
                
{}
        ~Tab()
        {
                
//dla tablic dynamicznych wsk, a nie obiektów jak niżej
                
niżej
                
//for (int i = 0; i < rozmiar; ++i)
                //        
++i)
                //      
delete tablica[i];
                
tablica[i];
                
//delete[] tablica;
                
tablica;
                
delete[] tablica;
        }
        
        
tablica;
        }
       
        
//operator += dodaje element na koniec tablicy
        
tablicy
        
virtual void operator +=(const Zespolona& z)
        {
                
z)
        {
                
//new Zespolona*[rozmiar + 1]; - dla tablicy wsk
                
wsk
                
auto nowaTab = new Zespolona[rozmiar + 1];
                
1];
                
for (unsigned int i = 0; i < rozmiar; ++i)
                {
                        
++i)
                {
                        
nowaTab[i] = tablica[i]; //dla tabWsk: nowaTab[i]= new Zespolona(*tablica[i]);
                        
Zespolona(*tablica[i]);
                        
//dla tabWsk musimy stworzyć nową tablicę na podstawie tablica[i], i trzeba wyłuskać te wartości
                }
                
wartości
                }
                
nowaTab[rozmiar] = z;
                
z;
                
delete[] tablica;
                
tablica;
                
tablica = nowaTab;
                ++rozmiar;
        }
        
        
nowaTab;
                ++rozmiar;
        }
       
        
//operator przypisania
        
przypisania
        
Tab& operator=(const Tab& t)
        {
                
t)
        {
                
//zwraca Tab&, żeby można było to łańcuchować
                
łańcuchować
                
//int a = 5;
                
5;
                
//int a = b = 5;
                
5;
                
if (this == &t) //ZAWSZE!!!
                        
//ZAWSZE!!!
                        
return *this;
                
*this;
                
delete[] tablica;
                
tablica;
                
rozmiar = t.rozmiar;
                                tablica = new Zespolona[rozmiar];
                
Zespolona[rozmiar];
                
for (unsigned int i = 0; i < rozmiar; ++i)
                        
++i)
                        
tablica[i] = t.tablica[i];
                                return *this;
        }

        
*this;
        }
 
        
//operator przenoszenia
        
przenoszenia
        
Tab& operator=(Tab&& t)
        {
                
t)
        {
                
if (this == &t) //ZAWSZE!!!
                        
//ZAWSZE!!!
                        
return *this;

                
*this;
 
                
delete[] tablica;
                
tablica;
                
rozmiar = t.rozmiar;
                                t.rozmiar = 0;
                                tablica = t.tablica;
                                t.tablica = nullptr;
        }

        
        }
 
        
Zespolona operator[](unsigned int i)//const jest do referencji
        {
                
referencji
        {
                
if (i >= rozmiar)
                {
                        
rozmiar)
                {
                        
throw std::exception();
                }
                
std::exception();
                }
                
return tablica[i];
        
tablica[i];
        
}
};

};
 
class Kolejka: public Tab
{
public:
                void operator+=(const Zespolona& z) override
        {
                
override
        {
                
auto nowaKolejka = new Zespolona[rozmiar + 1];

                
1];
 
                
int i = 0;
                
0;
                
//wyszukiwanie elementu i przepisywanie elementów do szukanego
                
szukanego
                
while (i < rozmiar && tablica[i] < z)
                {
                        
z)
                {
                        
nowaKolejka[i] = tablica[i];
                        ++i;
                }
                
tablica[i];
                        ++i;
                }
                
//wpisanie nowego elementu
                
elementu
                
nowaKolejka[i] = z;
                ++rozmiar;
                ++i;
                
z;
                ++rozmiar;
                ++i;
                
//przepisanie reszty elementów
                
elementów
                
while (i < rozmiar)
                {
                        
rozmiar)
                {
                        
nowaKolejka[i] = tablica[i-1];
                        ++i;
                }
                
tablica[i-1];
                        ++i;
                }
                
delete[] tablica;
                
tablica;
                
tablica = nowaKolejka;
        }

        
nowaKolejka;
        }
 
        
friend std::ostream& operator>>(std::ostream& s, Kolejka& k)
        {
                
k)
        {
                
//for (int i = 0; i < rozmiar; ++i) dla tabWSk
                //        
tabWSk
                //      
delete tablica[i]; dla tabWSk
                
tabWSk
                
for (int i = 0; i < rozmiar; ++i)
                {
                        
++i)
                {
                        
//tablica[i] = new Zespolona(); dla tabWSk
                        
tabWSk
                        
s >> tablica[i];
                }
                
tablica[i];
                }
                
return s;
        }

        
s;
        }
 
        
operator Zespolona()
        {
                
Zespolona()
        {
                
double sumaR = 0;
                
0;
                
double sumaU = 0;
                
0;
                
for (unsigned int i = 0; i < rozmiar; ++i)
                {
                        
++i)
                {
                        
sumaR += tablica[i].getR();
                                                sumaU += tablica[i].getU();
                }
                
                }
                
return Zespolona(sumaR, sumaU);
        }

        
sumaU);
        }
 
        
Zespolona pop()
        {
                
pop()
        {
                
auto bufor = tablica[0];
                
tablica[0];
                
auto nowaTab = new Zespolona[rozmiar - 1];
                
1];
                
for (unsigned int i = 1; i < rozmiar; ++i)
                {
                        
++i)
                {
                        
nowaTab[i - 1] = tablica[i];
                }
                
tablica[i];
                }
                
delete[] tablica;
                --rozmiar;
                
tablica;
                --rozmiar;
                
tablica = nowaTab;
                
nowaTab;
                
return bufor;
        
bufor;
        
}
};

};
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  \n 
  // Kolokwium_lista.cpp : Defines the entry point for the console application.
//

//
 
#include "stdafx.h"
#include 
#include h>

h>
 
class Payment
{
                char* name;
        
name;
        
double cost;
        
cost;
        
int day;
        
day;
        
int month;
        
month;
        
int year;
public:

        
public:
 
        
//konstruktor 5-cio argumentowy
        
argumentowy
        
Payment(char *n, double c, int d, int m, int y) : cost(c), day(d), month(m), year(y)
        {
                
year(y)
        {
                
name = new char[strlen(n) + 1];
                
1];
                
for (unsigned int i = 0; i <= strlen(n); ++i)
                        
++i)
                        
name[i] = n[i];
        }

        
n[i];
        }
 
        
//konstruktor kopiujący
        
kopiujący
        
Payment(const Payment& p) : Payment(p.name, p.cost, p.day, p.month, p.year) {}

        
{}
 
        
//konstruktor przenoszący
        
przenoszący
        
Payment(Payment&& p)
        {
                
p)
        {
                
name = p.name;
                                day = p.day;
                                month = p.month;
                                year = p.year;
                                cost = p.cost;
                                p.cost = 0;
                                p.day = 0;
                                p.month = 0;
                                p.year = 0;
                                p.name = nullptr;
        }

        
        }
 
        
//operator przypisania
        
przypisania
        
Payment& operator=(const Payment& p)
        {
                
p)
        {
                
if (this == &p) // ten if na początek każdego operatora = (& i &&) kopiującego i przenoszącego/przypisującego
                        
przenoszącego/przypisującego
                        
return *this;

                
*this;
 
                
cost = p.cost; 
                
cost;
                
day = p.day;
                                month = p.month;
                                year = p.year;
                                delete[] name;
                
name;
                
name = new char[strlen(p.name) + 1];
                                for (unsigned int i = 0; i <= strlen(p.name); ++i)
                                                name[i] = p.name[i];
        }

        
        }
 
        
//operator porównania
        
porównania
        
bool operator<(const Payment& p) const
        {
                
const
        {
                
if (year == p.year)
                                                if (month == p.month)
                                                                return day < p.day;
                        else
                                
                        else
                                
return month < p.month;
                else
                        
                else
                        
return year < p.year;
        }

        //gettery
        
        }
 
        //gettery
        
char* getName() const { return name; }
        
}
        
double getCost() const { return cost; }
        
}
        
int getDay() const { return day; }
        
}
        
int getMonth() const { return month; }
        
}
        
int getYear() const { return year; }

          //settery
          
}
 
        //settery
        
void setName(const char* n):name(n) {} 
          
{}
        
void setCost(const double c): cost(c) {}
          //itd...

        
{}
        //itd...
 
        
//zaprzyjażniony operator strumieniowy
        
strumieniowy
        
friend std::ostream& operator << (std::ostream& s, const Payment& p)
        {
                
p)
        {
                
s << p.name << " " << p.cost << " " << p.day << "." << p.month << "." << p.year;
                                return s;
        }

};


s;
        }
 
};
 
 
struct element
{
                element* next;
        
next;
        
element* prev;
        
prev;
        
Payment p;
        
p;
        
//konstruktor kopiujący
        
kopiujący
        
element(const Payment& other) : next(nullptr), prev(nullptr), p(other) {}
};

};
 
class List
{
protected:
                element* head;
        
head;
        
element* tail;
public:
                //konstruktor bezargumentowy
        
bezargumentowy
        
List() : head(nullptr), tail(nullptr) {}
        //destruktor
        ~List()
        {
                
{}
        //destruktor
        ~List()
        {
                
while (head)
                {
                        
(head)
                {
                        
auto bufor = head;
                        
head;
                        
head = head->next;
                        
head->next;
                        
delete bufor;
                }
                
bufor;
                }
                
tail = nullptr;
        }

        
nullptr;
        }
 
        
//jak operator ma być przeciążony w klasie pochodnej to stosujemy "virtual" w klasie nadrzednej, a w klasie pochodnej "override", bo nadpisujemy

        
nadpisujemy
 
        
//przeciążony operator +=
        
+=
        
virtual void operator+=(const Payment& p) //typy klasowe moga byc bardzo duze i chcemy uniknac ich kopiowania dlatego &
        {
                
&
        {
                
auto newElement = new element(p);
                
element(p);
                
if (tail)
                {
                        
(tail)
                {
                        
auto bufor = tail;
                        
tail;
                        
tail = newElement;
                        
newElement;
                        
bufor->next = tail;
                        
tail;
                        
tail->prev = bufor;
                }
                else
                {
                        
bufor;
                }
                else
                {
                        
tail = head = newElement;
                }
        }

        
newElement;
                }
        }
 
        
//jeśli mamy opertator += napisać to robimy to przed konstruktorem, bo go wykorzystujemy

        
wykorzystujemy
 
        
//konstruktor kopiujący
        
kopiujący
        
List(const List& other)
        {
                
other)
        {
                
auto ptr = other.head;
                                while (ptr)
                {
                        
(ptr)
                {
                        
*this += ptr->p;
                        
ptr->p;
                        
ptr = ptr->next;
                }
        }

        
ptr->next;
                }
        }
 
        
//zaprzyjaźniony operator strumieniowy <<
        
<<
        
friend std::ostream& operator <<(std::ostream& s, const List& l)
        {
                
l)
        {
                
auto ptr = l.head;
                                while (ptr)
                {
                        
(ptr)
                {
                        
s << ptr->p << std::endl;
                        
std::endl;
                        
ptr = ptr->next;
                }
                
ptr->next;
                }
                
return s;
        
s;
        
}
};

};
 
class Queue: public List
{
public:
  \n   
  
//nadpisany operator += 
        
+=
        
void operator+=(const Payment& p) override
        {
                
override
        {
                
auto newElement = new element(p);
                
element(p);
                
auto ptr = head;
                
head;
                
while (ptr && p < ptr->p)
                {
                        
ptr->p)
                {
                        
ptr = ptr->next;
                }
                
ptr->next;
                }
                
if (ptr)
                {
                        
(ptr)
                {
                        
auto bufor = ptr->prev;
                        
ptr->prev;
                        
ptr->prev = newElement;
                        
newElement;
                        
if (ptr == head) head = newElement; //czy to jest konieczne?
                        
konieczne?
                        
if (bufor) bufor->next = newElement; 
                        
newElement;
                        
newElement->prev = bufor;
                        
bufor;
                        
newElement->next = ptr;
                }
                else
                {
                        
ptr;
                }
                else
                {
                        
auto bufor = tail;
                        
tail;
                        
tail = newElement;
                        
newElement;
                        
if (bufor)
                        {
                                
(bufor)
                        {
                                
bufor->next = tail;
                                
tail;
                                
tail->prev = bufor;
                                return;
                        }
                        else
                        {
                                
bufor;
                                return;
                        }
                        else
                        {
                                
head = tail;
                        }
                }
        }

tail;
                        }
                }
        }
 
   //usuwanie listy
                void deleteList()
        {
                
deleteList()
        {
                
while (head)
                {
                        
(head)
                {
                        
auto bufor = head;
                        
head;
                        
head = head->next;
                        
head->next;
                        
delete bufor;
                }
                
bufor;
                }
                
tail = nullptr;
        }

nullptr;
        }
 
  //operator = przypisania
                Queue& operator=(const Queue& q)
q)
        {
                if (this == &q)
                        return *this;
 
                deleteList();
                auto ptr = q.head;
                while (ptr)
                {
                        *this += ptr->p;
                        ptr = ptr->next;
                }
                return *this;
        }
 
 
        //operatory konwersji zapisuje się:  operator TypDoKtoregoKonwertujesz() { funkcja }
        operator double()
        {
                auto ptr = head;
                auto sum = 0.0;
                while (ptr)
                {
                        sum += (ptr->p).getCost();
                        ptr = ptr->next;
                }
                return sum;
        }
 
        Payment pop()
        {
                auto toReturn = head->p;
                auto bufor = head;
                head = head->next;
                if (tail == bufor)
                        tail = nullptr;
                delete bufor;
                return toReturn;
        }
};
 
int main()
{
        Queue list;
        list += Payment("Adam", 50, 4, 12, 1997);
        list += Payment("Piotr", 30, 5, 12, 1997);
        list += Payment("Tomasz", 80, 4, 8, 1997);
        list += Payment("Domek", 25, 14, 1, 1998);
        list += Payment("Lipa", 60, 17, 2, 1995);
        list += Payment("Lol", 56, 19, 7, 2000);
        std::cout << list;
        Queue l;
        l = list;
        std::cout << std::endl << l;
        std::cout << std::endl << (double)l;
        std::cout << std::endl << l.pop();
        std::cout << std::endl <  
    return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include 
#include 
#include 
using namespace std;


///////////////////////////////////////////////////////////////////////////////
class task
{
        string todo = "empty task";
        int date = 0;

public:
        task(string, int);

        task() = default;
        task(const task &) = default;
        task & operator=(const task &) = default;

        void complete(void);
        int operator>(const task &);        // porownoje daty
        int operator==(const task &);        // porownuje cale zadania
        friend ostream & operator<<(ostream &, const task &);
        friend istream & operator>>(istream &, task &);
};

task::task(string todo, int date) : todo(todo), date(date) {}

void task::complete(void)
{
        cout << "\ntask " << todo << " scheduled for " << date << " done successfully!";
}

int  task::operator>(const task &rt)
{
        return date>rt.date;
}

int  task::operator==(const task &rt)
{
        return ((todo == rt.todo) && (date == rt.date));
}

ostream & operator<<(ostream &os, const task &rt)
{
        os << rt.todo;
        os << " ";
        os << rt.date;
        return os;
};

istream & operator>>(istream &is, task &rt)
{
        return is >> rt.todo >> rt.date;
};


struct q_elem
{
        task t;
        q_elem * next;
        q_elem(const task &ctr) :t(ctr), next(nullptr) {};
};


///////////////////////////////////////////////////////////////////////////////
class queue
{
protected:
        q_elem *head, /* nullptr dla pustej kolejki */
                *tail; /* istotny jesli head != nullptr */

public:
        queue() :head(nullptr) {}

        virtual queue & operator+=(const task &rq) // virtual!!!
        {
                q_elem *qep = new q_elem(rq);
                
if (this (head)
                        tail->next = qep;
                else
                        head = qep;

                tail = qep;

                return *this;
        }

        queue(const queue &rq) :head(nullptr)
        {
                q_elem *qep = rq.head;

                while (qep)
                {
                        (*this) += qep->t;
                        qep = qep->next;
                }
        }

        queue(queue &&rrq) : head(rrq.head), tail(rrq.tail)
        {
                rrq.head = rrq.tail = nullptr;
        }

        ~queue()
        {
                while (head)
                {
                        const q_elem * qep = head;
                        head = head->next;
                        delete qep;
                }
        }

        void empty()        // pozostawia obiekt pusty (head==nullptr)
        {
                while (head)
                {
                        const q_elem * qep = head;
                        head = head->next;
                        delete qep;
                }
        }

        queue & operator=(const queue &rq)
        {
                if (&rq 
== &q)
this)
                        return *this;

                deleteList();
                auto ptr 
empty();

                q_elem *qep 
q.rq.head;
                while (ptr)
(qep)
                {
                        *this (*this) += ptr->p;
                        ptr 
qep->t;
                        qep 
ptr->next;
                }
qep->next;
                }

                return *this;
        }


        //operatory konwersji zapisuje się:  operator TypDoKtoregoKonwertujesz() { funkcja }
        operator double()
}

        queue & operator=(queue &&rq)
        {
                auto ptr swap(head, rq.head);
                swap(tail, rq.tail);
                return *this;
        }

        int contains(const task & ctr)
        {
                q_elem *qep 
= head;
                auto sum = 0.0;
                
while (ptr)
(qep)
                        if (qep->t == ctr)
                                return 1;
                        else
                                qep = qep->next;

                return 0;
        }

        friend istream & operator>>(istream &is, queue &rq)
        {
                task t;                // wymaga konstruktora bezargumentowego task()

                rq.empty();

                while (is >> t)
                        rq += t;

                return is;
        }

        friend ostream & operator<<(ostream &os, const queue &rq)
        {
                q_elem *qep = rq.head;
                while (qep)
                {
                        sum += (ptr->p).getCost();
                        ptr 
os << qep->t << " ";
                        qep 
ptr->next;
qep->next;
                }

                return os;
        }

};

///////////////////////////////////////////////////////////////////////////////
class sorted_q :public queue
{
public:
        sorted_q & operator+=(const task &rq)        // inny typ zwracany - ok tutaj 
        {
                q_elem * const newel = new q_elem(rq);

                if ((!head) || (head->t > rq))        // wstaw na pocz�tek
                {
                        newel->next = head;
                        head = newel;
                }
                else
                {
                        q_elem * pq = head;
                        while (pq->next && !(pq->next->t>rq))
                                pq = pq->next;

                        newel->next = pq->next;
                        pq->next = newel;
                }

                if (!newel->next)
                        tail = newel;

                
return sum;
*this;
        }

        Payment pop()
void complete(queue &tobedone)
        {
                auto toReturn = head->p;
                auto bufor 
q_elem *qep = head;
                head = head->next;
                
while (qep)
                {
                        
if (tail == bufor)
                        tail 
(tobedone.contains(qep->t))
                                qep->t.complete();
                        qep 
nullptr;
                delete bufor;
                return toReturn;
        }
qep->next;
                }
        }

        using queue::operator=;

        //        kolejno�� wywo�ywania konstruktor�w powoduje ze poni�sza deklaracja nie jest potrzebna
        //
        //        using queue::queue;

        //        automatyczne konwersje o kl. bazowej i metody wirtualne powoduj� �e poni�sze nie s� niezb�dne
        //
        //        friend istream & operator>>(istream &is, sorted_q &rq)
        //        friend ostream & operator<<(ostream &os, const sorted_q &rq)

};

int main()
{
        Queue list;
        list 
queue q1;
        sorted_q s1;

        q1 
+= Payment("Adam", 50, 4, 12, 1997);
        list 
task("task a", 20161020);
        q1 
+= Payment("Piotr", 30, 5, 12, 1997);
        list 
task("task b", 20161020);
        q1 
+= Payment("Tomasz", 80, 4, 8, 1997);
        list 
task("task c", 20161515);
        q1 
+= Payment("Domek", 25, 14, 1, 1998);
        list 
task("task d", 20160505);

        queue q2 = move(q1);
        cout << "q1 " << q1 << endl << "q2 " << q2 << endl;
        q1 = move(q2);
        cout << "q1 " << q1 << endl << "q2 " << q2 << endl;
        q2 = q1;
        q1 
+= Payment("Lipa", 60, 17, 2, 1995);
        list 
task("task e", 20160505);
        cout << endl << q1;
        q1 = q2;
        cout << endl << q1;

        s1 = q1;

        cout << endl << s1;
        s1 
+= Payment("Lol", 56, 19, 7, 2000);
        std::cout 
task("task f", 900000000);
        s1 += task("task g", 00000);
        cout 
<< list;
        Queue l;
        l = list;
        std::cout 
endl << std::endl s1;
        s1.complete(q1);
        cin >> s1;
        cout 
<< l;
        std::cout 
endl << std::endl << (double)l;
        std::cout << std::endl << l.pop();
        std::cout << std::endl <
    return 0;
}


s1;
        cout.flush();
}