In: Computer Science
Please construct a code following the instructions below
Part 3 – infixEvaluator method
You are required to implement the following method:
public static double infixEvaluator(String line)
This function first needs to tokenize the input expression (in the form of a String and stored in input variable “line”). You can tokenize the input expression by creating an object of StringSplitter and passing it the String as follows. StringSplitter uses a queue to accomplish the tokenization (see the class for details).
StringSplitter data = new StringSplitter(line);
Next, create your two stacks. Remember one will contain the operators and the other one will include the operands. Define them as follows:
Stack<String> operators = new Stack<String>();
Stack<Double> operands = new Stack<Double>();
Before we continue further in this method, it will be helpful to consider helper methods we might need (which will make our job easier :-).
Skeleton of the code below:
/**
* give a description of the purpose of this method
* @param line fill in
* @return fill in
*/
public static double infixEvaluator(String line){
return 0.0; // placeholder
}
Hello There,
Code for the above probem is given as :-
import
java.util.Stack;
public
static
double infixEvaluator(String
line)
{
char
[]
tokenized = line.toCharArray();
//
to store operands..
Stack<Double>
operands =
new
Stack<Double>();
//
to store operators..
Stack<Character>
operators =
new
Stack<Character>();//taking of Character type as it is
converted to character array above..
for
(
int
i =
0
; i < tokenized.length;
i++)
{
//
if a current character is a whitespace just
skip..
if
(tokenized[i] ==
'
'
)
continue
;
//
if a current character is a number just push it to stack of
operators..
if
(tokenized[i] >=
'0'
&&
tokenized[i] <=
'9'
)
{
StringBuffer
S_B =
new
StringBuffer();
//
it is possible that it contains more than single digit
numbers..
while
(i < tokenized.length && tokenized[i] >=
'0'
&& tokenized[i] <=
'9'
)
S_B.
append(tokenized[i]);
i=i+1;
operators
.push(Double.parseDouble(S_B.toString()));
//converting each character to Double because calculations done in
Double values..
}
//
if an opening bracket is encountered push it to
operands...
else
if
(tokenized[i] ==
'('
)
operands.push(tokenized[i]);
//
For a closing bracket the whole brace is needed to be
solved..
else
if
(tokenized[i] ==
')'
)
{
while
(operands.peek() !=
'('
)
operators
.push(helper(operands.pop(),
operators.pop(), operators.pop()));
operands.pop();
}
//
for operators we have : -
else
if
(tokenized[i] ==
'+'
|| tokenized[i] ==
'-'
||
tokenized[i] == '*'
|| tokenized[i] ==
'/'
)
{
//
Now until top of operands has same or greater precedence than
current
//
character that is operator. Solve top two of operators as according
to
//
top of operands ..
while
( check_precedence(tokenized[i], operands.peek()) &&
!operands.empty() )
operators
.push(helper(operands.pop(),
operators.pop(), operators.pop()));
//
Push current character to operands...
operands.push(tokenized[i]);
}
}
//
After parsing till here apply the available operands to remaining
operators
...
while
(!operands.empty())
operators
.push(helper(operands.pop(),
operators.pop(), operators.pop()));
// return the top of operators as an final
answer...
return
operators.pop();
}
// this
check_precedence function gives true if operand_2 has higher or
same precedence as of operand_1 ,..
// otherwise it
simply returns false.
public
static
boolean
check_precedence(
char
op1,
char
op2)
{
if
((operand_1 ==
'/'
|| operand_1 ==
'*'
) && (operand_2 ==
'+'
|| operand_2 ==
'-'
))
return
false
;
if
(operand_2 ==
'('
|| operand_2 ==
')'
)
return
false
;
else
return
true
;
}
// an helper
function to calculate result of top two
operators..
public
static
Double helper(
char
operand, Double
x, Double
y)
{
if(
operand=='*')
return
x * y;
else if(operand=='+')
return
x + y;
else if(operand=='-')
return
x - y;
else if(operand=='/')
{
if
(y
==
0
)
{
throw
new
UnsupportedOperationException(
"divide
by zero exception occur.."
);
}
return
a / b;
}
return
0.0
; //placeholder
}
This is the required code to solve the infix experession using two stacks...for any further doubt you can ask again...
Thanks for reaching out to us..
keep asking and keep learning... :)))))