My code works fine and produces the infix but I get an exception that I don\'t k
ID: 3684071 • Letter: M
Question
My code works fine and produces the infix but I get an exception that I don't know how to fix. Also I'm trying to implement a way so it keeps asking the user for the x value and quits when the user enters q. In addition, if the user enters some error then it should tell the user that there is an error.
My code:
import java.util.Scanner;
import java.util.Stack;
/**
* @author Srinivas Palli
*
*/
class term {
public term next;
public Object data;
/**
*
*/
public term() {
// TODO Auto-generated constructor stub
data = "";
next = null;
}
/**
* @param val
*/
public term(Object val) {
this.data = val;
next = null;
}
}
/**
* @author Srinivas Palli
*
*/
public class intopost {
private term top;
public intopost() {
// TODO Auto-generated constructor stub
top = null;
}
/**
* @return
*/
public boolean empty() {
return top == null;
}
/**
* @return
*/
public boolean full() {
return false;
}
/**
* @param e
*/
public void push(Object e) {
term tmp = new term(e);
tmp.next = top;
top = tmp;
}
/**
* @return
*/
public Object pop() {
Object e = top.data;
top = top.next;
return e;
}
/**
* @return
*/
public Object peek() {
Object e = top.data;
return e;
}
public static boolean matching = true;
/**
* @param x
*/
public void maching(String x) {
Stack s = new Stack();
for (int i = 0; i < x.length(); i++) {
char c = x.charAt(i);
if (c == '(')
s.push(c);
else {
if (c == ')')
if (s.empty())
matching = false;
else
s.pop();
}
}
if (!s.empty())
matching = false;
}
/**
* @param x
*/
public void Evaluation(String x) {
int i, z, u;
char c;
intopost S = new intopost();
for (i = 0; i < x.length(); i++) {
c = x.charAt(i);
String s = "0" + c;
if (c == '+') {
z = Integer.parseInt((String) S.pop())
+ Integer.parseInt((String) S.pop());
S.push(Integer.toString(z));
}
else if (c == '*') {
z = Integer.parseInt((String) S.pop())
* Integer.parseInt((String) S.pop());
S.push(Integer.toString(z));
}
else if (c == '/') {
u = Integer.parseInt((String) S.pop());
z = Integer.parseInt((String) S.pop()) / u;
S.push(Integer.toString(z));
} else if (c == '-') {
u = Integer.parseInt((String) S.pop());
z = Integer.parseInt((String) S.pop()) - u;
S.push(Integer.toString(z));
} else
S.push(s);
}
System.out.println("THE POSTFIX = " + x);
System.out.println("THE RESULT = " + S.pop());
}
/**
* @param x
* @return
*/
public String postfix(String x) {
String output = "";
intopost S = new intopost();
for (int i = 0; i < x.length(); i++) {
char c = x.charAt(i);
if (c == '+' || c == '*' || c == '-' || c == '/') {
while (!S.empty() && priority(S.peek()) >= priority(c))
output += S.pop();
S.push(c);
} else if (c == '(')
S.push(c);
else if (c == ')') {
while (!S.peek().equals('('))
output += S.pop();
S.pop();
} else
output += c;
}
while (!S.empty())
output += S.pop();
return output;
}
/**
* @param x
* @return
*/
public int priority(Object x) {
if (x.equals('+') || x.equals('-'))
return 1;
else if (x.equals('*') || x.equals('/'))
return 2;
else
return 0;
}
public static void main(String[] args) {
try {
intopost e = new intopost();
String infix, post;
System.out.println("Enter equation in infixform");
Scanner in = new Scanner(System.in);
infix = in.next();
post = e.postfix(infix);
System.out.println("THE POSTFIX = " + post);
e.maching(infix);
e.Evaluation(post);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Examples of what the output should be like.
Enter infix expression: (x + 1) * (x – 2) / 4
Converted expression: x 1 + x 2 - * 4 /
Enter value of x: 5
Answer to expression: 4
Enter value of x: 7
Answer to expression: 10
Enter value of x: q
If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate. The following examples illustrate various types of errors:
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.
The output of your program must match the format illustrated in this example.
Write a program name calc.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: Integer constants (a series of decimal digits). x (representing a value to be supplied later). Binary operators (+. -, *,/and %). Parentheses additional requirements for this program: You must use stack objects during the translation from infix to postfix. Operators must have the correct precedence and associativity Binary operators *./and % takes precedence over binary And processing is always done from loft to right. If the infix expression contains an error of any kind, the program must display the message Error m expression (with an optional explanation) and then terminate.Explanation / Answer
please keep throws exception in your public static void main String args[] . keep it as public static void main String args[] throws Exception {
}.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.