View difference between Paste ID: kBZcNynJ and LpPih8cG
SHOW: | | - or go back to the newest paste.
1
package projekt;
2
3
import java.util.Scanner;
4
5
public class Main {
6
7
	static Stack<Operator> operator;
8
	static Stack<Double> operand;
9
	static Scanner sc=new Scanner(System.in);
10
	static String string;
11
12-
	public void evaluera(Stack operand) {
12+
13-
		double ett=Double.parseDouble((String)operand.pop());
13+
14-
		double två=Double.parseDouble((String)operand.pop());
14+
		operator=new Stack<Operator>();
15-
		operand.push(ett+två);
15+
		operand=new Stack<Double>();
16
		System.out.println("Skriv in ditt tal");
17
		string=sc.nextLine();
18
19
		String[] in=string.split(" ");
20-
	public void hantera(Stack operator, Stack operand) {
20+
21
		System.out.println(operand.pop());
22
23
24
	}
25
26
	public static void tilldela(String[] lista){
27
		for(String s: lista){
28
			switch(s){
29
30
			case "+":
31
				new Plus().hantera(operator, operand);
32
				break;
33
34
			case "-":
35-
MAIN 
35+
36
				break;
37
38
			case "*":
39
				new Multi().hantera(operator, operand);
40
				break;
41
42
			case "/":
43
				new Division().hantera(operator, operand);
44-
	static Stack operator;
44+
45-
	static Stack operand;
45+
46
			case "(":
47
				new VParantes().hantera(operator, operand);
48
				break;
49
50
			case ")":
51-
		operator=new Stack();
51+
52-
		operand=new Stack();
52+
53
				operand.push(Double.parseDouble(s));
54
				break;
55
			}
56
57
		}
58-
		System.out.println(operand.top());
58+
59
60
	}
61
}
62
63
64
65
66
package projekt;
67
68
public class Plus extends Operator {
69
70
	@Override
71
	public int prioritet() {
72
		// TODO Auto-generated method stub
73
		return 2;
74
	}
75
76
	@Override
77
	public void evaluera(Stack<Double> operand) {
78
		double ett=operand.pop();
79
		double två=operand.pop();
80
		operand.push((ett+två));
81
		
82
	}
83
84
	@Override
85
	public void hantera(Stack<Operator> operator, Stack<Double> operand) {
86
		while(operator.empty()!=true&&((Operator) operator.top()).prioritet()>=this.prioritet()){
87
			 ((Operator) operator.pop()).evaluera(operand);
88
		}
89
90
		operator.push(this);
91-
				operand.push(s);
91+
92
	}
93
94
}