APL/DEV 3.2/TP07/Variables/Variables.java
2022-11-24 15:17:33 +01:00

55 lines
1.7 KiB
Java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
/**
* Arithmetique
*/
public class Variables {
public static void main(String[] args) {
ArrayDeque<Node> pile = new ArrayDeque<>();
HashMap<String, Integer> ids = new HashMap<>();
for (String arg : args) {
try {
Integer.parseInt(arg);
pile.addFirst(new Node(arg));
} catch (NumberFormatException e) {
if (Character.isUpperCase(arg.charAt(0))) {
pile.addFirst(new Node(arg));
ids.put(arg, 0);
} else {
if (pile.size() < 2) {
System.err.println("Invalid stack size.");
return;
}
Node n2 = pile.pollFirst();
Node n1 = pile.pollFirst();
Node n3 = new Node(arg);
n3.addSubNode(n1);
n3.addSubNode(n2);
pile.addFirst(n3);
}
}
}
Node firstNode = pile.pollFirst();
System.out.println(firstNode.showNode());
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
for (Map.Entry<String, Integer> entry : ids.entrySet()) {
System.out.print(entry.getKey() + " ? ");
ids.put(entry.getKey(), Integer.parseInt(br.readLine()));
}
} catch (Exception e) {
System.out.println(e);
}
System.out.println("= " + firstNode.getResult(ids));
}
}