Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1 zadacha
- #include <iostream>
- #include <cstring>
- using namespace std;
- //const int MAX = 100;
- class Stack
- {
- int current; //индекс на върха на стека
- int capacity; //колко елемента има в стека
- int * elements; //елементите на стека
- void copyStack(const Stack& other)
- {
- current = other.current;
- capacity = other.capacity;
- elements = new int[capacity];
- for(int i = 0; i < capacity; i++)
- elements[i] = other.elements[i];
- }
- void deleteStack()
- {
- delete [] elements;
- }
- void resize()
- {
- int newSize = capacity*2;
- int* helper = new int [capacity];
- for (int i = 0; i < capacity; i++)
- {
- helper[i] = elements[i];
- }
- delete[] elements;
- elements = new int[newSize];
- for (int i = 0; i < capacity; i++)
- {
- elements[i] = helper[i];
- }
- capacity = newSize;
- delete[] helper;
- }
- public:
- Stack()
- {
- current = -1;
- capacity = 10;
- elements = new int[capacity];
- }
- Stack(const Stack& other)
- {
- copyStack(other);
- }
- Stack& operator=(const Stack& other)
- {
- if(this != &other)
- {
- deleteStack();
- copyStack(other);
- }
- return *this;
- }
- bool isEmpty()
- {
- return current < 0;
- }
- bool isFull()
- {
- return current == capacity - 1;
- }
- int numOfElements()
- {
- return current+1;
- }
- void push(int element)
- {
- if(isFull())
- resize();
- elements[++current] = element;
- }
- void pop()
- {
- if(!isEmpty())
- {
- current--;
- capacity--;
- }
- else
- cout << "The stack is empty!";
- }
- int peek() //top()
- {
- if(!isEmpty())
- return elements[current];
- else
- cout << "The stack is empty.";
- }
- friend ostream& operator<<(ostream& os, const Stack& s)
- {
- for (int i = 0; i < s.capacity; i++)
- {
- os << s.elements[i] << " ";
- }
- return os;
- }
- };
- int calculate(char str[])
- {
- Stack st;
- int n = strlen(str);
- for(int i = 0; i < n; )
- {
- if(str[i] == ' ') i++;
- if(str[i] >= '0' && str[i] <= '9')
- {
- st.push(str[i] - 48);
- i++;
- }
- else if(str[i] == '+')
- {
- if(st.numOfElements() < 2) return -1;
- else
- {
- int x = st.peek();
- st.pop();
- int y = st.peek();
- st.pop();
- int z = x + y; //cout << z << endl;
- st.push(z);
- i++;
- }
- }
- else if(str[i] == '-')
- {
- if(st.numOfElements() < 2) return -1;
- else
- {
- int x = st.peek();
- st.pop();
- int y = st.peek();
- st.pop();
- int z = y - x; //cout << z << endl;
- st.push(z);
- i++;
- }
- }
- else if(str[i] == '*')
- {
- if(st.numOfElements() < 2) return -1;
- else
- {
- int x = st.peek();
- st.pop();
- int y = st.peek();
- st.pop();
- int z = x * y; //cout << z << endl;
- st.push(z);
- i++;
- }
- }
- else if(str[i] == '/')
- {
- if(st.numOfElements() < 2) return -1;
- else
- {
- int x = st.peek();
- st.pop();
- int y = st.peek();
- st.pop();
- if(x == 0) return -1;
- else
- {
- int z = y / x; //cout << z << endl;
- st.push(z);
- i++;
- }
- }
- }
- else if(str[i] == '%')
- {
- if(st.numOfElements() < 2) return -1;
- else
- {
- int x = st.peek();
- st.pop();
- int y = st.peek();
- st.pop();
- if(x == 0) return -1;
- else
- {
- int z = y % x; //cout << z << endl;
- st.push(z);
- i++;
- }
- }
- }
- }
- if(st.numOfElements() == 1)
- return st.peek();
- else return -1; // wrong input
- }
- int main()
- {
- char expr[101];
- cin.getline(expr,101);
- cout << calculate(expr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement