• Infix to postfix conversion 中缀表达式转换为后缀表达式


    Conversion Algorithm

    1、操作符栈压入"#";

    2、依次读入表达式的每个单词;

    3、如果是操作数则压入操作数栈;

    4、如果是操作符,则将操作符栈顶元素与要读入的操作符进行优先级比较

    (4.1)如果读入的是 ')',则将操作符栈中的元素压入操作数栈直至遇到 '(';

    (4.2)如果读入的是 '(',压入操作符栈;

    (4.3)如果栈顶元素优先级低,压入操作符栈;

    (4.4)如果读入的元素不为'#',以及栈顶元素优先级高,则将栈顶元素压入操作数栈,将读入的元素压入操作符栈;

    (4.5)如果操作符栈为空或操作符栈栈顶元素为 '(',压入操作符栈;

    (4.6)其他,将操作符栈元素压出到操作数栈直至遇到'#';

    5.将操作数栈元素逆序输出。

    //infix to postfix
    #include<iostream>
    #include<vector>
    using namespace std;
    
    struct Node
    {
    	char data;
    	Node* next;
    };
    
    class LinkStack
    {
    public:
    	LinkStack()
    	{
    		top = new Node;
    		top = NULL;
    	}
    	~LinkStack()
    	{
    		delete top;
    	}
    	void push(char item);
    	void pop();
    	char front() const;
    	void display()const;
    private:
    	Node*top;
    };
    
    void LinkStack::display()const
    {
    	Node*p = top;
    	vector<char>s = {};
    	while (p != NULL)
    	{
    		s.push_back(p->data);
    		p = p->next;
    	}
    	vector<char>s1 = {};
    	vector<char>::size_type size = s.size();
    	for (vector<char>::size_type i = 0; i < size; i++)
    		s1.push_back(s[size - 1 - i]);
    	for (auto i : s1)
    		cout << i;
    	cout << endl;
    }
    
    void LinkStack::push(char item)
    {
    	Node*p = new Node;
    	p->data = item;
    	p->next = top;
    	top = p;
    }
    
    void LinkStack::pop()
    {
    	Node*p = top;
    	top = top->next;
    	delete p;
    }
    
    char LinkStack::front()const
    {
    	return top->data;
    }
    
    bool isNum(char c)
    {
    	return (c <= '9' && c >= '0');
    }
    
    char Precede(char f, char c)
    {
    	if (f == '+')
    	{
    		if (c == '*' || c == '/' || c == '(')return '<';
    		else return '>';
    	}
    	else if (f == '-')
    	{
    		if (c == '*' || c == '/' || c == '(')return '<';
    		else return '>';
    	}
    	else if (f == '*')
    	{
    		if (c == '(')return '<';
    		else return'>';
    	}
    	else if (f == '/')
    	{
    		if (c == '(')return '<';
    		else return'>';
    	}
    	else if (f == '(')
    	{
    		if (c == ')')return '=';
    		else return '<';
    	}
    	else if (f == ')')return '>';
    	else if (f == '#')
    	{
    		if (c == '#')return '=';
    		else return '<';
    	}
    }
    
    void evaluate(LinkStack*SOPTR, LinkStack*SOPND)
    {
    	SOPTR->push('#');
    	char c;
    	while (cin >> c)
    	{
    		if (isNum(c))
    		{
    			SOPND->push(c);
    		}
    		else
    		{
    			if (c == ')')
    			{
    				char c1 = SOPTR->front();
    				while (c1 != '(')
    				{
    					SOPND->push(c1);
    					SOPTR->pop();
    					c1 = SOPTR->front();
    				}
    				SOPTR->pop();
    			}
    			else if (c == '(')
    				SOPTR->push(c);
    			else if (Precede(SOPTR->front(), c) == '<')
    			{
    				SOPTR->push(c);
    			}
    			else if (c!='#'&&Precede(SOPTR->front(), c) == '>')
    			{
    				char cha = SOPTR->front();
    				SOPTR->pop();
    				SOPTR->push(c);
    				SOPND->push(cha);
    			}
    			else if (SOPTR->front() == '#' || SOPTR->front() == '(')
    			{
    				SOPTR->push(c);
    			}
    			else
    			{
    				char ch = SOPTR->front();
    				while (ch != '#')
    				{
    					SOPND->push(ch);
    					SOPTR->pop();
    					ch = SOPTR->front();
    				}
    			}
    		}
    	}
    	SOPND->display();
    	delete SOPND, SOPTR;
    }
    
    int main()
    {
    	cout << "input the infix expression:(you must input # to stop input)" << endl;
    	LinkStack* SOPTR = new LinkStack;
    	LinkStack* SOPND = new LinkStack;
    	evaluate(SOPTR, SOPND);
    
    	return 0;
    }
    

      

  • 相关阅读:
    6-5 函数
    6-2 触发器
    4、MongoDB学习之备份还原
    3、MongoDB学习之固定集合
    2、MongoDB学习之索引的管理
    1、MongoDB学习之基本操作
    JS-01
    | 和 ||,& 和 && 的区别
    正则
    设计测试用例的经验总结
  • 原文地址:https://www.cnblogs.com/KennyRom/p/5918626.html
Copyright © 2020-2023  润新知