Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "exception.cpp"
- #include <iostream>
- #include <fstream>
- using namespace std;
- template <class X>
- class Stack
- {
- struct Element
- {
- X inf;
- Element *next;
- Element (X x, Element *p): inf(x), next(p)
- {
- }
- };
- Element *head;
- public:
- Stack():head(0)
- {
- }
- bool Empty()
- {
- return head == 0;
- }
- X Pop()
- {
- if (Empty())
- {
- throw StackException("StackException: Pop - стек пуст");
- }
- else
- {
- Element *r = head;
- X i = r->inf;
- head = r->next;
- delete r;
- return i;
- }
- }
- void Push(X data)
- {
- head = new Element(data, head);
- }
- X Top()
- {
- if (Empty())
- {
- throw StackException("StackException: Top - стек пуст");
- }
- else
- {
- return head->inf;
- }
- }
- };
- int main()
- {
- Stack <int> t, t1;
- int i;
- ifstream in ("input.txt");
- ofstream out ("output.txt");
- while (in >> i)
- {
- t.Push (i);
- }
- in.close();
- while(!t.Empty())
- {
- i = t.Pop();
- if(i%2 == 0) t1.Push(2*i);
- else t1.Push(i);
- }
- while (!t1.Empty())
- {
- out << t1.Pop() << " ";
- }
- out.close();
- return 0;
- }
- //срр
- #include <exception>
- #include <string>
- using namespace std;
- class StackException: public exception
- {
- public:
- StackException(const string & message=""): exception(message.c_str())
- {
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement