Last active
June 2, 2018 19:11
-
-
Save michalwa/5e021f07bb8f2c5ab0816aae2a78d5ff to your computer and use it in GitHub Desktop.
RPN Variable Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 0.5 2 | |
x x 1 + * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Scanner; | |
import java.util.Stack; | |
/** Parsuje i oblicza wartosci wyrazenia ONP. | |
* (RPN - ang. Reverse Polish Notation) */ | |
public class RPN | |
{ | |
/** Reprezentuje operator w wyrazeniu ONP */ | |
public static interface Operator | |
{ | |
/** Zwraca wynik operacji na podanych liczbach */ | |
public double eval(double a, double b); | |
} | |
private static HashMap<String, Operator> ops = new HashMap<>(); | |
private static HashMap<String, Double> vars = new HashMap<>(); | |
/** Dodaje nowy operator */ | |
public static void createOperator(String str, Operator op) | |
{ | |
ops.put(str, op); | |
} | |
/** Tworzy albo przypisuje wartosc zmiennej | |
* dostepnej w wyrazeniu ONP pod podana nazwa */ | |
public static void setVariable(String name, double value) | |
{ | |
vars.put(name, value); | |
} | |
/** Oblicza i zwraca wartosc podanego wyrazenia ONP */ | |
public static double eval(String rpn) | |
{ | |
Scanner scanner = new Scanner(rpn); | |
Stack<Double> stack = new Stack<Double>(); | |
while(scanner.hasNext()) { | |
if(scanner.hasNextDouble()) { | |
stack.push(scanner.nextDouble()); | |
} else { | |
String key = scanner.next(); | |
if(vars.containsKey(key)) { | |
stack.push(vars.get(key)); | |
} else if(ops.containsKey(key)) { | |
if(stack.size() >= 2) { | |
double b = stack.pop(); | |
double a = stack.pop(); | |
stack.push(ops.get(key).eval(a, b)); | |
} | |
} | |
} | |
} | |
scanner.close(); | |
if(stack.size() != 1) { | |
// Jesli na stosie nie pozostala wylacznie | |
// jedna liczba, wyrazenie ONP jest nieprawidlowe. | |
// Zwykle w takiej sytuacji wyrzucilibysmy wyjatek, | |
// ale na potrzeby tego przykladu pominemy ten krok | |
return 0.0; | |
} | |
return stack.peek(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0.0 -> 0.0 | |
0.5 -> 0.75 | |
1.0 -> 2.0 | |
1.5 -> 3.75 | |
2.0 -> 6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.Paths; | |
import java.util.Locale; | |
import java.util.Scanner; | |
public class Zadanie | |
{ | |
// Nazwa pliku z danymi | |
private static final String FILENAME = "/dane.txt"; | |
public static void main(String[] args) | |
{ | |
// Wczytaj wszystkie dane z pliku | |
Scanner scanner = new Scanner(Zadanie.class.getResourceAsStream(FILENAME)); | |
String line1 = scanner.nextLine(); | |
String rpn = scanner.nextLine(); | |
scanner.close(); | |
// Sparsuj trzy liczby z pierwszej linii pliku | |
scanner = new Scanner(line1); | |
// Konieczne jest ustawienie amerykanskiego | |
// stylu zapisu liczb, aby kropki byly traktowane, | |
// jako przecinki w liczbach typu double | |
scanner.useLocale(Locale.US); | |
double min = scanner.nextDouble(); | |
double step = scanner.nextDouble(); | |
double max = scanner.nextDouble(); | |
scanner.close(); | |
// Stworz operatory | |
RPN.createOperator("+", (a, b) -> a + b); | |
RPN.createOperator("-", (a, b) -> a - b); | |
RPN.createOperator("*", (a, b) -> a * b); | |
RPN.createOperator("/", (a, b) -> a / b); | |
// Przeiteruj przez wszystkie wartosci | |
for(int i = 0; min + i * step <= max; i ++) { | |
double x = min + i * step; | |
// Oblicz i wydrukuj wartosc wyrazenia dla x | |
RPN.setVariable("x", x); | |
double result = RPN.eval(rpn); | |
System.out.println(x + " -> " + result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment