Description of program You are to write a program name Infix

Description of program:

You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The expression may contain the following tokens:
(1)   Integer constants (a series of decimal digits).
(2)   x (representing a value to be supplied later).
(3)   Binary operators (+, -, *, / and %).
(4)   Parentheses
        
Spaces between tokens are allowed but not required. The program will convert the expression to postfix form and display the converted expression. Upload the file into A7 folder of icollege.

Sample Output:

Enter infix expression: (x + 1) * (x – 2) / 4
Converted expression: x 1 + x 2 - * 4 /

Enter infix expression: 1 2 +
Error in expression!! No operator between operands. Also last token must be an operand.

Enter infix expression: 10.4
Error in expression!! Cannot accept floating point numbers.

Enter infix expression: 1 ( + 2)
Error in expression!! No operator between operand and left parentheses.

Enter infix expression: 5 – (x – 2))
Error in expression!! No matching left parentheses for a right parentheses.

Enter infix expression: 1 ** 2
Error in expression!! The * operator cannot be preceded by a * operator.

Solution

//program for infix to posrtfix convertion

import java.io.*;

class stack

{

char stack1[]=new char[20];

int top;

void push(char ch)

{

top++;

stack1[top]=ch;

}

char pop()

{

char ch;

ch=stack1[top];

top--;

return ch;

}

int pre(char ch)

{

switch(ch)

{

case \'-\':return 1;

case \'+\':return 1;

case \'*\':return 2;

case \'/\':return 2;

}

retrun 0;

}

boolean operator(char ch)

{

if(ch==\'/\'||ch==\'*\'||ch==\'+\'||ch==\'-\')

return true;

else

return false;

}

boolean isAlpha(char ch)

{

if(ch>=\'a\'&&ch<=\'z\'||ch>=\'0\'&&ch==\'9\')

return true;

else

return false;

}

void postfix(String str)

{

char output[]=new char[str.length()];

char ch;

int p=0,i;

for(i=0;i<str.length();i++)

{

ch=str.charAt(i);

if(ch==\' ( \' )

{

push(ch);

}

else if(isAlpha(ch))

{

output[p++]=ch;

}

else if(operator(ch))

{

if(stack1[top]==0 || (pre(ch)>pre(stack1[top])) || stack1[top]==\'(\')

{

push(ch);

}

}

else if(pre(ch)<=pre(stack1[top]))

{

output[p++]=pop();

push(ch);

}

else if(ch==\'(\')

{

while((ch=po())!=\'(\')

{

output[p++]=ch;

}

}

}

while(top!=0)

{

output[p++]=pop();

}

for(int j=0;j<str.length();j++)

{

System.out.print(output[j]);

}

}

}

class intopost

{

public static void main(String[] args)throws Exception

{

String s;

BufferedReader br=new BufferedReader(new InputStreanReader(System.in));

stack b=new stack();

System.out.println(\"Enter input string\");

s=br.readLine();

System.out.println(\"Input string:\"+s);

System.out.println(\"Output string:\");

b.postfix(s);

}

}

Description of program: You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The e
Description of program: You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The e
Description of program: You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The e
Description of program: You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site