Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #define DIM 20000
- using namespace std;
- template <class T>
- class Pila {
- private:
- T VettorePila[DIM];
- T cima;
- public:
- Pila(); //Costruttore della pila
- void Push(T elemento); //Inserisce un elemento nella pila
- T Pop(); //Estrae un elemento dalla pila
- void StampaPila();
- bool PilaVuota(); // Verifica se la pila è piena
- bool PilaPiena(); // Verifica se la pila è vuota
- };
- template <class T>
- Pila<T>::Pila(void)
- : cima(-1){};
- template <class T>
- bool Pila<T>::PilaPiena()
- {
- if (cima == (DIM - 1))
- return 1;
- else
- return 0;
- }
- template <class T>
- bool Pila<T>::PilaVuota()
- {
- if (cima == -1)
- return 1;
- else
- return 0;
- }
- template <class T>
- void Pila<T>::Push(T x)
- {
- if (PilaPiena()) {
- cerr << "Stack Overflow" << '\n';
- exit(1);
- }
- else {
- cima++;
- VettorePila[cima] = x;
- }
- }
- template <class T>
- T Pila<T>::Pop()
- {
- T x;
- if (PilaVuota()) {
- cerr << "Stack Underflow" << '\n';
- exit(1);
- }
- else {
- x = VettorePila[cima];
- cima--;
- return x;
- }
- }
- template <class T>
- void Pila<T>::StampaPila()
- {
- if (PilaVuota()) {
- cout << "Pila vuota!" << endl;
- }
- else {
- for (int i = cima; i >= 0; i--) {
- cout << VettorePila[i] << " ";
- }
- }
- }
- int main()
- {
- freopen("input(1).txt", "r", stdin);
- // freopen("output.txt","w",stdout);
- string line;
- int N;
- while (cin >> line >> N) {
- /* questa funziona
- if ( line == "int") {
- int num;
- Pila <int>p;
- for (size_t i = 0; i < N; i++) {
- cin >> num;
- //cout << num << " ";
- p.Push(num);
- }
- p.StampaPila();
- }else if ( line == "bool") {
- bool num;
- Pila <bool>p;
- for (size_t i = 0; i < N; i++) {
- cin >> num;
- p.Push(num);
- }
- p.StampaPila();
- }else if ( line == "char") {
- char num;
- Pila <char>p;
- for (size_t i = 0; i < N; i++) {
- cin >> num;
- p.Push(num);
- }
- p.StampaPila();
- }
- */
- if (line == "double") {
- double num;
- Pila<double> p;
- for (size_t i = 0; i < N; i++) {
- cin >> num;
- p.Push(num);
- }
- p.StampaPila();
- }
- cout << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement