Advertisement
myname0

стек

May 18th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include "exception.cpp"
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. template <class X>
  8. class Stack
  9. {
  10.     struct Element
  11.     {
  12.         X inf;
  13.         Element *next;
  14.         Element (X x, Element *p): inf(x), next(p)
  15.         {
  16.         }
  17.     };
  18.     Element *head;
  19. public:
  20.     Stack():head(0)
  21.     {
  22.     }
  23.     bool Empty()
  24.     {
  25.         return head == 0;
  26.     }
  27.     X Pop()
  28.     {
  29.         if (Empty())
  30.         {
  31.             throw StackException("StackException: Pop - стек пуст");
  32.         }
  33.         else
  34.         {
  35.             Element *r = head;
  36.             X i = r->inf;
  37.             head = r->next;
  38.             delete r;
  39.             return i;
  40.         }
  41.     }
  42.     void Push(X data)
  43.     {
  44.         head = new Element(data, head);
  45.     }
  46.     X Top()
  47.     {
  48.         if (Empty())
  49.         {
  50.             throw StackException("StackException: Top - стек пуст");
  51.         }
  52.         else
  53.         {
  54.             return head->inf;
  55.         }
  56.     }
  57. };
  58.  
  59. int main()
  60. {
  61.     Stack <int> t, t1;
  62.     int i;
  63.     ifstream in ("input.txt");
  64.     ofstream out ("output.txt");
  65.     while (in >> i)
  66.     {
  67.         t.Push (i);
  68.     }
  69.     in.close();
  70.     while(!t.Empty())
  71.     {
  72.         i = t.Pop();
  73.         if(i%2 == 0) t1.Push(2*i);
  74.         else t1.Push(i);
  75.     }
  76.     while (!t1.Empty())
  77.     {
  78.         out << t1.Pop() << " ";
  79.     }
  80.     out.close();
  81.     return 0;
  82. }
  83. //срр
  84. #include <exception>
  85. #include <string>
  86.  
  87. using namespace std;
  88.  
  89. class StackException: public exception
  90. {
  91. public:
  92.     StackException(const string & message=""): exception(message.c_str())
  93.     {
  94.     }
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement