Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.05 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.fxml.FXML;
  4. import javafx.fxml.Initializable;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import jdk.internal.util.xml.impl.Input;
  9.  
  10. import java.io.*;
  11. import java.net.URL ;
  12. import java.util.ResourceBundle;
  13. import java.util.Stack;
  14. import java.util.Vector;
  15.  
  16. public class Controller implements Initializable {
  17. @FXML
  18. public Button one ;
  19. public Button two ;
  20. public Button three ;
  21. public Button four ;
  22. public Button five ;
  23. public Button six ;
  24. public Button seven ;
  25. public Button eight ;
  26. public Button nine ;
  27. public Button zero ;
  28. public Button point ;
  29. public Button clear ;
  30. public Button delete ;
  31. public Button next ;
  32. public Button prev ;
  33. public Button save ;
  34. public Button load ;
  35. public Button add ;
  36. public Button minus ;
  37. public Button divided ;
  38. public Button multi ;
  39. public Button equal ;
  40. public Button negative ;
  41. public TextField input ;
  42. public Label prom ;
  43. private double fNumber ;
  44. private double sNumber ;
  45. private String result ;
  46. private String operation , n , m,temp ;
  47. private Vector<String> equation ;
  48. private Stack previous = new Stack();
  49. private Stack nextt = new Stack();
  50. private Stack<String> opP = new Stack();
  51. private Stack<String> opN = new Stack();
  52. private boolean flag = false ;
  53. private boolean flag1 = false ;
  54. @Override
  55. public void initialize (URL arg0 , ResourceBundle arg1){
  56.  
  57. }
  58. public void input(String s ){
  59. temp = s ;
  60. equation.add(s);
  61. prom.setText(s);
  62. }
  63. public String getResult() {
  64. for (int i = 1; i < temp.length(); i++) {
  65. if (temp.charAt(i) == '+' || temp.charAt(i) == '-' || temp.charAt(i) == 'x' || temp.charAt(i) == '/') {
  66. operation = String.valueOf(temp.charAt(i));
  67. }
  68. }
  69. for (int i = 0; i < temp.length(); i++) {
  70. if (temp.charAt(i) == operation.charAt(0)) {
  71. flag = true;
  72. }
  73. if (!flag && temp.charAt(i) != operation.charAt(0)) {
  74. m = m + temp.charAt(i);
  75. this.fNumber = Double.valueOf(m);
  76. } else if (flag && temp.charAt(i) != operation.charAt(0)) {
  77. n = n + temp.charAt(i);
  78. this.sNumber = Double.valueOf(n);
  79. }
  80. }
  81. switch (operation) {
  82. case "+":
  83. Double system = this.fNumber + this.sNumber;
  84. previous.push(system);
  85. input.setText(String.valueOf(system));
  86. result = String.valueOf(system);
  87. String oldProm = prom.getText();
  88. prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  89. opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  90. operation = "=";
  91. break;
  92. case "-":
  93. Double systemMinus = this.fNumber - this.sNumber;
  94. previous.push(systemMinus);
  95. input.setText(String.valueOf(systemMinus));
  96. result = String.valueOf(systemMinus);
  97. String oldPromMinus = prom.getText();
  98. prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  99. opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  100. operation = "=";
  101. break;
  102. case "x":
  103. Double systemMulti = this.fNumber * this.sNumber;
  104. previous.push(systemMulti);
  105. input.setText(String.valueOf(systemMulti));
  106. result = String.valueOf(systemMulti) ;
  107. String oldPromMulti = prom.getText();
  108. prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  109. opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  110. operation = "=";
  111. break;
  112. case "/":
  113. if (this.sNumber == 0.0) {
  114. input.setText("error");
  115. } else {
  116. Double systemDevided = this.fNumber / this.sNumber;
  117. previous.push(systemDevided);
  118. input.setText(String.valueOf(systemDevided));
  119. result = String.valueOf(systemDevided);
  120. String oldPromDevided = prom.getText();
  121. prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  122. opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
  123. operation = "=";
  124. }
  125. break;
  126. }
  127. return result;
  128. }
  129. public String Current(){
  130. if(!opP.isEmpty()){
  131. return opP.peek();
  132. }else{
  133. return null ;
  134. }
  135. }
  136. public String prev(){
  137. if(!opP.isEmpty()){
  138. opN.push(opP.peek());
  139. return opP.pop();
  140. }else{
  141. return null ;
  142. }
  143. }
  144. public String next(){
  145. if(!opN.isEmpty()){
  146. opP.push(opP.peek());
  147. return opN.pop();
  148. }else{
  149. return null ;
  150. }
  151. }
  152. public void save(){
  153. try{
  154.  
  155. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("history.file"));
  156. out.writeObject(equation);
  157. out.writeObject(previous);
  158. out.flush();
  159. out.close();
  160. }catch (Exception a){
  161. throw new RuntimeException("save failed");
  162. }
  163. }
  164. public void load(){
  165. try{
  166. ObjectInputStream in=new ObjectInputStream(new FileInputStream("history.file"));
  167. equation=(Vector<String>) in.readObject();
  168. previous = (Stack<String>) in.readObject();
  169. prom.setText(equation.lastElement());
  170. input.setText(String.valueOf(previous.peek()));
  171. }catch (Exception a){
  172. throw new RuntimeException("load failed");
  173. }
  174. }
  175.  
  176. public void point_Click(){
  177. if(operation=="="&&!flag1){
  178. clear_Click();
  179. }
  180. flag1 = false ;
  181. String oldValue = input.getText();
  182. String set = String.valueOf('.');
  183. input.setText(oldValue+set);
  184. }
  185. public void zero_Click(){
  186. if(operation=="="){
  187. clear_Click();
  188. }
  189. String oldValue = input.getText();
  190. String set = String.valueOf("0");
  191. input.setText(oldValue+set);
  192. }
  193. public void one_Click(){
  194. if(operation=="="){
  195. clear_Click();
  196. }
  197. String oldValue = input.getText();
  198. String set = String.valueOf("1");
  199. input.setText(oldValue+set);
  200. }
  201. public void two_Click(){
  202. if(operation=="="){
  203. clear_Click();
  204. }
  205. String oldValue = input.getText();
  206. String set = String.valueOf("2");
  207. input.setText(oldValue+set);
  208. }
  209. public void three_Click(){
  210. if(operation=="="){
  211. clear_Click();
  212. }
  213. String oldValue = input.getText();
  214. String set = String.valueOf("3");
  215. input.setText(oldValue+set);
  216. }
  217. public void four_Click(){
  218. if(operation=="="){
  219. clear_Click();
  220. }
  221. String oldValue = input.getText();
  222. String set = String.valueOf("4");
  223. input.setText(oldValue+set);
  224. }
  225. public void five_Click(){
  226. if(operation=="="){
  227. clear_Click();
  228. }
  229. String oldValue = input.getText();
  230. String set = String.valueOf("5");
  231. input.setText(oldValue+set);
  232. }
  233. public void six_Click(){
  234. if(operation=="="){
  235. clear_Click();
  236. }
  237. String oldValue = input.getText();
  238. String set = String.valueOf("6");
  239. input.setText(oldValue+set);
  240. }
  241. public void seven_Click() {
  242. if (operation == "=") {
  243. clear_Click();
  244. }
  245. String oldValue = input.getText();
  246. String set = String.valueOf("7");
  247. input.setText(oldValue + set);
  248. }
  249. public void eight_Click(){
  250. if(operation=="="){
  251. clear_Click();
  252. }
  253. String oldValue = input.getText();
  254. String set = String.valueOf("8");
  255. input.setText(oldValue+set);
  256. }
  257. public void nine_Click(){
  258. if(operation=="="){
  259. clear_Click();
  260. }
  261. String oldValue = input.getText();
  262. String set = String.valueOf("9");
  263. input.setText(oldValue+set);
  264. }
  265. public void clear_Click(){
  266. input.setText("");
  267. prom.setText("");
  268. this.fNumber = 0 ;
  269. this.sNumber = 0 ;
  270. }
  271. public void multi_Click(){
  272. String value = input.getText();
  273. Double valueNumber = Double.valueOf(value);
  274. this.fNumber = valueNumber ;
  275. input.setText("");
  276. prom.setText(value + "x");
  277. operation = "x" ;
  278. }
  279. public void add_Click(){
  280. String value = input.getText();
  281. Double valueNumber = Double.valueOf(value);
  282. this.fNumber = valueNumber ;
  283. input.setText("");
  284. prom.setText(value + "+");
  285. operation = "+" ;
  286. }
  287. public void minus_Click(){
  288. String value = input.getText();
  289. Double valueNumber = Double.valueOf(value);
  290. this.fNumber = valueNumber ;
  291. input.setText("");
  292. prom.setText(value + "-");
  293. operation = "-" ;
  294. }
  295. public void divided_Click(){
  296. String value = input.getText();
  297. Double valueNumber = Double.valueOf(value);
  298. this.fNumber = valueNumber ;
  299. input.setText("");
  300. prom.setText(value + "/");
  301. operation = "/" ;
  302. }
  303. public void equal_Click(){
  304. switch (operation){
  305. case "+" :
  306. String value = input.getText();
  307. this.sNumber = Double.valueOf(value);
  308. Double system = this.fNumber + this.sNumber ;
  309. previous.push(system);
  310. input.setText(String.valueOf(system));
  311. String oldProm = prom.getText();
  312. prom.setText(oldProm + value);
  313. opP.push(oldProm + value);
  314. operation = "=";
  315. flag1 = true ;
  316. break;
  317. case "-" :
  318. String valueMinus = input.getText();
  319. this.sNumber = Double.valueOf(valueMinus);
  320. Double systemMinus = this.fNumber - this.sNumber ;
  321. previous.push(systemMinus);
  322. input.setText(String.valueOf(systemMinus));
  323. String oldPromMinus = prom.getText();
  324. prom.setText(oldPromMinus + valueMinus);
  325. opP.push(oldPromMinus + valueMinus);
  326. operation = "=";
  327. flag1 = true ;
  328. break;
  329. case "x" :
  330. String valueMulti = input.getText();
  331. this.sNumber = Double.valueOf(valueMulti);
  332. Double systemMulti = this.fNumber * this.sNumber ;
  333. previous.push(systemMulti);
  334. input.setText(String.valueOf(systemMulti));
  335. String oldPromMulti = prom.getText();
  336. prom.setText(oldPromMulti + valueMulti);
  337. opP.push(oldPromMulti + valueMulti);
  338. operation = "=";
  339. flag1 = true ;
  340. break;
  341. case "/" :
  342. String valueDevided = input.getText();
  343. this.sNumber = Double.valueOf(valueDevided);
  344. if (this.sNumber == 0.0){
  345. input.setText("error");
  346. }else {
  347. Double systemDevided = this.fNumber / this.sNumber;
  348. previous.push(systemDevided);
  349. input.setText(String.valueOf(systemDevided));
  350. String oldPromDevided = prom.getText();
  351. prom.setText(oldPromDevided + valueDevided);
  352. opP.push(oldPromDevided + valueDevided);
  353. operation = "=";
  354. flag1 = true ;
  355. }
  356. break;
  357. }
  358. }
  359. public void save_Click() {
  360. try{
  361.  
  362. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("history.file"));
  363. out.writeObject(opP);
  364. out.writeObject(previous);
  365. out.flush();
  366. out.close();
  367. System.out.println("saved");
  368. }catch (Exception a){
  369. throw new RuntimeException("save failed");
  370. }
  371. }
  372. public void load_Click(){
  373. try{
  374. ObjectInputStream in=new ObjectInputStream(new FileInputStream("history.file"));
  375. opP=(Stack<String>) in.readObject();
  376. previous = (Stack<String>) in.readObject();
  377. prom.setText(opP.peek());
  378. input.setText(String.valueOf(previous.peek()));
  379. }catch (Exception a){
  380. throw new RuntimeException("load failed");
  381. }
  382. }
  383. public void next_Click(){
  384. if(!nextt.isEmpty()) {
  385. opP.push(opN.peek());
  386. prom.setText(String.valueOf(opN.pop()));
  387. previous.push(nextt.peek());
  388. input.setText(String.valueOf(nextt.pop()));
  389.  
  390. }
  391. }
  392. public void prev_Click(){
  393. if(!previous.isEmpty()) {
  394. opN.push(opP.peek());
  395. prom.setText(String.valueOf(opP.pop()));
  396. nextt.push(previous.peek());
  397. input.setText(String.valueOf(previous.pop()));
  398. }
  399. }
  400. public void negative_Click(){
  401. String value = input.getText();
  402. Double valueNumber = Double.valueOf(value);
  403. valueNumber = -1*valueNumber ;
  404. value = String.valueOf(valueNumber);
  405. input.setText( value);
  406. }
  407.  
  408.  
  409.  
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement