Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Infix to Prefix
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


metzie
Infix to Prefix
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners


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

Advertisement
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 operator
Java operator  What is the difference between the prefix and postfix forms of the ++ operator
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
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 <%@taglib 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: <%@taglib prefix="s" uri="/struts-tags"%> <%@taglib 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
 

 

 

DMCA.com