Question
Write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain methods to
Write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain methods to recognize and evaluate prefix expressions. I need help creating this program with the given algorthms I have from the book.
isPre()
size = length of expression strExp
lastChar = endPre(0, size 1)
if (lastChar >= 0 and lastChar == size-1 {
return true
}
else {
return false
}
evaluatePrefix(strExp)
ch = first character of expression strExp
Delete first character from strExp
if (ch is an identifier) {
return value of the identifier
}
else if (ch is an operator named op) {
operand1 = evaluatePrefix(strExp)
operand2 = evaluatePrefix(strExp)
return operand1 op operand2
}
endPre( in first:integer, in second:integer):integer
if (first < 0 || first > last )
{
return -1
}
ch = character at position first of strExp
if ( ch is an identifier)
{
//index of last character in simple prefix expression
return first;
}
else if(ch is an operator )
{
//find the end of the first prefix expression
firstEnd = endPre (first +1, last)
//If the end of the first expression was found
//If the end of the second prefix expression
if(firstEnd > -1)
{
return endPre(firstEnd +1, last)
}
else
{
return -1
}
else
{
return -1
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started