Home > Back-end >  For help: the Stack class Stack, combined statement and implement a PostfixEvaluation class, for the
For help: the Stack class Stack, combined statement and implement a PostfixEvaluation class, for the

Time:09-20

[description]
Stack can be applied to an arithmetic expression evaluation, here in the following simplified arithmetic expressions do: operator as +, -, *,/, %; The operands to a single digital nonnegative integer (0 ~ 9),
For example: (2 * 3 + 5) - 8/3
Infix expression, according to the form of arithmetic expressions also because each operator appears between the two operands, the compiler evaluate arithmetic expressions, tend to convert infix expressions to postfix expression, postfix expression, also known as reverse polish expression, by polish mathematician Jan Lukasiewicz invention, refers to the operator appears behind the operands of arithmetic expressions without parentheses,
Infix expressions: a + b * c, the postfix expression is: a, b + c * and * higher priority than + because it is over, so first calculate the b * c, namely c * b; + operand is a and b c *, namely a c * b +,
Infix expression (a + b) * c, the postfix expression is: a b + c *, first calculate a + b in parentheses, namely a, b +. * of the operand is a + b and c, namely a b + c *,
Infix expression (a * b + c)/d + e, the postfix expression is: a b * c + d/e +, first calculate the parentheses a * b + c, namely a, b * c +./the operand is a and b * c + d, namely a b * c + d/; + operand is a b * c + d/a and e, namely a b * c + d/e +,
Postfix expression is evaluated using a deposit operand stack, the process of evaluation is sequential scans the postfix expression, every time I encounter operand will it into the stack; When you meet the operators from the two operands to calculate pops up in the stack, and the results into the stack, to the end of the scanning, stay at the top of the stack the value of the operand is demanding arithmetic expression
For example:
2 * 7 + 4
Sequence as shown in figure 1 below, read operation number 4,2,7, incorporated into the stack, as shown in figure 2, read the operator *, operand 7, 2 out of the stack, calculate 2 * 7, the results of 14 into stack, shown in the diagram below, read the operators +, operands 14, 4 out of the stack, calculate 4 + 14, results of 18 into stack, after scanning the postfix expression, the only element in the stack is the final result,


Please mix the Stack class Stack, declaration and realize a PostfixEvaluation class, the value of the postfix expression, PostfixEvaluation classes include:
Of type string postfixExpression private data members, depositing postfix expression,
OperandStack Stack type of private data members, to store the operands (single digital non-negative integer),
Private member function getOperands, straight from the top of stack two operands,
Private member function to calculate, stack calculation result of two operands, and the results into the stack,
Private member function isOperator, determine the input operator is a valid operator (+, -, *,/, %)
A constructor, create a postfix expression object,
GetPostfixExpression accessor function, and obtain the postfix expression,
Change the device function setPostfixExpression, set up the new postfix expression,
Member function evaluate, calculate, and returns the value of postfix expression,
PostfixEvaluation class is as follows:
The class PostfixEvaluation {
Public:
PostfixEvaluation (const string& amp; PostfixExpression);//the constructor
StringgetPostfixExpression () const;//get the postfix expression
Void setPostfixExpression (const string& amp; PostfixExpression);//set the postfix expression
Int the evaluate ();//calculates and returns the postfix expression values
Private:
String postfixExpression;//store to compute the postfix expression of
Stack& lt; Int& gt; OperandStack;//hold operands
Void getOperands (int & amp; amp; Left, int& amp; Right);//count the operation stack
Int calculate (int left, int right, char op) const;//ask operand operation value
Bool isOperator const char (ch);//whether the operator
};
[enter]
Enter a postfix expression,
[output]
Output the value of the postfix expression,
[example 1 input]
2 * 7 + 4
[example 1] output
18
[example 2 input]
8 4 0% +
[example 2] output
Divisor always be zero!
[input sample 3]
2 ^ 4 + 3
[output sample 3]
Illegal input!
[input example 4]
4 2/*
[output example 4]
Too many operators!
[input example 5]
1 2 3 +
[output example 5]
Too many operands!


 # include & lt; Iostream> 
#include
#include
using namespace std;
The template & lt; Typename T>
Class Vector {
Public:
The Vector (int size);//the constructor
The Vector (int size, const T& Value);//the constructor
The Vector (const Vector & V);//copy constructor
Virtual ~ Vector ();//destructors
Const Vector & Operator=(const Vector & Right);//overloaded assignment operator
T& Operator [] (int index);//overloaded subscript operator
T operator [] (int index) const;//overloaded subscript operator
Const int getSize ();
Void the resize (int size);
Private:
T * pVector;//pointer to storage array element of dynamically allocated memory space
Int size;//array length
};
The template & lt; Typename T>
Vector : : Vector (int size) {
If (size & gt; 0)
This - & gt; Size=size;
The else
Throw invalid_argument (" the length of the array must be positive integer!" );
PVector=new T [size];
}
The template & lt; Typename T>
Vector : : Vector (int size, const T& Value) {
If (size & gt; 0)
This - & gt; Size=size;
The else
Throw invalid_argument (" the length of the array must be positive integer!" );
PVector=new T [size];
for (int i=0; i PVector [I]=value;
}
The template & lt; Typename T>
Vector : : Vector (const Vector & V) {
Size=v. considering;
PVector=new T [size];
for (int i=0; i PVector [I]=Valerie plame Vector [I];
}
The template & lt; Typename T>
Vector : : ~ Vector () {
The delete [] pVector;
}
The template & lt; Typename T>
Const Vector & Vector : : operator=(const Vector & Right) {
If (this!=& amp; Right) {
If (the size!=right. The size) {
The delete [] pVector;
Size=right. The size;
PVector=new T [size];
}
for (int i=0; i PVector [I]=right. PVector [I];
}
return *this;
}
The template & lt; Typename T>
T& Vector : : operator [] (int index) {
If (index & lt; 0 | | index & gt; The size 1)
Throw out_of_range (" array subscript is beyond the scope allowed!" );
Return pVector [index];
}
The template & lt; Typename T>
T Vector : : operator [] (int index) const {
If (index & lt; 0 | | index & gt; The size 1)
Throw out_of_range (" array subscript is beyond the scope allowed!" );
Return pVector [index];
}
The template & lt; Typename T>
Int Vector : : getSize () const {
Return the size;
}
The template & lt; Typename T>
Void Vector : : resize (int size) {
If (size & gt; 0 {
If (this - & gt; The size!={
the size)T * old=pVector;
PVector=new T [size];
Int newSize=(this - & gt; The size & gt; The size)? Size: this - & gt; The size;
for (int i=0; i nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related