Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.StringTokenizer;
  5.  
  6. public class EquationSolver extends JFrame
  7. implements ActionListener
  8. {
  9. private JTextField display;
  10.  
  11. private static final String ARROW = "==> ";
  12.  
  13. public EquationSolver()
  14. {
  15. super("Equation Solver");
  16. display = new JTextField(40);
  17. display.setFont(new Font("Monospaced", Font.BOLD, 14));
  18. display.setBackground(Color.white);
  19. display.setForeground(Color.blue);
  20. display.addActionListener(this);
  21. display.setText("Enter a math problem to solve");
  22. setBounds(50, 150, 450, 100);
  23. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. Container c = getContentPane();
  25. c.setLayout(new FlowLayout());
  26. c.add(display);
  27.  
  28.  
  29. setVisible(true);
  30. // Select the whole display text:
  31. display.selectAll();
  32.  
  33. // Prepare for typing:
  34. display.requestFocus();
  35. }
  36.  
  37. public void actionPerformed(ActionEvent e)
  38. {
  39. // Skip if display has any selected text:
  40. if (display.getSelectedText() != null)
  41. return;
  42.  
  43. String text = display.getText();
  44. display.setText(text + " " + process(text));
  45. display.selectAll();
  46. }
  47.  
  48. private String process(String s)
  49. {
  50. String term1, op, term2;
  51.  
  52. s = s.trim();
  53.  
  54.  
  55. for(int i=0;i<s.length();i++)
  56. {
  57. char x=s.charAt(i);
  58. if (x=='+' || x=='-' || x=='*' || x=='/' || x=='%')
  59. {
  60. int r=s.indexOf('x');
  61. String q=s.substring(0,r);
  62. String y=s.substring(r+1,s.length());
  63.  
  64. s = q+" "+x+" "+y;
  65. break;
  66. }
  67.  
  68.  
  69. }
  70.  
  71.  
  72. StringTokenizer terms = new StringTokenizer(s);
  73. if (terms.countTokens() == 3)
  74. {
  75. term1 = terms.nextToken();
  76. op = terms.nextToken();
  77. term2 = terms.nextToken();
  78. return evaluate(term1, op, term2);
  79. }
  80. else
  81. return ARROW + "Syntax error";
  82. }
  83.  
  84. /**
  85. * Evaluates an arithmetic expression in the form
  86. * "a +/- b" and returns the result
  87. * in the form "= c" or an error message.
  88. */
  89. private String evaluate(String term1, String op, String term2)
  90. {
  91. String result;
  92.  
  93. if (op.length() != 1)
  94. return ARROW + "Syntax error";
  95.  
  96. char opSign = op.charAt(0);
  97. int a = Integer.parseInt(term1);
  98. int b = Integer.parseInt(term2);
  99.  
  100. switch (opSign)
  101. {
  102. case '+':
  103. result = "= " + (a + b);
  104. break;
  105.  
  106. case '-':
  107. result = "= " + (a - b);
  108. break;
  109.  
  110. case '*':
  111. result = "= "+(a*b);
  112. break;
  113.  
  114. case '/':
  115. if(b!=0 && a%b==0)
  116. result = "= "+(a/b);
  117. else if(b==0)
  118. return ARROW + " Divide by zero error!";
  119. else
  120. return ARROW + " "+a+" is not evenly divisible by "+b;
  121. break;
  122.  
  123. case '%':
  124. if (b!=0)
  125. result = "= "+(a%b);
  126. else
  127. return ARROW + " Divide by zero error!";
  128. break;
  129.  
  130. default:
  131. result = ARROW + "Invalid operation";
  132. break;
  133. }
  134. return result;
  135. }
  136.  
  137. public static void main(String args[])
  138. {
  139.  
  140. EquationSolver panel = new EquationSolver();
  141.  
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement