Bob103

C++_(for Nig**)_2

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