Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /**
  2. * Assignment 4 Q2
  3. *
  4. */
  5.  
  6. /**
  7. * Student Name: Rup Gautam
  8. * Student No: 3091289
  9. */
  10.  
  11. import java.util.Scanner;
  12.  
  13.  
  14. public class Calculator {
  15.  
  16. public static void main(String[] args) {
  17. // An equation with +, -, /, *
  18. String EquationExp;
  19. System.out.print("Please enter math Expression: ");
  20. Scanner kb = new Scanner(System.in);
  21. EquationExp = kb.nextLine();
  22.  
  23. int another = computeEquation(EquationExp);
  24. System.out.println(another);
  25. }
  26.  
  27. static int computeEquation(String equation) {
  28. int result = 0;
  29. String Minus = equation.replace("-", "+-");
  30. String[] Plus = Minus.split("\\+");
  31.  
  32. for (String multiply : Plus) {
  33. String[] Multiply = multiply.split("\\*");
  34. int multiplyResult = 1; //
  35. for (String operand : Multiply) {
  36.  
  37. if (operand.contains("/")) {
  38. String[] division = operand.split("\\/");
  39. int dividend = Integer.parseInt(division[0]);
  40. for (int i = 1; i < division.length; i++) {
  41. dividend /= Integer.parseInt(division[i]);
  42. }
  43. multiplyResult *= dividend;
  44. } else {
  45. multiplyResult *= Integer.parseInt(operand);
  46. }
  47. }
  48. result += multiplyResult;
  49. }
  50.  
  51. return result;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement