Tuesday 16 September 2014

BODMAS Rule

B → Brackets first (parentheses)

→ Of (orders i.e. Powers and Square Roots, Cube Roots, etc.)

DM  Division and Multiplication (start from left to right)

AS → Addition and Subtraction (start from left to right)

Note:

(i) Start Divide/Multiply from left side to right side since they perform equally.

(ii) Start Add/Subtract from left side to right side since they perform equally.

Steps to simplify the order of operation using BODMAS rule:

First part of an equation is start solving inside the 'Brackets'.

For Example; (6 + 4) × 5
First solve inside ‘brackets’ 6 + 4 = 10, then 10 × 5 = 50.


Next solve the mathematical 'Of'.

For Example; 3 of 4 + 9
First solve ‘of’ 3 × 4 = 12, then 12 + 9 = 21.


Next, the part of the equation is to calculate 'Division' and 'Multiplication'.
We know that, when division and multiplication follow one another, then their order in that part of the equation is solved from left side to right side.

For Example; 15 ÷ 3 × 1 ÷ 5

Multiplication’ and ‘Division’ perform equally, so calculate from left to right side. First solve 15 ÷ 3 = 5, then 5 × 1 = 5, then 5 ÷ 5 = 1.


In the last part of the equation is to calculate 'Addition' and 'Subtraction'. We know that, when addition and subtraction follow one another, then their order in that part of the equation is solved from left side to right side.

For Example; 7 + 19 - 11 + 13

Addition’ and ‘Subtraction’ perform equally, so calculate from left to right side. First solve 7 + 19 = 26, then 26 - 11 = 15 and then 15 + 13 = 28.

These are simple rules need to be followed forsimplifying or calculating using BODMAS rule.


In brief, after we perform "B" and "O", start from left side to right side by solving any "D"or "M" as we find them. Then start from left side to right side solving any "A" or "S" as we find them.

How to use BODMAS rule

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        String orgString = "(3+4)*(7/2)";
        System.out.println(findValueInBraces(orgString));

    }

    public static String findValueInBraces(String finalStr) {

       while (finalStr.contains("(") && finalStr.contains(")")) {

            int fIndex = finalStr.indexOf("(");
            int nIndex = finalStr.indexOf(")");
            String subString = finalStr.substring(fIndex + 1, nIndex);
            

            finalStr = finalStr.substring(0, fIndex) + calculate(subString) + finalStr.substring(nIndex +1, finalStr.length());
        }
        return calculate(finalStr);

    }

    public static String calculate(String finalString) {

        while (finalString.contains("(") && finalString.contains(")")) {
            findValueInBraces(finalString);
        }
        while (!isNum(finalString)) {
            List<Integer> positions = getOperandPosition(finalString);
            int pos = positions.get(0);
            if (positions.size() >= 2 && positions.get(1) != null) {
                int nxtPos = positions.get(1);
                finalString = getValue(finalString.substring(0, nxtPos), pos)
                        + finalString.substring(nxtPos, finalString.length());
            } else {
                finalString = getValue(
                        finalString.substring(0, finalString.length()), pos);
            }
        }
        return finalString;

    }

    public static boolean isNum(String str) {
        if (str.contains("+") || str.contains("-") || str.contains("*")
                || str.contains("/")) {
            return false;
        }
        return true;
    }

    public static List<Integer> getOperandPosition(String str) {

        List<Integer> integers = new ArrayList<Integer>();

        if (str.contains("+")) {
            integers.add(str.indexOf("+"));
        }

        if (str.contains("-")) {
            integers.add(str.indexOf("-"));
        }

        if (str.contains("*")) {
            integers.add(str.indexOf("*"));
        }

        if (str.contains("/")) {
            integers.add(str.indexOf("/"));
        }

        Collections.sort(integers);
        return integers;
    }

    public static String getValue(String str, int pos) {
        double finalVal = 0;
        double a = Double.parseDouble(str.substring(0, pos));
        double b = Double.parseDouble(str.substring(pos + 1, str.length()));
        char c = str.charAt(pos);

        if (c == '*') {
            finalVal = a * b;
        } else if (c == '/') {
            finalVal = a / b;
        } else if (c == '+') {
            finalVal = a + b;
        } else if (c == '-') {
            finalVal = a - b;
        }
        return String.valueOf(finalVal);
    }
}