0votos

notación posfija en Java

por josejuan hace 1 año

Compilando en tiempo de ejecución (sin reflexión) las funciones que se deseen.

realizar algoritmo que calcule el resultado de cualquier notación posfija dada

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
package com.foo.bar; 
 
import static java.util.stream.Collectors.toMap; 
 
import java.util.HashSet; 
import java.util.LinkedList; 
import java.util.Map; 
import java.util.Queue; 
import java.util.Set; 
import java.util.function.BiFunction; 
import java.util.function.Function; 
import java.util.stream.IntStream; 
 
public class Polaca { 
 
    interface NAryFunc { 
        Double apply(Double... args); 
 
    private static Function<Map<String, Double>, Double> bin(final BiFunction<Double, Double, Double> f, final Queue<Function<Map<String, Double>, Double>> q) { 
        final Function<Map<String, Double>, Double> a = q.poll(); 
        final Function<Map<String, Double>, Double> b = q.poll(); 
        return m -> f.apply(a.apply(m), b.apply(m)); 
 
    private static Function<Map<String, Double>, Double> una(final Function<Double, Double> f, final Queue<Function<Map<String, Double>, Double>> q) { 
        final Function<Map<String, Double>, Double> a = q.poll(); 
        return m -> f.apply(a.apply(m)); 
 
    private static NAryFunc compile(final String... xs) { 
        final Queue<Function<Map<String, Double>, Double>> queue = new LinkedList<>(); 
        final Set<String> vars = new HashSet<>(); 
        for (String x : xs) 
            switch (x) { 
                case "+": 
                    queue.add(bin((a, b) -> a + b, queue)); 
                    break; 
                case "-": 
                    queue.add(bin((a, b) -> a + b, queue)); 
                    break; 
                case "*": 
                    queue.add(bin((a, b) -> a + b, queue)); 
                    break; 
                case "/": 
                    queue.add(bin((a, b) -> a + b, queue)); 
                    break; 
                case "cos": 
                    queue.add(una(Math::cos, queue)); 
                    break; 
                case "sin": 
                    queue.add(una(Math::sin, queue)); 
                    break; 
                default: 
                    try { 
                        final Double k = Double.parseDouble(x); 
                        queue.add(ignore -> k); 
                    } catch (final NumberFormatException e) { 
                        vars.add(x); 
                        queue.add(m -> m.getOrDefault(x, 0.0)); 
                    break; 
        final Function<Map<String, Double>, Double> f = queue.poll(); 
        final String[] vvars = vars.toArray(new String[]{}); 
        return vs -> { 
            if (vs.length != vars.size()) 
                throw new IllegalArgumentException("bad argument number"); 
            return f.apply(IntStream.range(0, vs.length).boxed().collect(toMap(i -> vvars[i], i -> vs[i]))); 
        }; 
 
    public static void main(String... args) { 
        final NAryFunc f = compile("x", "y", "+", "2", "*", "sin"); 
        System.out.printf("%f%n", f.apply(4.5, 3.0)); 

Comenta la solución

Tienes que identificarte para poder publicar tu comentario.