MCPcopy Create free account
hub / github.com/Aghajari/MathParser / orderAgain

Method orderAgain

MathParser/src/com/aghajari/math/MathParser.java:525–565  ·  view source on GitHub ↗

#calculate(String, String, boolean) has simplified the expression as much as it's possible, It is time to set priorities of #operations. This function first checks if all operations are in the same priority. If yes, the phrase is the simplest case possible and the final calculations

(String src, String main)

Source from the content-addressed store, hash-verified

523 * This cycle will also apply to all variables
524 */
525 private double orderAgain(String src, String main) throws MathParserException {
526 boolean allInSamePriority = true;
527 int highestPriority = -1;
528 for (int i = 0; i < order.length; i++) {
529 if (src.contains(String.valueOf(order[i]))) {
530 if (highestPriority != -1 && orderPriority[i] != highestPriority) {
531 allInSamePriority = false;
532 break; // the first ones always have higher priority
533 }
534 highestPriority = Math.max(highestPriority, orderPriority[i]);
535 }
536 }
537
538 if (!allInSamePriority) {
539 int ind = -1;
540 char op = '+';
541 for (int i = 0; i < order.length; i++) {
542 if (orderPriority[i] == highestPriority) {
543 int ind2 = src.indexOf(order[i]);
544 if (ind2 != -1 && (ind == -1 || ind > ind2)) {
545 ind = ind2;
546 op = order[i];
547 }
548 }
549 }
550
551 if (ind != -1) {
552 int index;
553 String wordAfter = src.substring(ind + 1, ind + 1 + Utils.findBestIndex(src.substring(ind + 1), false));
554 String wordBefore = src.substring(index = Utils.findBestIndex(src.substring(0, ind), true), ind);
555
556 src = src.substring(0, index) + "(" + wordBefore + op + wordAfter + ")"
557 + src.substring(ind + wordAfter.length() + 1);
558 }
559 }
560
561 if (src.contains("(") || src.contains(")"))
562 return calculate(src, main);
563
564 return new SimpleParser(src, main).parse();
565 }
566
567 /**
568 * Generates a new unique name for temp variables in format of __tmp{index}

Callers 1

calculateMethod · 0.95

Calls 4

findBestIndexMethod · 0.95
calculateMethod · 0.95
maxMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected