How Does Operator Precedence Impact Infix to Postfix Conversion?

  • Thread starter jsmith0476
  • Start date
In summary: Expert SummaryIn summary, a method for the PostfixConversion class needs to be written. The method, called precedence, determines the precedence between two operators (+, -, *, or /). It checks if the first operator has a higher or equal precedence compared to the second operator, returning true if it does and false otherwise. The suggested solution involves using a switch statement, an array or list, and a hashmap for more flexibility and efficiency. The code should also be able to handle multiple levels of precedence.
  • #1
jsmith0476
6
0

Homework Statement



I need to write a method for the PostfixConversion class:

public static boolean precedence(char first, char second)

The precedence method determines the precedence between two operators. (here, they are either '+', '-', '*', or '/') If the first operator is of higher or equal precedence than the second operator, it returns the value true; otherwise, it returns the value false. (for instance, if the new operator is '+' or '-', then all operators (+,-,*,/) have precedence greater than or equals to the operator. If the new operator is '*' or '/', then only the operators '*' and '/' have precedence greater than or equals to the operator.)



Homework Equations





The Attempt at a Solution



public static boolean precedence(char first, char second)
{
int char1Index=0;
int char2Index=0;
String precedence = "-+*/";
for(int i=0; i<precedence.length(); i++)
{
if(first==precedence.charAt(i)) char1Index=i;
if(second==precedence.charAt(i)) char2Index=i;
}
if(((char1Index==0)||(char1Index==1)) && (char2Index>1)) return false;
else return true;
}

I feel like something is messed up. Let me know if I am making any mistakes. I didn't really understand what I was supposed to be doing. This was my best guess.
 
Physics news on Phys.org
  • #2





Thank you for your post. Your attempt at the solution is on the right track, but there are a few things that could be improved upon. Here are some suggestions:

1. Instead of using an if statement for each operator, you could use a switch statement to check the operator and assign the correct index. This would make the code more efficient and easier to read.

2. Your code will only work for the four operators mentioned in the problem statement (+, -, *, /). It would be better to make it more general so that it can handle any operator. You could use an array or a list to store all the operators and then check for the index in that array/list.

3. Instead of using a string to store the operators, you could use a hashmap with the operators as keys and their precedence as values. This would make the code more flexible and easier to update in case new operators are added.

4. Your code only checks for one level of precedence (i.e. only for the new operator being either + or -). It would be better to check for all levels of precedence (i.e. + or -, * or /, and then any other operators). This can be done by assigning different values to the operators in the hashmap and then checking for the precedence based on those values.

I hope this helps. Let me know if you have any further questions or need clarification.
 
  • #3


I would like to clarify the purpose and expected outcome of this method. Is it meant to determine the precedence between two given operators, or is it meant to compare a new operator with a list of existing operators and determine its precedence relative to them? Additionally, it would be helpful to provide an example of how this method would be used in the context of infix to postfix conversion. This will help me provide a more accurate and effective solution.
 

What is Infix to Postfix Conversion?

Infix to Postfix Conversion is a process of converting an arithmetic expression written in infix notation (where operators are placed between operands) to postfix notation (where operators are placed after operands). This conversion is necessary for evaluating arithmetic expressions using a computer program.

Why do we need to convert from Infix to Postfix?

Computers use postfix notation to evaluate arithmetic expressions because it is easier for them to process. Infix notation requires the use of parentheses to indicate the order of operations, whereas postfix notation does not. Therefore, converting from infix to postfix allows for more efficient evaluation of expressions.

What are the steps involved in Infix to Postfix Conversion?

The steps for converting from infix to postfix are:

  1. Start with an empty stack and an empty postfix string.
  2. Scan the infix expression from left to right.
  3. If the current element is an operand, add it to the postfix string.
  4. If the current element is an opening parenthesis, push it onto the stack.
  5. If the current element is a closing parenthesis, pop elements from the stack and add them to the postfix string until an opening parenthesis is encountered. Discard both the opening and closing parentheses.
  6. If the current element is an operator, compare its precedence with the topmost element of the stack. If the precedence is higher, push it onto the stack. If the precedence is lower or equal, pop the topmost element from the stack and add it to the postfix string. Repeat this process until the stack is empty or an opening parenthesis is encountered. Then push the current element onto the stack.
  7. After scanning the entire expression, pop all remaining elements from the stack and add them to the postfix string.

Can Infix to Postfix Conversion handle all types of expressions?

Yes, Infix to Postfix Conversion can handle all types of expressions, including expressions with single or multiple digits, decimals, and different operators (such as addition, subtraction, multiplication, division, and modulo). However, it does not handle expressions with functions or nested parentheses.

Is Infix to Postfix Conversion reversible?

No, Infix to Postfix Conversion is not reversible. Once an expression is converted to postfix, it cannot be converted back to infix without additional information, such as the placement of parentheses. Therefore, it is important to carefully evaluate an expression before converting it to postfix.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
981
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top