Hi...
I'm a new user here. I've stumbled across this place on a couple occasions in the past, and it worked out to my advantage when I needed a little help with some coding assignments.
i need to include "parenthesis" in the equation in my program but i dont know how. some help or suggestions.
package client;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Stack;
import java.util.Vector;
public class InfixPostfix{
private Stack<Character> output = new Stack<Character>();
private Stack<Character> operator = new Stack<Character>();
private Vector<String> temp = new Vector<String>();
private Vector<String> finaloutput = new Vector<String>();
private Vector<Character> infix = new Vector<Character>();
private Enumeration<Character>temp_elem;
private int counter=0;
public InfixPostfix(String InFile)throws IOException{
this.reader(InFile);
}
private void reader(String inFileName)throws IOException{
String Line;
BufferedReader inFile = new BufferedReader(new FileReader(inFileName));
Line = inFile.readLine();
while(Line!=null)
{
temp.add(Line);
counter++;
Line = inFile.readLine();
}
inFile.close();
infix();
}
private void infix(){
String formula = temp.elementAt(counter-1);
for(int a=0;a<formula.length();a++)
{
infix.add(formula.charAt(a));
}
postfix();
}
private void postfix(){
char ch;
boolean flag=true;
temp_elem=infix.elements();
while(temp_elem.hasMoreElements()){
ch=temp_elem.nextElement();
if(this.isOperand(ch))
{
output.push(ch);
}
else if(this.isOperator(ch))
{
if(operator.empty())
operator.push(ch);
else if (this.Precedence(operator.peek()) < this.Precedence(ch)){
operator.push(ch);
}
else{
output.push(operator.peek());
operator.pop();
int len = operator.size();
for(int i=0; i<len; i++){
flag=false;
if(this.Precedence(operator.peek()) < this.Precedence(ch)){
operator.push(ch);
break;
}
else{
output.push(operator.peek());
operator.pop();
flag=true;
}
}
if(flag)operator.push(ch);
}
}
}
while(operator.empty()==false){
output.push(operator.peek());
operator.pop();
}System.out.println("Postfix: "+output);
convert();
}
private boolean isOperator(char ch)
{
if ((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/'))
return true;
else
return false;
}
private boolean isOperand(char ch)
{
if ((ch>='a' && ch<='z') || (ch>='0' && ch<='9'))
return true;
else
return false;
}
private int Precedence(char opr)
{
int n=0;
switch(opr){
case '-':
case '+': n=1; break;
case '*':
case '/': n=2; break;
}
return n;
}
public void convert()
{
String Line="";
for(int i=0;i<output.size();i++){
for(int j=0;j<counter-1;j++){
Line=temp.elementAt(j);
char ch=Line.charAt(0);
if(output.get(i)==ch){
output.set(i,Line.charAt(2));
}
}
}
result();
}
public void result(){
int num1=0,num2=0;
String s;
char ch;
for(int i=0;i<output.size();i++){
finaloutput.add(output.get(i).toString());
}
for(int i=0;i<finaloutput.size();i++){
s=finaloutput.get(i);
ch=s.charAt(0);
if(this.isOperator(ch)){
if(ch=='*'){
num1=Integer.valueOf(finaloutput.get(i-2));
num2=Integer.valueOf(finaloutput.get(i-1));
finaloutput.set(i-2, Integer.toString((num1*num2)));
finaloutput.remove(i-1);
finaloutput.remove(i-1);
i=0;
}
else if(ch=='/'){
num1=Integer.valueOf(finaloutput.get(i-2));
num2=Integer.valueOf(finaloutput.get(i-1));
finaloutput.set(i-2, Integer.toString((num1/num2)));
finaloutput.remove(i-1);
finaloutput.remove(i-1);
i=0;
}
else if(ch=='+'){
num1=Integer.valueOf(finaloutput.get(i-2));
num2=Integer.valueOf(finaloutput.get(i-1));
finaloutput.set(i-2, Integer.toString((num1+num2)));
finaloutput.remove(i-1);
finaloutput.remove(i-1);
i=0;
}
else{
num1=Integer.valueOf(finaloutput.get(i-2));
num2=Integer.valueOf(finaloutput.get(i-1));
finaloutput.set(i-2, Integer.toString((num1-num2)));
finaloutput.remove(i-1);
finaloutput.remove(i-1);
i=0;
}
}
}System.out.println("Answer: "+finaloutput);
}
}
//end of the class
package host;
import java.io.*;
import client.InfixPostfix;
public class hostClass {
public static void main(String[] args)throws IOException {
InfixPostfix InPostFix = new InfixPostfix("D:\\inFileName.txt");
}
}