basically gamit ko ani ug tissue kai gadugo akong ilong.
btw, gamit ani katong concept na
preFix(+ab), inFix(a+b), postFix(ab+) ,
input an
inFix expression nya convert it into
preFix or
postFix.
Selection:
[1]Convert to preFix
[2]Convert to postix
2 (postFix ang gipili)
input inFix expression:
(300+23)*(43-21)/(84+7) //kuha nani nako
Output in postFix:
300 23 + 43 21 - * 84 7 + /
input inFix expression:
(60*4)+(43-21)-(44+7) //kuha nani nako
Output in postFix:
604 * 43 21 - + 44 7 + -
input inFix expression:
(60*4)+(43-21)*(44+7)
Output in postFix:
60 4 * 43 21 - 44 7 + * +
input inFix expression:
12+(2-7) //kuha nani nako
Output in postFix:
12 2 7 - +
Selection:
[1]Convert to preFix
[2]Convert to postix
1 (preFix ang gipili)
input inFix expression:
(300+23)*(43-21)/(84+7)
Output in preFix:
/ * + 300 23 - 43 21 + 84 7
input inFix expression:
(60*4)+(43-21)-(44+7)
Output in preFix:
- + * 60 4 - 43 21 + 44 7
input inFix expression:
(60*4)+(43-21)*(44+7)
Output in preFix:
+ * 60 4 * - 43 21 + 44 7
input inFix expression:
12+(2-7)
Output in pretFix:
+ 12 - 2 7
ang paagi sa pag solve kai
PMDAS pero di tanto kai same raman ang value sa
multiplication/division or
addition/subtraction, so left to right ang priority. maka solve ko ani manually pero sa paghimo nag codes, naglisud jud ko as in, mao nang ni post ko dri kai di pako ganahan mabuang, hahahaha.
here is my source code. (sample sa postfix ra akong nakuha(1st and second sample)). kanang sample uban, ako rai naghimo ana. wa pajud koi idea, tabang mga boss., hahaha boss stealthghost, superStar, Klave ug kinsa panang mga sweto na au. hehehe tnx daan.
Code:
import java.util.*;
public class Algo {
private static Scanner in = new Scanner(System.in);
private int selected=0,index1=0,index2=0,isTrue=0;
private String expression="";
private static List<Object> list = new ArrayList<Object>();
private static List<Object> list2 = new ArrayList<Object>();
private boolean digit,insideParentheses=false,tf=false;
private String numbers = "", groupings="";
private static PriorityQueue<Object> q = new PriorityQueue<Object>();
private static Stack myStack = new Stack();
private static Stack myStack2 = new Stack();
public Algo(){
Selection();
}
public void Selection(){
try{
System.out.println("Select Conversion: \n[1]PreFix \n[2]PostFix");
this.selected = Integer.parseInt(in.nextLine());
Selected(selected);
}catch(Exception e){
System.out.println("Invalid. Try again "+e.getMessage());
Selection();
}
}
public void Selected(int s){
switch(s){
case 1:
askExpression();
//PreFix();
break;
case 2:
askExpression();
postFixProcess();
break;
default:
System.out.println("Invalid. Try again");
Selection();
break;
}
}
public void askExpression(){
System.out.println("\nInput Expression:");
expression = in.nextLine();
int size = expression.length();
System.out.println("By one:");
for(int x=0; x<size;x++){
char c = expression.charAt(x);
if(Character.isDigit(c)){
this.numbers += c;
digit = true;
}else{
if(digit==true){
list.add(numbers);
}
list.add(c);
digit = false;
this.numbers = "";
}
// System.out.println(numbers);
}
this.numbers = "";
System.out.println(list);
//======================================groupings
size = expression.length();
System.out.println("By group:");
for(int x=0; x<expression.length();x++){
char c = expression.charAt(x);
if(c=='('|c==')'){
this.groupings += c;
tf=true;
if(c=='(')
insideParentheses = true;
else
insideParentheses = false;
}else if(insideParentheses==true){
this.groupings += c;
}else if(insideParentheses==false){
if(tf==true)
list2.add(groupings);
list2.add(c);
groupings = "";
tf=false;
}else
list2.add(c);
}
System.out.println(list2);
}
public void postFixProcess(){
System.out.println("Output:");
for(int x=0; x<list.size();x++){
if(list.get(x)=='('){
myStack.push(list.get(x));
insideParentheses = true;
}else if(list.get(x)=='+'||list.get(x)=='-'||list.get(x)=='*'||list.get(x)=='/'){
if(insideParentheses==true)
myStack.add(list.get(x));
if(insideParentheses==false)
q.offer(list.get(x));
}else if(list.get(x)==')'){
insideParentheses = false;
System.out.print(myStack.pop()+" ");
isTrue++;
// insideParentheses = false;
/*
if(insideParentheses==false)
System.out.print(myStack2.pop()+" ");
insideParentheses = false;
*
*/
}else
System.out.print(list.get(x)+" ");
if(isTrue==2){
System.out.print(q.poll()+" ");
isTrue = 0;
}
}
if(isTrue==1)
System.out.print(q.poll()+" ");
//System.out.println("\n"+q);
}
public static void main(String args []){
Algo a = new Algo();
}
}