Share on Google+Share on Google+

metzie
Infix to Prefix
1 Answer(s)      7 years ago
Posted in : Java Beginners
Hello, I needed help on converting from an infix expression to a prefix expression using stacks.

Ads

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
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
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
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
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: To demonstrate the difference between prefix and postfix operator we... of this variable. Now we are applying the prefix increment operator and display the value
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
Struts 2.3.15.2 also fixes one more problems related to "action:" prefix... implementation and stop using the "action:" prefix completely if they do not need need
JSP Interview Questions
(XML-based) prefix of "jsp" followed by a colon, followed by the action name
jAVA BEGINNER PROBLEMS
jAVA BEGINNER PROBLEMS  I need the program that takes from standard input an expression without left parenthesis and prints the equivalent infix expression with the parenthesis inserted??? Like EXAMPLE: 1+2)3-4)5-6
java program - Development process
java program  hi, i need a simple program for converting given infix expression to postfix expression  Hi friend, Code to solve...(); System.out.println("Infix is " + inStr + '\n'); System.out.println
Java operator
Java operator  What is the difference between the prefix and postfix forms of the ++ operator
XML namespace
XML namespace  hi.... please tell me about that What is an XML namespace prefix? thanks
XML namespace
XML namespace  Hi...... please tell me about that What characters are allowed in an XML namespace prefix? Thanks
XML namespace
XML namespace  hi... please tell me about that What happens if there is no prefix on an element type name? thanks
how to display the value of termcell name in label
of the jsp page is as: <%@ taglib uri="http://displaytag.sf.net" prefix="display"%> <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%> <%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean"%> <%@ taglib
need java code for <company:loc="bang">
need java code for   hi i want to write code like , i used setAttribute method for this. but its showing ERROR: 'Namespace for prefix 'company...: java.lang.RuntimeException: Namespace for prefix 'company' has not been declared. please help
xmlname space in java
xmlname space in java  hi i want to write code like , i used setAttribute method for this. but its showing ERROR: 'Namespace for prefix...: java.lang.RuntimeException: Namespace for prefix 'company' has not been declared
XML namespace
XML namespace  hi... please anyone tell me about Can I use the same prefix for more than one XML namespace? Thanks
Struts Tag Lib - Struts
Defines a tag library and prefix for the custom tags used in the JSP page... that defines them, and specifies their tag prefix. A taglib directive before you... in a JSP page. but the prefix defined in each must be unique. Attributes
text processing program
text processing program  how can i compare letter by letter? what i was trying to do is a program that can define what is root word,prefix and suffix. plz help
JSP tag lib directive
JSP tag lib directive   What is tag lib directive in the JSP?   Hi, The answer is: Defines a tag library and prefix for the custom tags used in the JSP page. Thanks
Tiles in jsp
" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %> Read... at line <[email protected] uri="WEB-INF/struts-tiles.tld" prefix="tiles"%> error>
datetimepicker not displayed in struts2
datetimepicker not displayed in struts2  Hi, I am facing problem in struts2(2.1 version) to diaplay datetimepicker. sample code: <[email protected] prefix="s" uri="/struts-tags"%> <[email protected] prefix="d" uri="/struts-dojo-tags
jsf: generate another panelGrid
jsf: generate another panelGrid  hello the following is my code print("code sample"); <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
JSP Taglib Directive using process
="URIToTagLibrary" prefix="tagPrefix" %> Example: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="core" %> Multiply 5 and 2:: Thanks
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads