Infix to Prefix

Infix to Prefix

Hello, I needed help on converting from an infix expression to a prefix expression using stacks.
View Answers

March 12, 2010 at 10:30 AM

Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;

public class ConvertInfixToPrefix {
public static String[] operators = {"+","-","/","*","^"};
public ConvertInfixToPrefix() {
}
public String convert(String infix) {
StringBuffer sb = new StringBuffer(infix);
int index = sb.length();
String operand = "";
int steps = 0;
for (int i = 0; i < sb.length(); i++) {
for (int x = 0; x < operators.length; x++) {
int tempIndex = sb.indexOf(operators[x], i);
if (tempIndex < index && tempIndex >= 0) {
index = tempIndex;
operand = operators[x];
}
}
if (index == sb.length()) {
break;
}
sb.delete(index, index + operand.length() + 1);
for (int x = index - 1; x >= 0; x--) {
if (sb.charAt(x) == '(' && steps == 0) {
sb.insert(x, operand);
break;
} else if (sb.charAt(x) == '(') {
steps--;
} else if (sb.charAt(x) == ')') {
steps++;
}
}
i = index;
index = sb.length();
}
return sb.toString();
}
public static void main(String[] args) {
String infix = "((A * ( B + C ))/ D)";
ConvertInfixToPrefix ConvertInfixToPrefix = new ConvertInfixToPrefix();
System.out.println(ConvertInfixToPrefix.convert(infix));
}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
Prefix / Postfix to Infix
Prefix / Postfix to Infix  Hi, I'm currently a high school student studying Computer Science 3 in the USA. We are currently learning prefix, postfix, and infix and have been tasked with creating a program that converts prefix
Infix to Prefix - Java Beginners
Infix to Prefix  Hello, I needed help on converting from an infix expression to a prefix expression using stacks.  Hi Friend, Try... infix) { StringBuffer sb = new StringBuffer(infix); int index
Advertisements
Prefix as A
Prefix as A   I want to know how to write validations in javascript for prefix as as letters and rest numbers.I have ACC NO A111 with A as prefix How to give validations
ModuleNotFoundError: No module named 'infix'
ModuleNotFoundError: No module named 'infix'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'infix' How to remove the ModuleNotFoundError: No module named 'infix'
infix to post fix convertion
infix to post fix convertion  a+(bc-(d/e^f))*h
infix to postfix covertion
infix to postfix covertion  (a+b)*(d/e) convert in post fix notation step by step
maximal prefix
maximal prefix  Read the maximal prefix of the input string that may be a prefix of a decimal number.Determine whether this prefix is a decimal number or not, and the number may be optionally preceded
Java Convert Infix to Postfix
Java Convert Infix to Postfix In this tutorial, you will learn how to convert expression from infix to postfix. The given code accepts the expression from... { private String infix; private String postfix = ""; public void
write a java program to solve the infix expression
write a java program to solve the infix expression  ==>> solve the below expression using java program: 2+5*2-5+6/2 using stack operations or any other process in java
ModuleNotFoundError: No module named 'si-prefix'
ModuleNotFoundError: No module named 'si-prefix'  Hi, My Python...-prefix' How to remove the ModuleNotFoundError: No module named 'si-prefix' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'prefix-codes'
ModuleNotFoundError: No module named 'prefix-codes'  Hi, My Python... 'prefix-codes' How to remove the ModuleNotFoundError: No module named 'prefix-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-db-prefix'
ModuleNotFoundError: No module named 'django-db-prefix'  Hi, My... named 'django-db-prefix' How to remove the ModuleNotFoundError: No module named 'django-db-prefix' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-table-prefix'
ModuleNotFoundError: No module named 'django-table-prefix'  Hi, My... named 'django-table-prefix' How to remove the ModuleNotFoundError: No module named 'django-table-prefix' error? Thanks   Hi
ModuleNotFoundError: No module named 'prefix-list-agent'
ModuleNotFoundError: No module named 'prefix-list-agent'  Hi, My... named 'prefix-list-agent' How to remove the ModuleNotFoundError: No module named 'prefix-list-agent' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-db-prefix'
ModuleNotFoundError: No module named 'django-db-prefix'  Hi, My... named 'django-db-prefix' How to remove the ModuleNotFoundError: No module named 'django-db-prefix' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-table-prefix'
ModuleNotFoundError: No module named 'django-table-prefix'  Hi, My... named 'django-table-prefix' How to remove the ModuleNotFoundError: No module named 'django-table-prefix' error? Thanks   Hi
org.apache.hbase - hbase-prefix-tree version 1.4.3 Maven dependency. How to use hbase-prefix-tree version 1.4.3 in pom.xml?
org.apache.hbase  - Version 1.4.3 of hbase-prefix-tree Maven dependency? How to use  org.apache.hbase  - Version 1.4.3 of hbase-prefix-tree in pom.xml? How to use hbase-prefix-tree version 1.4.3 in pom.xml? Learn to use
org.apache.hbase - hbase-prefix-tree version 1.2.11 Maven dependency. How to use hbase-prefix-tree version 1.2.11 in pom.xml?
org.apache.hbase  - Version 1.2.11 of hbase-prefix-tree Maven dependency? How to use  org.apache.hbase  - Version 1.2.11 of hbase-prefix-tree in pom.xml? How to use hbase-prefix-tree version 1.2.11 in pom.xml? Learn to use
org.apache.hbase - hbase-prefix-tree version 0.96.1.1-hadoop1 Maven dependency. How to use hbase-prefix-tree version 0.96.1.1-hadoop1 in pom.xml?
org.apache.hbase  - Version 0.96.1.1-hadoop1 of hbase-prefix-tree Maven... of hbase-prefix-tree in pom.xml? How to use hbase-prefix-tree version... it easy to use org.apache.hbase  - Version 0.96.1.1-hadoop1 of hbase-prefix
Maven Repository/Dependency: org.apache.hbase | hbase-prefix-tree
Maven Repository/Dependency of Group ID org.apache.hbase and Artifact ID hbase-prefix-tree. Latest version of org.apache.hbase:hbase-prefix-tree dependencies. # Version Release Date
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.3.5 is released. Learn to use hbase-prefix-tree version 1.3.5 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.3.5. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.4.13 is released. Learn to use hbase-prefix-tree version 1.4.13 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 24 Feb 2020, the released version of  org.apache.hbase - hbase-prefix-tree library
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.18-hadoop1 is released. Learn to use hbase-prefix-tree version 0.98.18-hadoop1 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 19 Mar 2016, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.7.2 is released. Learn to use hbase-prefix-tree version 1.7.2 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.7.2. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.4.14 is released. Learn to use hbase-prefix-tree version 1.4.14 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 20 Oct 2021, the released version of  org.apache.hbase - hbase-prefix-tree library
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.21-hadoop1 is released. Learn to use hbase-prefix-tree version 0.98.21-hadoop1 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 09 Aug 2016, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.5-hadoop1 is released. Learn to use hbase-prefix-tree version 0.98.5-hadoop1 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 05 Aug 2014, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.4.2 is released. Learn to use hbase-prefix-tree version 1.4.2 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.4.2. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.7.1 is released. Learn to use hbase-prefix-tree version 1.7.1 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.7.1. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.1-hadoop2 is released. Learn to use hbase-prefix-tree version 0.98.1-hadoop2 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 30 Mar 2014, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.3.0 is released. Learn to use hbase-prefix-tree version 1.3.0 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.3.0. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.10-hadoop1 is released. Learn to use hbase-prefix-tree version 0.98.10-hadoop1 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 01 Feb 2015, the released version of  org.apache.hbase - hbase-prefix
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.1.1 is released. Learn to use hbase-prefix-tree version 1.1.1 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 24 Jun 2015, the released version of  org.apache.hbase - hbase-prefix-tree library
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.4.7 is released. Learn to use hbase-prefix-tree version 1.4.7 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 29 Aug 2018, the released version of  org.apache.hbase - hbase-prefix-tree library
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.4-hadoop1 is released. Learn to use hbase-prefix-tree version 0.98.4-hadoop1 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 15 Jul 2014, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.7.0 is released. Learn to use hbase-prefix-tree version 1.7.0 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.7.0. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.6.0 is released. Learn to use hbase-prefix-tree version 1.6.0 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.6.0. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.12.1-hadoop1 is released. Learn to use hbase-prefix-tree version 0.98.12.1-hadoop1 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 18 May 2015, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 0.98.12.1-hadoop2 is released. Learn to use hbase-prefix-tree version 0.98.12.1-hadoop2 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 18 May 2015, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.1.0.1 is released. Learn to use hbase-prefix-tree version 1.1.0.1 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 18 May 2015, the released version of  org.apache.hbase - hbase-prefix-tree library
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.2.6 is released. Learn to use hbase-prefix-tree version 1.2.6 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix..., the released version of  org.apache.hbase - hbase-prefix-tree library is 1.2.6. Developer can use this version ( org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 2.0.0-alpha4 is released. Learn to use hbase-prefix-tree version 2.0.0-alpha4 in Maven based Java projects
of hbase-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 03 Nov 2017, the released version of  org.apache.hbase - hbase-prefix-tree
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.4.5 is released. Learn to use hbase-prefix-tree version 1.4.5 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 14 Jun 2018, the released version of  org.apache.hbase - hbase-prefix-tree library
Maven dependency for org.apache.hbase - hbase-prefix-tree version 1.1.3 is released. Learn to use hbase-prefix-tree version 1.1.3 in Maven based Java projects
-prefix-tree released The developers of   org.apache.hbase - hbase-prefix-tree project have released the latest version of this library on 17 Jan 2016, the released version of  org.apache.hbase - hbase-prefix-tree library
Controlling RDF Prefix in Java
Controlling RDF Prefix in Java   ....  First is explicit prefix controlling and second is implicit prefix definition controlling. Here we have created prefix definitions with the explicit
Auto Increment and Decrement
of prefix and postfix operators by incrementing and decrementing the value... of program:ADS_TO_REPLACE_1 To demonstrate the difference between prefix and postfix... of this variable. Now we are applying the prefix increment operator
CreditCardValidator in Flex4
CreditCardValidator in Flex4: The CreditCardValidator class is used for validate a credit card number. It checks the length and correct prefix of the credit card number and check the number is currently active
Simple Query on RDF file in Java
Simple Query on RDF file in Java       As you have already learned about controlling prefix, generating RDF files and about RDF Iterators in the previous examples, so
Apache Struts 2.3.15.2 launched to fix vulnerable Feature
to "action:" prefix of the action mapping mechanism that can be used to attach... the "action:" prefix completely if they do not need need support for multiple submit
JSP Interview Questions
(XML-based) prefix of "jsp" followed by a colon, followed by the action name

Ads