Created
March 27, 2017 20:51
-
-
Save RupGautam/a3b754d4a973a19e2b4022cb8a92a759 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Assignment 4 Q2 | |
| * | |
| */ | |
| /** | |
| * Student Name: Rup Gautam | |
| * Student No: 3091289 | |
| */ | |
| import java.util.Scanner; | |
| public class Calculator { | |
| public static void main(String[] args) { | |
| // An equation with +, -, /, * | |
| String EquationExp; | |
| System.out.print("Please enter math Expression: "); | |
| Scanner kb = new Scanner(System.in); | |
| EquationExp = kb.nextLine(); | |
| int another = computeEquation(EquationExp); | |
| System.out.println(another); | |
| } | |
| static int computeEquation(String equation) { | |
| int result = 0; | |
| String Minus = equation.replace("-", "+-"); | |
| String[] Plus = Minus.split("\\+"); | |
| for (String multiply : Plus) { | |
| String[] Multiply = multiply.split("\\*"); | |
| int multiplyResult = 1; // | |
| for (String operand : Multiply) { | |
| if (operand.contains("/")) { | |
| String[] division = operand.split("\\/"); | |
| int dividend = Integer.parseInt(division[0]); | |
| for (int i = 1; i < division.length; i++) { | |
| dividend /= Integer.parseInt(division[i]); | |
| } | |
| multiplyResult *= dividend; | |
| } else { | |
| multiplyResult *= Integer.parseInt(operand); | |
| } | |
| } | |
| result += multiplyResult; | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment