оброго времени суток. Помогите, пожалуйста, правильно реализовать действие для кнопок "sqrt"(вычисление квадратного корня), "pow(x,2)"(вторая степень), "c"(общий сброс) и "<"(удаление последнего символа). Дело в том, что сразу после вычисления квадратного корня получившийся результат не возводится в степень, а после общего сброса бывает вообще неразбериха.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyCalc extends JFrame {
private JButton display;
private double result;
private String lastCom;
private boolean start;
private void addButton(String name, ActionListener listener, int x, int y,
int width, int height) {
JButton button = new JButton(name);
button.setBounds(x, y, width, height);
button.addActionListener(listener);
getContentPane().add(button);
}
public MyCalc() {
getContentPane().setLayout(null);
setTitle("Мой калькулятор");
setBounds(200, 200, 203, 242);
getContentPane().setBackground(new Color(100, 149, 237));
ActionListener output = new OutputAction();
ActionListener command = new CommandAction();
result = 0;
lastCom = "=";
start = true;
display = new JButton("0");
display.setEnabled(false);
getContentPane().add(display);
display.setBounds(5, 10, 177, 30);
addButton("7", output, 5, 50, 41, 20);
addButton("8", output, 50, 50, 41, 20);
addButton("9", output, 95, 50, 41, 20);
addButton("/", command, 140, 50, 41, 20);
addButton("4", output, 5, 75, 41, 20);
addButton("5", output, 50, 75, 41, 20);
addButton("6", output, 95, 75, 41, 20);
addButton("*", command, 140, 75, 41, 20);
addButton("1", output, 5, 100, 41, 20);
addButton("2", output, 50, 100, 41, 20);
addButton("3", output, 95, 100, 41, 20);
addButton("-", command, 140, 100, 41, 20);
addButton("0", output, 5, 125, 41, 20);
addButton(".", output, 50, 125, 41, 20);
addButton("=", command, 95, 125, 41, 20);
addButton("+", command, 140, 125, 41, 20);
addButton("с", command, 5, 150, 85, 20);
addButton("<", command, 95, 150, 85, 20);
addButton("sqrt", command, 5, 175, 85, 20);
addButton("pow(x,2)", command, 95, 175, 85, 20);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
TextEvent t = new TextEvent(getContentPane(), 1);
}
private class OutputAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (start) {
if (command.equals("-")) {
display.setText(command);
start = false;
} else {
lastCom = command;
}
}else if (command.equals("sqrt")) {
display.setText("" + Math.sqrt(Double.parseDouble(display.getText())));
start = true;
} else if (command.equals("pow(x,2)")) {
display.setText("" + Math.pow(Double.parseDouble(display.getText()), 2));
start = true;
}else if (command.equals("с")) {
display.setText("");
start = true;
}else if (command.equals("<")) {
//???
} else {
calculate(Double.parseDouble(display.getText()));
lastCom = command;
start = true;
}
}
}
public void calculate(double x) {
if (lastCom.equals("+")) {
result += x;
display.setText("" + result);
} else if (lastCom.equals("-")) {
result -= x;
display.setText("" + result);
} else if (lastCom.equals("*")) {
result *= x;
display.setText("" + result);
} else if (lastCom.equals("/")) {
if (x == 0) {
display.setText("Деление на ноль");
} else {
result /= x;
display.setText("" + result);
}
} else if (lastCom.equals("=")) {
result = x;
display.setText("" + result);
}
}
public static void main(String[] args) {
new MyCalc();
}
}
|