Question
What changes do i need to make for the program below to not require space between each character in the math problem? import java.util.EmptyStackException;import java.util.Stack;import
What changes do i need to make for the program below to not require space between each character in the math problem?
import java.util.EmptyStackException;import java.util.Stack;import javax.swing.JOptionPane;
public class InfixExpression{Stack ans= q+p; st1.push(ans+""); } else if(op=='-') { ans= q-p; st1.push(ans+""); } else if(op=='*'){ ans= q*p; st1.push(ans+"");} else if(op=='/'){ ans=q/p; st1.push(ans+"");}}} catch(EmptyStackException e){ f=true;} if(f==false) return Integer.parseInt(st1.pop()); else return -1;} boolean checkHighPrecedence(char top, char c){ int tp=-1; int cp=-1; if(top == '+' || top == '-'){ tp=0;} if(top == '*' || top == '/' || top== '%'){ tp=1;} if(c == '+' || c == '-'){ cp=0;} if(c == '*' || c == '/' ){ cp=1;} if(tp>=cp) return true; else return false;}} import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.EmptyStackException;import java.util.Stack;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants; public class GUI extends JFrame implements ActionListener{ JTextField in;JLabel inl,rel;JPanel ip,rp;JButton button;Stack super("Infix Expression Evaluator"); ip=new JPanel(new FlowLayout()); rp=new JPanel(new FlowLayout()); setLayout(new GridLayout(2,1)); in =new JTextField(20); inl=new JLabel("Enter Infix Expression:",SwingConstants.LEFT); button=new JButton("Evaluate"); button.addActionListener(this); rel=new JLabel("Result:",SwingConstants.LEFT); add(ip); add(rp); ip.add(inl); ip.add(in); ip.add(button); rp.add(rel); s=new Stack public static void main(String args[]){ GUI g=new GUI(); g.setVisible(true); g.setSize(400,150);} boolean checkBalanceInfix(String exp){ int a = 0; boolean fail = false; int length=exp.length(); try{ while (a < length && !fail){ char ch = exp.charAt(a); switch(ch){ case '(': s.push( new Character(ch) ); break; case ')': Character rightBrace = (Character)s.pop(); if(rightBrace!='(') fail=true; break; default: break;} a++;}} catch(EmptyStackException e){ fail = true;} return s.empty() && !fail;} public void actionPerformed(ActionEvent arg0){ InfixExpression ife=new InfixExpression(); String exp=in.getText(); if(checkBalanceInfix(exp)){ try{ int ans=ife.findResult(exp); if(ans==-1) rel.setText("Result: It is not balanced."); else rel.setText("Result: "+exp+" = "+ans);} catch(ArithmeticException e){ JOptionPane.showMessageDialog(this, "Cannott divide by zero", "Divide by zero ERROR", JOptionPane.ERROR_MESSAGE);}} else rel.setText("Result: It is not balanced."); in.setText("");}}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started