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

Class Main

MathParser/src/com/aghajari/Main.java:10–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import java.util.Scanner;
9
10public class Main {
11
12 public static void main(String[] args) throws MathParserException {
13 Scanner scanner = new Scanner(System.in);
14 MathParser parser = MathParser.create();
15
16 for (; ; ) {
17 String line = scanner.nextLine().trim();
18 if (line.isEmpty())
19 continue;
20
21 if (isExp(line)) {
22 parser.addExpression(line);
23
24 } else {
25 try {
26 System.out.println("\n" + parser.parse(line));
27
28 } catch (MathParserException e) {
29 if (e instanceof MathVariableNotFoundException) {
30 System.out.println(e.getMessage());
31 continue;
32 }
33 e.printStackTrace();
34 return;
35 }
36 break;
37 }
38 }
39
40 if (!parser.getVariables().isEmpty()) {
41 System.out.println("Variables:");
42 for (MathParser.MathVariable variable : parser.getVariables())
43 System.out.println(variable.getName() + " = " + variable.getExpression() + " = " + variable.getAnswer());
44 }
45 }
46
47 public static boolean isExp(String line) {
48 if (line.contains("=")) {
49 if (line.contains("if")) {
50 return isExp(line.substring(0, line.indexOf("if")));
51 } else {
52 String l2 = line.substring(0, line.indexOf('='));
53 return Utils.isIdentifier(Utils.realTrim(l2)) || l2.contains("(");
54 }
55 } else
56 return false;
57 }
58}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected