Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import oving2.ArgumentStack;
- import java.util.Scanner;
- void performOperation(char op) {
- double n1;
- double n2;
- switch (op) {
- case '+': n2 = ArgumentStack.pop(0.0);
- n1 = ArgumentStack.pop(0.0);
- ArgumentStack.push(n1+n2);
- break;
- case '-': n2 = ArgumentStack.pop(0.0);
- n1 = ArgumentStack.pop(0.0);
- ArgumentStack.push(n1-n2);
- break;
- case '*': n2 = ArgumentStack.pop(1.0);
- n1 = ArgumentStack.pop(1.0);
- ArgumentStack.push(n1*n2);
- break;
- case '/': n2 = ArgumentStack.pop(1.0);
- n1 = ArgumentStack.pop(1.0);
- if (n2==0) {
- System.out.println("Du kan ikke dele pƄ 0");
- break;
- }
- else {
- ArgumentStack.push(n1/n2);
- break;
- }
- case ',': ArgumentStack.push(ArgumentStack.peek(0.0));
- break;
- case '.': ArgumentStack.pop(0.0);
- break;
- case '~': if (ArgumentStack.arguments.length>=2) {
- n2 = ArgumentStack.pop(0.0);
- n1 = ArgumentStack.pop(0.0);
- ArgumentStack.push(n1);
- ArgumentStack.push(n2);
- }
- break;
- default: System.err.println("Ukjent operasjon");
- break;
- };
- }
- void handleInput(String input) {
- char c = input.charAt(0);
- if (Character.isDigit(c)) {
- double inputDigit = Double.parseDouble(input);
- ArgumentStack.push(inputDigit);
- }
- else {
- }
- performOperation(c);
- }
- void splitAndHandleInput(String input) {
- String[] inputSplit = input.split(" ");
- for (int i=0; i<inputSplit.length; i++); {
- handleInput(inputSplit[i]);
- }
- }
- Scanner sc = new Scanner(System.in);
- while (sc.hasNextLine()) {
- String input = sc.nextLine().trim();
- if (input.length()==0) {
- break;
- }
- splitAndHandleInput(input);
- ArgumentStack.print("{",",","}");
- }
Add Comment
Please, Sign In to add comment