Программа расчета уравнений - оператор switch должен работать, но term1 должен быть переменной (в алгебре), и я не думаю, что код использует его как переменную - следует оценить уравнение в виде x op b = c,где x - любое имя переменной, а b и c - целые числа. Результаты должны отображаться как x op b = c ==> x =
package Equations;
/**
* This program evaluates arithmetic expressions
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.StringTokenizer;
public class EquationSolver extends JFrame
implements ActionListener
{
private JTextField display;
private static final String ARROW = "==> ";
public EquationSolver()
{
super("Equation Solver");
display = new JTextField(40);
display.setFont(new Font("Monospaced", Font.BOLD, 14));
display.setBackground(Color.white);
display.setForeground(Color.blue);
display.addActionListener(this);
display.setText("Enter a math problem to solve");
setBounds(50, 150, 450, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(display);
setVisible(true);
// Select the whole display text:
display.selectAll();
// Prepare for typing:
display.requestFocus();
}
public void actionPerformed(ActionEvent e)
{
// Skip if display has any selected text:
if (display.getSelectedText() != null)
return;
String text = display.getText();
display.setText(text + " " + process(text));
display.selectAll();
}
private String process(String s)
{
String term1, op, term2;
s = s.trim(); //this takes all the white space out on the end
//s.insert(" ",s.indexOf('=')+1);
//s.insert(" ",s.indexOf('=')-1);
//modify s so that there are spaces around the operator and/or equals sign (extra spaces are ok but must have 1 space)
//only works for 3 or 5 tokens in this program(the else part of the if-statement)
StringTokenizer terms = new StringTokenizer(s);
if (terms.countTokens() == 3)
{
term1 = terms.nextToken();
op = terms.nextToken();
term2 = terms.nextToken();
return evaluate(term1, op, term2);
}
else
return ARROW + "Syntax error";
}
/**
* Evaluates an arithmetic expression in the form
* "a +/- b" and returns the result
* in the form "= c" or an error message.
*/
private String evaluate(String term1, String op, String term2)
//write another method evaluate with 4 parameters for the second part of the problem
{
String result;
if (op.length() != 1)
return ARROW + "Syntax error";
char opSign = op.charAt(0);
int a = Integer.parseInt(term1);
int b = Integer.parseInt(term2);
switch (opSign)
{
case '+':
result = "= " + (a + b);
break;
case '-':
result = "= " + (a - b);
break;
case '*':
result = "= " + (a * b);
break;
case '%':
if(b==0)
result = "==> Divide by zero error!";
else // a%b==0
result = "= " + (a / b);
break;
case '/':
if(b==0)
result = "==> Divide by zero error!";
else if(a%b>0)
result = "==> " + a + " is not evenly divisible by " + b;
else //a%b==0
result = "= " + (a / b);
break;
//expand this switch statement to work on all the operators
default:
result = ARROW + "Invalid operation";
break;
}
return result;
}
private String evaluate(String term1, String op, String term2, String term3)
{
String result;
if (op.length() != 1)
return ARROW + "Syntax error";
char opSign = op.charAt(0);
int b = Integer.parseInt(term2);
int c = Integer.parseInt(term3);
int a = Integer.parseInt(term1);
switch (opSign)
{
case '+':
result = a + "= " + (c - b);
break;
case '-':
result = a + "= " + (c + b);
break;
case '*':
if(b==0)
result = "No Solution";
else if(c%b != 0)
result = "The answer is not an integer";
else
result = a + "= " + (c / b);
break;
case '%': //still needs work
if(b==0)
result = "==> Divide by zero error!";
else // a%b==0
result = term1 + "= " + b + "n" + "+" + c + "where n>=0;"; //needs to be in a JOptionPane window
break;
case '/':
if(b==0)
result = "==> Divide by zero error!";
else //a%b==0
result = a + "= " + (c * b);
break;
default:
result = ARROW + "Invalid operation";
break;
}
return result;
}
public static void main(String args[])
{
EquationSolver panel = new EquationSolver();
}
}