Advertisement
Guest User

Untitled

a guest
May 26th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Calculator {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. boolean z = true;
  7. System.out.println("command list : +,*,-,/,%,^");
  8. String[] operation = new String[]{"+", "*", "-", "/", "%"};
  9. while (z) {
  10. System.out.println("Enter example");
  11. String example = sc.nextLine();
  12. String[] strArr = new String[operation.length];
  13. String oper = null;
  14. for (int i = 0; i < operation.length; i++) {
  15. if (example.contains(operation[i])) {
  16. oper = operation[i];
  17. strArr = example.split("\\" + operation[i]);
  18. break;
  19. }
  20. }
  21. double intArr[] = new double[strArr.length];
  22. for (int i = 0; i < strArr.length; i++) {
  23. intArr[i] = Double.valueOf(strArr[i]);
  24. }
  25. double x = intArr[0];
  26. double y = intArr[1];
  27.  
  28.  
  29. switch (oper) {
  30. case "+":
  31. System.out.println(sum(x, y));
  32. break;
  33. case "/":
  34.  
  35. System.out.println(division(x, y));
  36. break;
  37. case "*":
  38. System.out.println(multi(x, y));
  39. break;
  40. case "%":
  41. System.out.println(percent(x, y));
  42. break;
  43. case "-":
  44. System.out.println(substr(x, y));
  45. break;
  46. case "^":
  47. default:
  48.  
  49.  
  50. }
  51.  
  52. }
  53. }
  54.  
  55.  
  56. public static double sum(double x, double y) {
  57. return x + y;
  58. }
  59.  
  60. public static double division(double x, double y) {
  61. return x / y;
  62. }
  63.  
  64. public static double multi(double x, double y) {
  65. return x * y;
  66. }
  67.  
  68. public static double percent(double x, double y) {
  69. while (x > 0) {
  70. x -= y;
  71. }
  72. x += y;
  73. return x;
  74. }
  75.  
  76.  
  77. public static double substr(double x, double y) {
  78.  
  79. return x - y;
  80. }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement