Write a Stack class that supports only push and pop operations of character elements in the stack that is implemented using the java LinkedList API. Use the stack class to do the following: 1. Using stacks implement a Queue. 2. Convert a given infix expression to postfix. The infix expression may contain brackets. 3. Take as a input values of the variables in the postfix expression from the user. Find the value of postfix expression.
import java.util.*; public class assg08 { public static void main(String[] args)throws Exception { int k=0,k1=0,k2=0,b=1,j=0,m1=0,m2=0,m3=0,m4=0,m5=0,n1=0,n2=0,l=0,m6=0; String s1; int out[]=new int[100]; stack h=new stack(); Stack1 z=new Stack1(); Scanner stdin = new Scanner(System.in); System.out.print("enter the expression"); s1=stdin.nextLine(); char exp[]=new char[50]; for(int i=0;i<s1.length();i++) { char ch=s1.charAt(i); if(ch=='(') { h.push(ch); if(i>0) { char ch1=s1.charAt(i-1); if(ch1==')') m5=1; } } if(ch==')') { if(!h.isempty()) { h.pop(); } else m2++; if(i>0) { char ch1=s1.charAt(i-1); if(ch1=='(') m4=1; } if(i>0) { char ch1=s1.charAt(i-1); if(ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/') m6=1; } } if(ch=='+'||ch=='-'||ch=='*'||ch=='/') { n1++; if(i>0) { char ch1=s1.charAt(i-1); if(ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/') m1=1; } } char ch2=s1.charAt(0); if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/') { m1=1; } if(isOperand(ch)) { n2++; if(i>0) { char ch1=s1.charAt(i-1); if(isOperand(ch1)) m3=1; } } if(isOperand(ch)||ch=='+'||ch=='-'||ch=='*'||ch=='/') { k2=1; } } k=n2-n1; if(!h.isempty()||h.isempty()&&m2!=0||m1==1||m3==1||k!=1||m4==1||m5==1||k2==0||m6==1) { System.out.println("expression is not valid"); } else { System.out.println("expression is valid"); l=1; } if(l==1) { for(int i=0;i<s1.length();i++) { char ch=s1.charAt(i); if(isOperand(ch)) { out[j++]=ch; } else if(ch==')') { do { out[j++]=h.pop(); }while(h.seek()!='('); h.pop(); } else if(ch=='(') { h.push(ch); } else { if(pre(ch)>pre(h.seek())|| h.head==null) { h.push(ch); } else { do{ out[j++]=h.pop(); }while(pre(ch)<=pre(h.seek())); h.push(ch); } } } while(h.head!=null) { out[j++]=h.pop(); } int y; for (int i = 0; i < j; i++) { if(out[i]>48) {System.out.print(out[i]-48);} else { System.out.print((char)out[i]);} } for(int g=0;g<j;g++) { if(out[g]>=48) { y=out[g]-48; z.push(""+y); } else { char d=(char)out[g]; int j2=Integer.parseInt(z.pop()); int j1=Integer.parseInt(z.pop()); int d2=operation(d,j1,j2); z.push(""+d2); } } System.out.print("the result is="+Integer.parseInt(z.pop())); } } static int operation(char z,int x,int y) { switch(z) { case '+':return (x+y); case '-':return (x-y); case '*':return (x*y); case '/':return(x/y); default:return 0; } } static boolean isOperand(char c) { if(c>=97 && c<=122 || c>=48 && c<=57) { return true; } else { return false; } } static int pre(char c) { int a=0; switch(c) { case '+':a=1; break; case '-':a=1; break; case '*':a=2; break; case '/':a=2; break; case '(':a=0; break; case ')':a=0; break; case '?':a=-1; break; } return a; } } class stack { linked head; stack() { head=null; } void push(char e) { if(head!=null) {head.next=new linked(e);; head.next.prev=head; head=head.next; } else {head=new linked(e);} } char pop() { linked o; o=head; head=head.prev; if(head!=null) {head.next=null;} return o.a; } char seek() { if(head==null) {return '?';} else {return head.a;} } boolean isempty() { if(head==null) return true; else return false; } } class linked { char a; linked next; linked prev; linked(char x) { a=x; next=null; prev=null; } } class linked1 { String a; linked1 next; linked1 prev; linked1(String x) { a=x; next=null; prev=null; } } class Stack1 { linked1 head; Stack1() { head=null; } void push(String e) { if(head!=null) {head.next=new linked1(e);; head.next.prev=head; head=head.next; } else {head=new linked1(e);} } String pop() { linked1 o; o=head; head=head.prev; if(head!=null) {head.next=null;} return o.a; } String seek() { if(head==null) {return null;} else {return head.a;} } }
Write a Stack class that supports only push and pop operations of character elements in the stack that is implemented using the java LinkedList API. Use the stack class to do the following: 1. Using stacks implement a Queue. 2. Convert a given infix expression to postfix. The infix expression may contain brackets. 3. Take as a input values of the variables in the postfix expression from the user. Find the value of postfix expression.
Reviewed by Unknown
on
03:48
Rating:
No comments: