18
maj
2007
Program C++ obrazujący działanie autorskiej klasy Stos (Stack)
posted by
file under C/C++, Programowanie
Kolejne labolatoria z C++ i kolejna klasa. Tym razem implementacja struktury Stosu.
/* * Grzegorz Bernaś, * http://www.bernas.com.pl * * OS: Ubuntu (6.10) Linux * Powered by Eclipse 3.2 * * Copyright (C) 2007 Grzegorz Bernaś * License http://www.gnu.org/copyleft/gpl.html GNU/GPL * Zgadula is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. */ #ifndef STACK_H_ #define STACK_H_ #define STACK_MAX 10 #include <iostream> using namespace std; class Stack { private: /* Numer najwyższego indexu (jeżeli Top = -1 oznacza, że stos jest pusty */ int Top; /* Elementy stosu */ int Element[STACK_MAX]; void copy(Stack & s); public: /* Konstruktor domyślny */ Stack():Top(-1) {}; /* Konstruktor kopiujacy */ Stack(Stack & s); /* Destruktor */ ~Stack(){}; /* Operator przypisania */ const Stack & operator= (Stack & s); /* Zwraca ostatni element stosu */ int getLast() const; /* Czy stos jest pełny */ bool isFull() const; /* Zwraca liczbę elementów w stosie */ int getTop() const; /* Dodaj element do stosu */ void push(const int data); /* Zdejmij element ze stosu */ void pop(); /* Wyczyść stos */ void clear(); }; #endif /*STACK_H_*/
Download this code: p4/Stack.h
/* * Grzegorz Bernaś, * http://www.bernas.com.pl * * OS: Ubuntu (6.10) Linux * Powered by Eclipse 3.2 * * Copyright (C) 2007 Grzegorz Bernaś * License http://www.gnu.org/copyleft/gpl.html GNU/GPL * Zgadula is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. */ #include "Stack.h" /* Implementacja konstruktora kopiującego */ Stack::Stack(Stack & s) { Top = -1; copy(s); } /* Implementacja operator przypisania (używając pseudo-przypisania) */ const Stack & Stack::operator= (Stack & s) { //cout << "Operator przypisania" << endl; /* Sprawdzamy czy są to dwa takie same obiekty */ if (this != &s) { Top = -1; copy(s); } return *this; } void Stack::copy(Stack & s) { /* numer maksymalnego indeksu */ int i = s.getTop(); /* kopiujemy Nasz stos do bufora */ Stack buffor; for(i; i >= 0; i--) { buffor.push(s.getLast()); s.pop(); } /* Z buffora dane trafiają do stosu kopiowanego oraz do aktualnego stosu */ int i_b = buffor.getTop(); for(i_b; i_b >= 0; i_b--) { push(buffor.getLast()); s.push(buffor.getLast()); buffor.pop(); } } /* Zwraca ostatni element stosu */ int Stack::getLast() const { if(Top == -1) { cout << "Stos jest pusty..."; return -1; } else return Element[Top]; } /* Czy stos jest pełny */ bool Stack::isFull() const { if(Top == STACK_MAX-1) return true; else return false; } /* Zwraca liczbę elementów w stosie */ int Stack::getTop() const { return Top; } /* Dodaj element do stosu */ void Stack::push(const int data) { if(!isFull()) { //cout << "Numer indexu przed dodaniem: " << Top << endl; Top++; Element[Top] = data; //cout << "Numer indexu po dodaniu: " << Top << endl; //cout << "Wartość indexu: " << Element[Top] << endl; //cout << endl; } else cout << "Stos jest pełny!" << endl; } /* Zdejmij element ze stosu */ void Stack::pop() { if(Top <= 0) { Element[0] = 0; Top = -1; //cout << "Stos jest już pusty" << endl; } else { //cout << "Numer indexu przed odjęciem: " << Top << endl; Element[Top] = 0; Top--; //cout << "Numer indexu po odjęciu: " << Top << endl; //cout << "Element zostal zdjęty ze stosu" << endl; //cout << endl; } } /* Wyczyść stos */ void Stack::clear() { Top = -1; cout << "Stos został wyczyszczony" << endl; }
Download this code: p4/Stack.cpp
/* * Grzegorz Bernaś, * http://www.bernas.com.pl * * OS: Ubuntu (6.10) Linux * Powered by Eclipse 3.2 * * Copyright (C) 2007 Grzegorz Bernaś * License http://www.gnu.org/copyleft/gpl.html GNU/GPL * Zgadula is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. */ #include "Stack.h" int main() { cout << "Inicjalizacja dwóch stosów" << endl; Stack stos, stos2; cout << "Dodawanie wartości do [stosu pierwszego]" << endl; stos.push(11); stos.push(22); stos.push(33); stos.push(44); stos.push(55); stos.push(66); stos.push(77); stos.push(88); stos.push(99); stos.push(111); stos.push(122); stos.push(133); cout << "Przypisz [stos pierwszy] do [stosu drugiego]" << endl; stos2 = stos; cout << "Ostatni element [stostu pierwszego]: "; cout << stos.getLast() << endl; cout << "Ostatni element [stosu drugiego]: "; cout << stos2.getLast() << endl; stos.pop(); stos.pop(); stos.pop(); stos.pop(); stos.pop(); Stack nowy(stos); cout << "Wartość ostatniego elementu [stosu pierwszego]: " << stos.getLast() << endl; stos.clear(); cout << "Ostatni element skopiowanego stosu: " << nowy.getLast() << endl; stos.pop(); stos.pop(); stos.pop(); stos.pop(); stos.pop(); stos.pop(); cout << stos.getLast() << endl; }
Download this code: p4/zad4.cc
