Jak stworzyć kalkulator ? (hex)

0

Witam potrzebny mi jest poradnik bądź jakieś nakierowanie jak i w jakim języku stworzyć najprostszy kalkulator.
Ma on odczytywać wartości z plików binarnych. Np.

user image

na przykład linijki 038 ma odczytać 2 komórki, a następnie na przykład je zamienić na inne liczby bądź wykonać operację logiczne (xor) oraz dodawanie odejmowanie itd.

jakim języku najprościej stworzyć takie coś ?

2

jakimkolwiek

0

Jakiś poradnik czy coś ? Gotowa baza komend ?

0
spartanPAGE napisał(a):

jakimkolwiek

Tak jak kolega napisał.

A jak taki kalkulator mógłby działać na przykładzie Javy? Choćby tak:

  1. Wczytujesz plik.
  2. Metodą double getValue(int column, int row), wyciągasz interesujące Cię wartości z wgranego pliku i zamieniasz je np. na double.
  3. W metodzie double calculate(String statement) podajesz wyrażenie, które chcesz policzyć, np. getValue(2, 3) + "+" + getValue(1, 4).
    3.1 Konwertujesz wyrażenie na wyrażenie w ONP (http://pl.wikipedia.org/wiki/Odwrotna_notacja_polska).
    3.2 Wykonujesz obliczenia.
    3.3 Zwracasz wynik.

Tadam :)

0

A byłby ktoś w stanie na zlecenie wykonac taki kalkulator ?

Jeżeli tak to proszę o napisanie posta tutaj.

0

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
0

znajdzie się jakiś przykład w VisualBasic?

1 użytkowników online, w tym zalogowanych: 0, gości: 1