Przykład napisany w Javie oparty na ONP, który można rozwinąć i dopasować do swoich potrzeb:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
public class HexCalculator {
public static void main(String[] args) {
/* READ THE FILE */
BufferedReader reader;
ArrayList<String> hexValues = new ArrayList<String>();
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("HexCalculatorData.txt")));
hexValues = new ArrayList<String>();
String line = "";
while ((line = reader.readLine()) != null) {
hexValues.add(line);
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
/* TEST */
int v1 = getValue(hexValues, 2, 2);
int v2 = getValue(hexValues, 2, 3);
int v3 = getValue(hexValues, 1, 3);
int v4 = getValue(hexValues, 3, 1);
System.out.println("V1: " + v1);
System.out.println("V2: " + v2);
System.out.println("V3: " + v3);
System.out.println("V4: " + v4);
String statement = v1 + " + " + v2 + " - " + v3 + " + " + v4;
System.out.println(statement);
System.out.println("Result: " + calculate(statement));
}
/**
* Get INT value from its HEX representation.
* @param values
* @param column
* @param row
* @return
*/
public static int getValue(ArrayList<String> values, int column, int row) {
String myRow = values.get(row);
String[] myRowSplitted = myRow.split("\t");
return Integer.parseInt(myRowSplitted[column], 16);
}
/**
* Calculate the "statement" basen on the Reverse Polish Notation (RNP).
* @param statement - separate consecutive values and operators with one \s (space).
*/
public static double calculate(String statement) {
String RNP = "";
OperatorPriority op = new OperatorPriority();
Stack<String> stack = new Stack<String>();
StringTokenizer st = new StringTokenizer(statement, " ");
while (st.hasMoreTokens()) {
String s = st.nextToken();
if (s.equals("+") || s.equals("*") || s.equals("-") || s.equals("/")) {
while (!stack.empty() && op.getPriorityOf(stack.peek()) >= op.getPriorityOf(s)) {
RNP += stack.pop() + " ";
}
stack.push(s);
} else {
RNP += s + " ";
}
}
while (!stack.empty()) {
RNP += stack.pop() + " ";
}
Stack<Double> stackDouble = new Stack<Double>();
StringTokenizer stDouble = new StringTokenizer(RNP, " ");
while (stDouble.hasMoreTokens()) {
String sDouble = stDouble.nextToken();
if (!sDouble.equals("+") && !sDouble.equals("*") && !sDouble.equals("-") && !sDouble.equals("/")) {
double value = Double.parseDouble(sDouble);
stackDouble.push(value);
} else {
double v1 = stackDouble.pop();
double v2 = stackDouble.pop();
switch (sDouble.charAt(0)) {
case '*':
stackDouble.push(v2 * v1);
break;
case '+':
stackDouble.push(v2 + v1);
break;
case '-':
stackDouble.push(v2 - v1);
break;
case '/':
stackDouble.push(v2 / v1);
break;
}
}
}
double result = stackDouble.pop();
return result;
}
private static class OperatorPriority {
private int getPriorityOf(String operator) {
if (operator.equals("+") || operator.equals("-")) {
return 1;
} else if (operator.equals("*") || operator.equals("/")) {
return 2;
} else {
return 0;
}
}
}
}
I przykładowy plik *.txt
. Wartości są rozdzielone \t
(tabem).
3A 4F 5C 1D
1E 2A 3B 4E
3A 4F 5C 1D
1E 2A 3B 4E