Total members 11893 |It is currently Fri Nov 15, 2024 10:46 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





infix to postfix java converter implementation.
java code
/* Data Structure Project 
Converting Infix to Post-fix


*/

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

class InfixToPostfixConverter
{
private static String ADD = "+";
private static String SUBTRACT = "-";
private static String MULTIPLY = "*";
private static String DIVIDE = "/";
private static String EXP = "^";
private static String LPAREN = "(";
private static String RPAREN = ")";

public static void main(String[] args){
System.out.println("processing, wait...");
ArrayList infixArrayList = readInfixList("input.txt");
if(infixArrayList != null){
writePostfixList("itop.txt", infixListToPostfixList(trimInfixList(infixArrayList)));
System.out.println("done.");
}
}

//function that reads infix values line by line from a file, saving each as an ArrayList item
//then returns the ArrayList.
public static ArrayList readInfixList(String infixFile){
ArrayList infixList = new ArrayList();

BufferedReader infixLines = null;
try {
infixLines = new BufferedReader(new FileReader(infixFile) );
String infixLine = null;
while (( infixLine = infixLines.readLine()) != null){
infixList.add(infixLine);
}
}
catch (FileNotFoundException e) {
System.out.println("Input file '" + infixFile + "' not found!");
return null;
}
catch (IOException e){
System.out.println("I/O error!");;
return null;
}
finally {
try {
if (infixLines != null) {
infixLines.close();
}
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
return infixList;
}

//function that writes back the generated postfix values to a file
public static void writePostfixList(String postfixFile, ArrayList postfixList){
try{
BufferedWriter postfixLines = new BufferedWriter(new FileWriter(postfixFile));
for(int i = 0; i < postfixList.size(); i++){
postfixLines.write(postfixList.get(i).toString());
if(i < (postfixList.size() - 1)){
postfixLines.newLine();
}
}
postfixLines.close();
}
catch(IOException e){
System.out.println(e);
}
}

//function that strips the "begin " and " end" strings to each item on the infixList
public static ArrayList trimInfixList(ArrayList infixList) {
String line = "";
String beginText = "begin ";
String endText = " end";
for(int i=0; i<infixList.size(); i++){
line = infixList.get(i).toString().trim();
line = line.replaceAll(beginText, "").replaceAll(endText, "");
infixList.set(i, line);
}
return infixList;
}

//function that loops on the infixList generating the required output to a postfixList
public static ArrayList infixListToPostfixList(ArrayList infixList) {
ArrayList postfixList = new ArrayList();
String infix = "";
String postfix = "";
int postfixVal = 0;

for(int i=0; i<infixList.size(); i++){
infix = infixList.get(i).toString();
postfix = infixToPostfix(infix);
postfixVal = evaluatePostfix(postfix);
postfixList.add("Infix: " + infix);
postfixList.add(" Postfix:" + postfix);
postfixList.add(" Result: " + postfixVal);
}
return postfixList;
}

//function that returns the priority level of the passed operator parameter
public static int getOperatorPriority(String operator){
int retval = -1;
if(operator.equals(LPAREN)){
retval = 0;
}
else if(operator.equals(ADD) || operator.equals(SUBTRACT)){
retval = 1;
}
else if(operator.equals(MULTIPLY) || operator.equals(DIVIDE)){
retval = 2;
}
else if(operator.equals(EXP)){
retval = 3;
}
return retval;
}

//function that tests if the parameter is an operator
public static boolean isOperator(String operator){
return operator.equals(ADD) || operator.equals(SUBTRACT)
|| operator.equals(MULTIPLY) || operator.equals(DIVIDE)
|| operator.equals(EXP);
}

//function that converts from infix to postfix notation
public static String infixToPostfix(String Infix){
MyStackArray operatorStack = new MyStackArray();
String tokens[] = Infix.split(" ");
String token = "";
String postfix = "";
int tokenPriority = 0;

for(int i=0; i<tokens.length; i++){
if(tokens[i].equals(LPAREN)){
operatorStack.push(tokens[i]);
}
else if(tokens[i].equals(RPAREN)){
while(!operatorStack.isEmpty()){
token = operatorStack.topAndPop();
if(token.equals(LPAREN)){
break;
}
postfix += " " + token;
}
}
else if(isOperator(tokens[i])){
tokenPriority = getOperatorPriority(tokens[i]);

if(tokens[i].equals(EXP)){
while(!operatorStack.isEmpty() && getOperatorPriority(operatorStack.top()) > tokenPriority){
token = operatorStack.topAndPop();
if(!token.equals(LPAREN)){
postfix += " " + token;
}
}
}
else{
while(!operatorStack.isEmpty() && getOperatorPriority(operatorStack.top()) >= tokenPriority){
token = operatorStack.topAndPop();
if(!token.equals(LPAREN)){
postfix += " " + token;
}
}
}
operatorStack.push(tokens[i]);
}
else{
postfix += " " + tokens[i];
}
}

while(!operatorStack.isEmpty()){
token = operatorStack.topAndPop();
if(!token.equals(LPAREN)){
postfix += " " + token;
}
}
return postfix;
}

//function which evaluates the given postfix parameter
public static int evaluatePostfix(String Postfix){
MyStackArray operandStack = new MyStackArray();
String tokens[] = Postfix.trim().split(" ");
int token;
String operand1 = "";
String operand2 = "";
int retval = 0;

for(int i=0; i<tokens.length; i++){
if(isOperator(tokens[i])){
operand2 = operandStack.topAndPop();
operand1 = operandStack.topAndPop();
token = evaluateExpression(tokens[i], Integer.parseInt(operand1), Integer.parseInt(operand2));
operandStack.push(String.valueOf(token));
}
else{
operandStack.push(tokens[i]);
}
}
retval = Integer.parseInt(operandStack.topAndPop());
return retval;
}

//function that returns result from the given expression
public static int evaluateExpression(String operator, int operand1, int operand2){
int retval = 0;

if(operator.equals(ADD)){
retval = operand1 + operand2;
}
else if(operator.equals(SUBTRACT)){
retval = operand1 - operand2;
}
else if(operator.equals(MULTIPLY)){
retval = operand1 * operand2;
}
else if(operator.equals(DIVIDE)){
retval = operand1 / operand2;
}
else if(operator.equals(EXP)){
retval = myExponent(operand1, operand2);
}

return retval;
}

//custom exponent function
public static int myExponent(int base, int exp){
return (exp == 0) ? 1 : (base * myExponent(base, exp-1));
}
}


//custom array-based stack class.
class MyStackArray{
private String[] theArray;
private int topOfStack;

static final int DEFAULT_CAPACITY = 255;

//constructor
public MyStackArray(){
this(DEFAULT_CAPACITY);
}

//constructor
public MyStackArray(int capacity){
theArray = new String[capacity];
topOfStack = -1;
}

//tests whether the stack is empty or not
public boolean isEmpty( ){
return topOfStack == -1;
}

//function that tests whether the stack is already full
public boolean isFull(){
return topOfStack == theArray.length - 1;
}

//function which sets the stack to empty
public void makeEmpty(){
topOfStack = -1;
}

//function which returns the top of the stack
public String top(){
if(isEmpty()){
return null;
}
return theArray[topOfStack];
}

//function which pops the top item on the stack
public void pop(){
if(!isEmpty()){
theArray[topOfStack--] = null;
}
}

//function which inserts item on the stack
public void push(String x){
if(!isFull()){
theArray[++topOfStack] = x;
}
}

//function which pops the top item on the stack and then returns it.
public String topAndPop(){
if(isEmpty()){
return null;
}
String topItem = top();
theArray[topOfStack--] = null;
return topItem;
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : infix to postfix converter
 Postfix and Prefix Operators table     -  
 C to C++ converter     -  
 PST Converter Pro     -  
 need help doing Universal Converter     -  
 PHP srt to xml subtitle converter     -  
 Number system converter (C++)     -  
 The Digital to Analogue converter DAC writer     -  



Topic Tags

Java Strings
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com