Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Z3.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- class Stos
- {
- int *tab;
- int rozmiar, wierzcholek;
- public:
- //inicjacja stosu, który jest tablica int
- void init(int x)
- {
- tab = new int[x];
- wierzcholek = -1;
- rozmiar = x;
- }
- //metoda usuwajaca tablice z pamieci
- void destroy()
- {
- delete tab;
- }
- //metoda umieszczajaca liczbe na tablica
- void push(int y)
- {
- if (!full()) tab[++wierzcholek] = y;
- else cout << "Stos jest pelny, nic nie moge dopisac!" << endl;
- }
- //metoda zdejmujaca liczbe z wierzcholka
- void pop()
- {
- --wierzcholek;
- }
- //metoda zwracajaca liczbe z wierzcholka stosu
- int top()
- {
- if (empty())
- {
- cout << "Nie moge przeczytac liczby, bo stos jest pusty!" << endl;
- }
- return tab[wierzcholek];
- }
- //metoda zwracajaca wartosc 1 dla pustego stosu lub 0 dla nie pustego
- int empty()
- {
- if (wierzcholek == -1) return 1;
- else return 0;
- }
- //metoda zwracajaca wartosc 1 dla stosu pelnego lub 0 dla niepelnego
- int full()
- {
- if (wierzcholek == (rozmiar - 1)) return 1;
- else return 0;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Stos s1, s2;
- int maksymalny_rozmiar = 10;
- s1.init(maksymalny_rozmiar);
- s2.init(maksymalny_rozmiar);
- for (int i = 1; i <= maksymalny_rozmiar; i++)
- {
- char bufor[50];
- int liczba;
- cout << "Podaj " << i << " liczbe: ";
- cin >> bufor;
- s1.push(liczba = atoi(bufor));
- }
- int licznik = maksymalny_rozmiar;
- while (!s1.empty())
- {
- s2.push(s1.top());
- s1.pop();
- cout << licznik-- << " liczba to: " << s2.top() << endl;
- }
- while (!s1.full())
- {
- s1.push(s2.top());
- s2.pop();
- }
- s1.destroy();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment