Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct Node{
- string obj;
- Node * next;
- };
- class Stack{
- protected:
- Node * head;
- Node * tail;
- public:
- Stack():head(NULL), tail(NULL){};
- bool ifEmpty();
- Stack(const Stack&element);
- void Show_stack();
- void push(string object);
- void pop();
- // bool ifBrackets(char a);
- string GetAnOperator();
- };
- bool Stack :: ifEmpty(){
- if(head==NULL)
- return true;
- return false;
- }
- void Stack:: Show_stack(){
- Node * temp = head;
- while(temp!= NULL){
- cout<<temp->obj;
- temp = temp->next;
- }
- }
- void Stack :: push(string object){
- Node * temp = new Node;
- temp->obj = object;
- if(head!=NULL){
- temp->next = head;
- head = temp;
- }
- else{
- head = temp;
- tail = temp;
- temp->next = NULL;
- }
- delete temp;
- }
- void Stack::pop (){
- Node * temp = head;
- head = head->next;
- delete temp;
- }
- Stack::Stack(const Stack&element){
- Node * temp = element.head;
- while(temp){
- push(temp->obj);
- temp = temp->next;
- }
- }
- class Queue{
- protected:
- Node * head;
- Node * tail;
- public:
- Queue():head(NULL), tail(NULL){};
- Queue(const Queue&element);
- void Show_queue();
- void push_queue(string obj);
- void pop_queue();
- };
- void Queue :: Show_queue(){
- Node * temp = head;
- while(temp!= NULL){
- cout<<temp->obj;
- temp = temp->next;
- }
- }
- void Queue :: push_queue(string object){
- Node * temp = new Node;
- temp->obj = object;
- if(head!=NULL){
- tail->next = temp;
- tail = temp;
- }
- else{
- tail = temp;
- head = temp;
- temp->next = NULL;
- }
- delete temp;
- }
- void Queue :: pop_queue(){
- Node * temp = head;
- head = head->next;
- delete temp;
- }
- Queue :: Queue(const Queue&element){
- Node * temp = element.head;
- while(temp){
- push_queue(temp->obj);
- temp = temp->next;
- }
- }
- bool ifOperator(char a){
- if (a == '(' || a == '+' || a=='-' || a=='{' || a=='[' || a=='*' || a=='/' || a=='^' || a == ')' || a == ']' || a == '}' )
- return true;
- else return false;
- }
- int priority(string a){
- int prior = 0;
- if (a == "^")
- prior = 4;// приоритет текущего оператора
- else if (a == "*" || a == "/")
- prior = 3;
- else if (a == ")" || a == "]" || a == "}")
- prior = 2;
- else if (a == "+" || a == "-")
- prior = 1;
- else if (a == "(" || a == "[" || a == "{")
- prior = 0;
- return prior;
- }
- string Stack :: GetAnOperator(){
- return head->obj;
- }
- void PolishNotation (Stack & object, Queue & element,char a){
- if(ifOperator(a) == true){
- string str(1,a);
- int prior = priority(str);//вычисляем приоритет текущего оператора
- if(object.ifEmpty() != true){//если стек не пустой, то проверяем операторы на выход
- while(priority(object.GetAnOperator()) >= prior){
- if(object.GetAnOperator() != "(" || object.GetAnOperator() != "["
- || object.GetAnOperator() != "{" || object.GetAnOperator() != ")"
- || object.GetAnOperator() != "]" || object.GetAnOperator() != "}")
- element.push_queue(object.GetAnOperator());// добавляем в очередь
- object.pop();//убираем из стека
- }
- }
- if(str!= ")"|| str != "]" || str != "}")
- object.push(str);//кладем в стек текущий элемент
- }
- else{
- string str (1,a);
- element.push_queue(str);
- }
- }
- int main(){
- Stack operatory;
- string expression;
- getline(cin,expression);
- Queue express;
- for(int i =0; i<expression.size(); i++){
- PolishNotation(operatory,express,expression[i]);
- }
- express.Show_queue();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement