Pojemnik na dynamicznej tablicy jednowymiarowej

⎗
✓
#include<iostream>

using namespace std;

class Figura {
    private:
        string nazwa;
    public:
        virtual double pole() = 0;
        virtual string typ() = 0;
        Figura(string n) {
            nazwa = n;
        }
        string get_nazwa() {
            return nazwa;
        }
};

class Kwadrat : public Figura {
    private:
        double bok;
    public:
        Kwadrat(string n, double b) : Figura(n) {
            bok = b;
        }
        double pole() {
            return bok * bok;
        }
        string typ() {
            return "kwadrat";
        }
};

class Trojkat : public Figura {
    private:
        double podstawa;
        double wysokosc;
    public:
        Trojkat(string n, double p, double w) : Figura(n) {
            podstawa = p;
            wysokosc = w;
        }
        double pole() {
            return (podstawa * wysokosc) / 2;
        }
        string typ() {
            return "trojkat";
        }
};

class Pojemnik {
    private:
        Figura** wsk;
        int licznik;
        int rozmiar;
    public:
        Pojemnik(int r) {
            r = rozmiar;
            wsk = new Figura*[rozmiar];
            licznik = 0;
        }
        void wyswietlanie() {
            for(int i = 0; i < licznik; i++) {
                cout << wsk[i]->get_nazwa() << " " << wsk[i]->typ() << " " << wsk[i]->pole() << " ";
            }
        }
        void add(Figura* figura) {
            if(licznik == rozmiar) {
                rozmiar += 2;
                Figura** temp = new Figura*[rozmiar];
                for(int i = 0; i < rozmiar - 2; i++) {
                    temp[i] = wsk[i];
                }
                wsk = temp;
            }
            wsk[licznik] = figura;
            for(int i = 0; i < licznik - 1; i++) {
                for(int j = 0; j < licznik - i - 1; j++) {
                    if((wsk[j]->pole() > wsk[j + 1]->pole()) or ((wsk[j]->pole() == wsk[j + 1]->pole()) and wsk[j]->get_nazwa() > wsk[j + 1]->get_nazwa())) {
                        swap(wsk[j], wsk[j + 1]);
                    }
                }
            }
            licznik++;
        }
        void removee(string nazwa) {
            for(int i = 0; i < licznik; i++) {
                if (wsk[i]->get_nazwa() == nazwa) {
                    delete wsk[i];
                    for(int j = i; j < licznik - 1; j++) {
                        wsk[j] = wsk[j + 1];
                    }
                    licznik--;
                    i--;
                }
            }
        }
};

int main() {

    int N;
    cin >> N;
    int M;
    cin >> M;
    Pojemnik pojemnik(N);
    for(int i = 0; i < M; i++) {
        string typ, nazwa;
        double dlugosc, wysokosc;
        cin >> typ;
        if(typ == "trojkat") {
            cin >> nazwa >> dlugosc >> wysokosc;
            pojemnik.add(new Trojkat(nazwa, dlugosc, wysokosc));
        }
        if(typ == "kwadrat") {
            cin >> nazwa >> dlugosc;
            pojemnik.add(new Kwadrat(nazwa, dlugosc));
        }
    }
    int L;
    cin >> L;
    for(int i= 0; i < L; i++) {
        string nazwa;
        cin >> nazwa;
        pojemnik.removee(nazwa);
    }
    pojemnik.wyswietlanie();
    return 0;
}

Lista jednokierunkowa

⎗
✓
#include<iostream>

using namespace std;

template <typename T>
class Lista;

template <typename T>
class Element {
    private:
        T value;
        Element<T> *next;
    public:
        Element(T v, Element<T> *n) {
            value = v;
            next = n;
        }
        friend class Lista<T>;
};

template <typename T>
class Lista {
    private:
        Element<T> *first;
    public:
        Lista() {
            first = nullptr;
        }
        void add(T value) {
            first = new Element<T>(value, first);
        }
        void removee(int ile){
            if(first != nullptr) {
                for(int i = 0; i < ile; i++) {
                    first = first->next;
                }
            }
        }
        void show() {
            Element<T> *tmp = first;
            while(tmp != nullptr) {
                cout << tmp->value << " ";
                tmp = tmp->next;
            }
        }
        void znajdz(string slowo) {
            int licznik = 0;
            Element<T> *tmp = first;
            while(tmp != nullptr) {
                for(int i = 0; i < slowo.length(); i++) {
                    if(slowo[i] == tmp->value[i]) {
                        licznik++;
                    }
                }
                if(licznik == slowo.length()) {
                    cout << tmp->value << " ";
                    return;
                }
                licznik = 0;
                tmp = tmp->next;
            }
            cout << "No string fulfilling the search criteria" << " ";
        }
};

int main() {

    Lista<string> lista;

    int M;
    cin >> M;
    for(int i = 0; i < M; i++) {
        string slowo;
        cin >> slowo;
        lista.add(slowo);
    }

    string slowo;
    cin >> slowo;

    int N;
    cin >> N;

    lista.znajdz(slowo);
    lista.removee(N);

    lista.show();

    return 0;
}

Lista dwukierunkowa

⎗
✓
#include<iostream>
#include<cmath>
#include <cassert>

using namespace std;

template <typename T>
class Lista;

template <typename T>
class Element {
    private:
        T wartosc;
        Element<T> *next;
        Element<T> *prev;
    public:
        Element(T w, Element<T> *n = nullptr, Element<T> *p = nullptr) {
            wartosc = w;
            next = n;
            prev = p;
        }

        ~Element() {}

        T getData() {
            return wartosc;
        }

        Element<T> *getNext() {
            return next;
        }

        Element<T> *getPrev() {
            return prev;
        }

        friend class Lista<T>;
};

template <typename T>
class Lista {
    private:
        Element<T> *first;
        Element<T> *last;
    public:
        Lista() {
            first = nullptr;
            last = nullptr;
        }

        ~Lista() {}

        void add(T val) {
            Element<T>* newElement = new Element<T>(val);
            if (!first) {
                first = last = newElement;
            } else {
                Element<T>* current = first;
                Element<T>* prev = nullptr;
                while (current && val > current->getData()) {
                    prev = current;
                    current = current->getNext();
                }
                if (!current) {
                    newElement->prev = last;
                    last->next = newElement;
                    last = newElement;
                } else {
                    newElement->next = current;
                    newElement->prev = prev;
                    if (prev) {
                        prev->next = newElement;
                    } else {
                        first = newElement;
                    }
                    current->prev = newElement;
                }
            }
        }

        void removeElement(T val) {
            Element<T>* current = first;
            while (current) {
                if (current->getData() == val) {
                    Element<T>* next = current->getNext();
                    Element<T>* prev = current->getPrev();
                    if (prev) {
                        prev->next = next;
                    } else {
                        first = next;
                    }
                    if (next) {
                        next->prev = prev;
                    } else {
                        last = prev;
                    }
                    Element<T>* temp = current;
                    current = next;
                    delete temp;
                } else {
                    current = current->getNext();
                }
            }
        }

        friend ostream& operator<<(ostream& os, Lista<T>& list) {
            Element<T> *current = list.last;
            while (current) {
                os << current->getData() << " ";
                current = current->getPrev();
            }
            return os;
        }
};

class Zespolona {
    private:
        float real;
        float imaginary;
    public:
        Zespolona(float r = 0, float i = 0) {
            real = r;
            imaginary = i;
        }

        ~Zespolona() {}

        float calculateMagnitude() const {
            return sqrt(real * real + imaginary * imaginary);
        }

        bool operator<(const Zespolona& other) const {
            if (calculateMagnitude() == other.calculateMagnitude()) {
                if (real == other.real) {
                    return imaginary < other.imaginary;
                }
                return real < other.real;
            }
            return calculateMagnitude() < other.calculateMagnitude();
        }

    bool operator>(const Zespolona& other) const {
        return other < *this;
    }

    bool operator==(const Zespolona& other) const {
        return real == other.real && imaginary == other.imaginary;
    }

    friend ostream& operator<<(ostream& os, const Zespolona& complex) {
        os << complex.real;
        if (complex.imaginary != 0) {
            if(complex.imaginary < 0) {
                os << complex.imaginary << "i";
            }
            if(complex.imaginary > 0) {
                os << "+" << complex.imaginary << "i";
            }
        }
        return os;
    }

};

int main() {

    int M, N;
    cin >> M;

    Lista<Zespolona> lista;
    for (int i = 0; i < M; i++) {
        float real, imaginary;
        cin >> real;
        cin >> imaginary;

        lista.add(Zespolona(real, imaginary));
    }

    cout << lista;

    cin >> N;

    for (int i = 0; i < N; ++i) {
        float real, imaginary;
        cin >> real;
        cin >> imaginary;

        lista.removeElement(Zespolona(real, imaginary));
    }

    cout << lista;

    return 1;
}

ONP

⎗
✓
#include<iostream>
#include<cassert>
#include<stack>
using namespace std;

int dzialanie(int a, int b, char znak) {
    switch(znak) {
        case '+':
            return a +b;
        case '-':
            return a - b;
        case '*':
            return a * b;
        case '/':
            return a / b;
    }
    return 0;
}

bool cyfra(char znak) {
    return znak >= '0' and znak <= '9';
}

bool oper(char znak) {
    return znak == '+' or znak == '-' or znak == '*' or znak == '/';
}

int zamiana(string string, int miejsce) {
    int liczba = 0;

    while(miejsce < string.length() and cyfra(string[miejsce])) {
        liczba = liczba * 10 + string[miejsce] - '0';
        miejsce++;
    }
    miejsce--;
    return liczba;
}

bool poprawnosc(string string) {
    int x = 0;
    for(int i = 0; i < string.length(); i++) {
        if(i % 2 != 0 ) {
            if(string[i] != 32) {
                return false;
            }
        }
        if((string[i] >= 48 and string[i] <= 57) or (string[i] == 32) or (string[i] == 43) or (string[i] == 45) or (string[i] == 42) or (string[i] == 47)) {
            x++;
        }
    }
    if(x != string.length()) {
            return false;
        }
    return true;
}

int main() {
    string ONP;
    do {
        getline(cin, ONP);
    } while(!poprawnosc(ONP));
    stack<int> stos;
    int a, b;
    for(int i = 0; i < ONP.length(); i++) {
        if(cyfra(ONP[i])) {
            stos.push((zamiana(ONP, i)));
        }
        else {
            if(oper(ONP[i])) {
                a = stos.top();
                stos.pop();
                b = stos.top();
                stos.pop();
                stos.push(dzialanie(b, a, ONP[i]));
            }
        } 
    }

    cout<<stos.top();

    return 1;
}

Drzewo poszukiwań binarnych

⎗
✓
#include<iostream>

using namespace std;

template <typename T>
class Drzewo;

template <typename T>
class Element {
    private:
        Element<T> *left_child;
        Element<T> *right_child;
        T wartosc;
    public:
        Element() {}

        Element(T w, Element<T> *l = nullptr, Element<T> *r = nullptr) {
            wartosc = w;
            left_child = l;
            right_child = r;
        }

        ~Element() {}

        friend class Drzewo<T>;
};

template <typename T>
class Drzewo {
    private:
        Element<T> *first;
    public:
        Drzewo() {
            first = nullptr;
        }

        void add(T wartosc) {
            if(first == nullptr) {
                first = new Element<T>(wartosc);
                return;
            }
            Element<T> *curr = first;
            while(true) {
                if(wartosc > curr->wartosc) {
                    if(curr->left_child == nullptr) {
                        curr->left_child = new Element<T>(wartosc);
                        return;
                    }
                    curr = curr->left_child;
                }
                else {
                    if(curr->right_child == nullptr) {
                        curr->right_child = new Element<T>(wartosc);
                        return;
                    }
                    curr = curr->right_child;
                }
            }
        }

        void remove_w(T data) {
            if (first == nullptr) {
                return;
            }

            Element<T> *current = first;
            Element<T> *parent = nullptr;
            while (current != nullptr && current->wartosc != data) {
                parent = current;
                if (data < current->wartosc) {
                    current = current->right_child;
                } else {
                    current = current->left_child;
                }
            }

            if (current == nullptr) {
                return;
            }

            // Element nie ma dzieci

            if (current->left_child == nullptr && current->right_child == nullptr) {
                if (parent == nullptr) {
                    first = nullptr;
                } else {
                    if (data < parent->wartosc) {
                        parent->right_child = nullptr;
                    } else {
                        parent->left_child = nullptr;
                    }
                }
                delete current;
                return;
            }

            // Element ma jedno dziecko

            if (current->left_child == nullptr) {
                if (parent == nullptr) {
                    first = current->right_child;
                } else {
                    if (data < parent->wartosc) {
                        parent->right_child = current->right_child;
                    } else {
                        parent->left_child = current->right_child;
                    }
                }
                delete current;
                return;
            } else if (current->right_child == nullptr) {
                if (parent == nullptr) {
                    first = current->left_child;
                } else {
                    if (data < parent->wartosc) {
                        parent->right_child = current->left_child;
                    } else {
                        parent->left_child = current->left_child;
                    }
                }
                delete current;
                return;
            }

            // Element ma dwa dzieci

            // Znajdujemy następcę usuwanego węzła

            Element<T> *successor = current->right_child;
            Element<T> *successorParent = current;
            while (successor->left_child != nullptr) {
                successorParent = successor;
                successor = successor->left_child;
            }

            // Zastępujemy usuwany węzeł następcą

            if (successorParent == current) {
                current->right_child = successor->right_child;
            } else {
                successorParent->left_child = successor->right_child;
            }

            // Kopiujemy dane następcy do usuwanego węzła

            current->wartosc = successor->wartosc;

            // Usuwamy następcę

            delete successor;
        }

        void preOrder(Element<T> *node) {
            if (node == nullptr) {
                return;
            }
            cout << node->wartosc << " ";
            preOrder(node->left_child);
            preOrder(node->right_child);
        }

        void inOrder(Element<T> *node) {
            if (node == nullptr) {
                return;
            }
            inOrder(node->left_child);
            cout << node->wartosc << " ";
            inOrder(node->right_child);
        }

        void postOrder(Element<T> *node) {
            if (node == nullptr) {
                return;
            }
            postOrder(node->left_child);
            postOrder(node->right_child);
            cout << node->wartosc << " ";
        }

        void preorder_print() {
            preOrder(first);
        }

        void inorder_print() {
            inOrder(first);
        }

        void postorder_print() {
            postOrder(first);
        }

        int longestPath(Element<T> *node) {
            if (node == nullptr) {
                return 0;
            }
            int longestPathLeft = longestPath(node->left_child);
            int longestPathRight = longestPath(node->right_child);
            return max(longestPathLeft, longestPathRight) + 1;
        }

        int shortestPath(Element<T> *node) {
            if (node == nullptr) {
                return 0;
            }
            int shortestPathLeft = shortestPath(node->left_child);
            int shortestPathRight = shortestPath(node->right_child);
            return min(shortestPathLeft, shortestPathRight) + 1;
        }

        void shortest() {
            cout << shortestPath(first) << " ";
        }

        void longest() {
            cout << longestPath(first) - 1 << " ";
        }
};

int main() {

    Drzewo<double> drzewo;

    int k;
    cin >> k;
    for(int i =0; i < k; i++) {
        double wartosc;
        cin >> wartosc;
        drzewo.add(wartosc);
    }

    int l;
    cin >> l;
    for(int i = 0; i < l; i++) {
        double wartosc;
        cin >> wartosc;
        for(int j = 0; j < 10; j++) {
            drzewo.remove_w(wartosc);
        }
    }

    int m;
    cin >> m;
    switch(m) {
        case 0:
            drzewo.preorder_print();
            break;
        case 1:
            drzewo.inorder_print();
            break;
        case 2:
            drzewo.postorder_print();
            break;
    }

    drzewo.longest();
    drzewo.shortest();

    return 1;
}
Edit

Pub: 18 Jan 2024 19:02 UTC

Edit: 21 Jan 2024 20:48 UTC

Views: 41