Post

OO Design

Basic Calculator IV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// evaluation map
private Map<String, Integer> map = new HashMap<>();

class Term {
    int coefficient = 1;
    List<String> variables = new ArrayList<>();

    Term(int coefficient) {
        this.coefficient = coefficient;
    }

    Term(String s) {
        if (map.containsKey(s)) {
            coefficient = map.get(s);
        } else {
            variables.add(s);
        }
    }

    @Override
    public String toString() {
        if (coefficient == 0) {
            return "";
        }

        return coefficient + (variables.isEmpty() ? "" : "*") + String.join("*", variables);
    }

    // two terms equal if they are like terms
    boolean equals(Term t) {
        if (variables.size() != t.variables.size()) {
            return false;
        }

        for (int i = 0; i < variables.size(); i++) {
            if (!variables.get(i).equals(t.variables.get(i))) {
                return false;
            }
        }

        return true;
    }

    int compareTo(Term t) {
        // degree
        if (variables.size() > t.variables.size()) {
            return -1;
        }

        if (variables.size() < t.variables.size()) {
            return 1;
        }

        // lexicographic order
        for (int i = 0; i < variables.size(); i++) {
            int value = variables.get(i).compareTo(t.variables.get(i));
            if (value != 0)  {
                return value;
            }
        }

        return 0;
    }

    void sign(int sign) {
        coefficient = sign * coefficient;
    }

    Term times(Term t) {
        Term product = new Term(coefficient * t.coefficient);
        variables.forEach(product.variables::add);
        t.variables.forEach(product.variables::add);

        Collections.sort(product.variables);
        return product;
    }
}

class Expression {
    List<Term> terms = new ArrayList<>();
    char operator = '+';

    Expression(int number) {
        terms.add(new Term(number));
    }

    Expression(String expr) {
        terms.add(new Term(expr));
    }

    Expression(List<Term> terms) {
        this.terms = terms;
    }

    Expression times(Expression e) {
        List<Term> list = new ArrayList<>();
        for (Term t1 : terms) {
            for (Term t2 : e.terms) {
                list.add(t1.times(t2));
            }
        }

        return new Expression(combine(list));
    }

    Expression plus(Expression e, int sign) {
        List<Term> list = new ArrayList<>();
        terms.forEach(list::add);
        e.terms.stream().peek(t -> t.sign(sign)).forEach(list::add);
        return new Expression(combine(list));
    }

    Expression eval(Expression e) {
        return operator == '*' ? times(e) : plus(e, operator == '+' ? 1 : -1);
    }

    List<String> toList() {
        return terms.stream()
            .map(t -> t.toString())
            .filter(s -> !s.isEmpty())
            .collect(Collectors.toList());
    }
}

// combines like terms
private List<Term> combine(List<Term> terms) {
    Collections.sort(terms, (a, b) -> a.compareTo(b));

    List<Term> list = new ArrayList<>();
    for (Term t : terms) {
        if (!list.isEmpty() && t.equals(list.get(list.size() - 1))) {
            list.get(list.size() - 1).coefficient += t.coefficient;
        } else {
            list.add(t);
        }
    }
    return list;
}

public List<String> basicCalculatorIV(String expression, String[] evalvars, int[] evalints) {
    // builds evaluation map
    for (int i = 0; i < evalvars.length; i++) {
        map.put(evalvars[i], evalints[i]);
    }

    // expression stack
    Deque<Expression> stack = new ArrayDeque<>();
    stack.push(new Expression(0));

    // priority stack
    Deque<Integer> priorityStack = new ArrayDeque<>();
    priorityStack.push(0);

    int i = 0, n = expression.length();
    int priority = 0;
    while (i < n) {
        char ch = expression.charAt(i);
        if (Character.isDigit(ch)) {
            int num = 0;
            while (i < n && Character.isDigit(expression.charAt(i))) {
                num = num * 10 + expression.charAt(i) - '0';
                i++;
            }
            stack.push(new Expression(num));
        } else if (Character.isLetter(ch)) {
            StringBuilder sb = new StringBuilder();
            while (i < n && Character.isLetter(expression.charAt(i))) {
                sb.append(expression.charAt(i));
                i++;
            }
            stack.push(new Expression(sb.toString()));
        } else {
            // priority
            // +/-: +0
            // *: +1
            // (): +2
            if (ch == '(') {
                priority += 2;
            }

            if (ch == ')') {
                priority -= 2;
            }

            if (ch == '+' || ch == '-' || ch == '*') {
                int currPriority = priority;
                if (ch == '*') {
                    currPriority++;
                }

                // evaluates until curr prority is lower than priority stack
                while (!priorityStack.isEmpty() && currPriority <= priorityStack.peek()) {
                    Expression curr = stack.pop(), prev = stack.pop();
                    priorityStack.pop();
                    stack.push(prev.eval(curr));
                }

                // assigns operator to stack top
                stack.peek().operator = ch;
                priorityStack.push(currPriority);
            }
            i++;  
        }
    }

    // evaluates the remaining expressions in stack
    while (stack.size() > 1) {
        Expression curr = stack.pop(), prev = stack.pop();
        stack.push(prev.eval(curr));
    }

    return stack.peek().toList();
}
This post is licensed under CC BY 4.0 by the author.