Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement