How to evaluate a template stack and an infix expression using C ++

Source: Internet
Author: User
Tags int size

The stack is directly implemented using a linked list. This is relatively simple, not to mention, but the error detection of IDE is not very powerful for C ++ writing programs.

For a given infix expression, how to directly evaluate without converting it into a suffix expression is to use two stacks, one operator stack, one operand stack, and then scan the expression from left to right, here, the computation of the fix expression is simple and incomplete. You can extend it. Stack implementation is what I want to write. The idea is as follows:

1. How is the operand pushed to the operand stack?

2. If it is an operator, press it into the operator stack.

3. Ignore the left brackets.

4. If there are parentheses, the top element of the operator stack will pop up, and two elements of the operand stack will pop up. After the operation, the result will be pushed to the operand stack.

Just look at a picture.



Finally, the stack top implementation code is provided.

# Include "stdafx. h"

Pragma region Node definition

Template <class T>
Class Node
{
Template <class T>
Friend class Stack;
Private:
T m_data;
Node * pNextNode;
Public:
Node ();
Node (T d );
};

Template <class T>
Node <T>: Node ()
{
M_data = default (T );
PNextNode = NULL;
}
Template <class T>
Node <T>: Node (T d)
{
M_data = d;
PNextNode = NULL;
}
Pragma endregion
Pragma region Stack definition

Template <class T>
Class Stack
{

Private:
Node <T> * m_pTopNode;
Int m_nNodeCount;
Public:
Stack ();
~ Stack ();
Bool IsEmpty ();
Bool Push (T e );
T Pop ();
Int Size ();
};

Template <class T>
Stack <T>: Stack (): m_pTopNode (NULL), m_nNodeCount (0)
{
}

Template <class T>
Stack <T> ::~ Stack ()
{
While (! IsEmpty ())
{
Node <T> * pTempNode = m_pTopNode;
M_pTopNode = m_pTopNode-> pNextNode;
Delete (pTempNode );
PTempNode = NULL;
}
M_nNodeCount = 0;
M_pTopNode = NULL;
}

Template <class T>
Bool Stack <T >:: IsEmpty ()
{
Return (m_pTopNode = NULL );
}

Template <class T>
Bool Stack <T>: Push (T e)
{
Node <T> * pNewNode = new Node <T> (e );
If (NULL = pNewNode ){
Return false;
}

If (! IsEmpty ()){
PNewNode-& gt; pNextNode = m_pTopNode;
}
M_pTopNode = pNewNode;
M_nNodeCount ++;

Return true;

}

Template <class T>
T Stack <T>: Pop ()
{
If (IsEmpty ()){
Return T (-1 );
}
T e;
E = m_pTopNode-> m_data;
Node <T> * pNode = m_pTopNode;
M_pTopNode = m_pTopNode-> pNextNode;
Delete (pNode );
M_nNodeCount -;

Return e;

}

Template <class T>
Int Stack <T>: Size ()
{
Return m_nNodeCount;
}
Pragma endregion

Then the main function code

Int _ tmain (int argc, _ TCHAR argv [])
{
String str;
Str = "(1 + (2 + 3) (45 )))";
For (int I = 0; I <str. length (); I ++)
    {
Char s = str [I];
If (s = '(');
Else if (s = '+') ops. Push (s );
Else if (s = '') ops. Push (s );
Else if (s = ')')
        {
Char op = ops. Pop ();
If (op = '+ ')
Vals. Push (vals. Pop () + vals. Pop ());
Else if (op = '')
Vals. Push (vals. Pop () vals. Pop ());
        }
Else
Vals. Push (s-'0 ');
    }
Cout <(vals. Pop ());
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.