View difference between Paste ID: LpPih8cG and tM9xfiXT
SHOW: | | - or go back to the newest paste.
1
package projekt;
2
3
public class Plus extends Operator {
4
5
	@Override
6
	public int prioritet() {
7
		// TODO Auto-generated method stub
8
		return 2;
9
	}
10
11
	@Override
12
	public void evaluera(Stack operand) {
13
		double ett=Double.parseDouble((String)operand.pop());
14
		double två=Double.parseDouble((String)operand.pop());
15
		operand.push(ett+två);
16
		
17
	}
18
19
	@Override
20
	public void hantera(Stack operator, Stack operand) {
21
		while(operator.empty()!=true&&((Operator) operator.top()).prioritet()>=this.prioritet()){
22
			 ((Operator) operator.pop()).evaluera(operand);
23
		}
24
25
		operator.push(this);
26
		
27
	}
28
29
}
30
31
32
33
34
35
MAIN 
36
37
38
package projekt;
39
40
import java.util.Scanner;
41
42
public class Main {
43
44
	static Stack operator;
45
	static Stack operand;
46
	static Scanner sc=new Scanner(System.in);
47
	static String string;
48
49
50
	public static void main (String[] args){
51
		operator=new Stack();
52
		operand=new Stack();
53
		System.out.println("Skriv in ditt tal");
54
		string=sc.nextLine();
55
56
		String[] in=string.split(" ");
57
		tilldela(in);
58
		System.out.println(operand.top());
59
		
60
61
62
	}
63
64
	public static void tilldela(String[] lista){
65
		for(String s: lista){
66
			switch(s){
67
68
			case "+":
69
				new Plus().hantera(operator, operand);
70
				break;
71
72
			case "-":
73
				new Minus().hantera(operator, operand);
74
				break;
75
76
			case "*":
77
				new Multi().hantera(operator, operand);
78
				break;
79
80
			case "/":
81
				new Division().hantera(operator, operand);
82
				break;
83
84
			case "(":
85
				new VParantes().hantera(operator, operand);
86
				break;
87
88
			case ")":
89
				break;
90
			default:
91
				operand.push(s);
92
				break;
93
			}
94
95
		}
96
97
98
	}
99
}
100
101
102
package projekt;
103
104
import java.util.LinkedList;
105
106
public class Stack<E> {
107
108
	LinkedList<E> stack=new LinkedList<E>();
109
110
	public void push(E item) {
111
		stack.addLast(item);
112
	}
113
114
	public E pop() {
115
		return stack.removeLast();
116
	}
117
118
	public E top() {
119
		return stack.peek();
120
	}
121
122
	public Boolean empty() {
123
		return stack.size() == 0;
124
	}
125
	
126
	public void clear(){
127
		stack.clear();
128
	}
129
	
130
}