View difference between Paste ID: JyEStpqX and kBZcNynJ
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
13
	public static void main (String[] args){
14
		operator=new Stack<Operator>();
15
		operand=new Stack<Double>();
16
		System.out.println("Skriv in ditt tal");
17
		string=sc.nextLine();
18
19
		String[] in=string.split(" ");
20
		tilldela(in);
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
				new Minus().hantera(operator, operand);
36
				break;
37
38
			case "*":
39
				new Multi().hantera(operator, operand);
40
				break;
41
42
			case "/":
43
				new Division().hantera(operator, operand);
44
				break;
45
46
			case "(":
47
				new VParantes().hantera(operator, operand);
48
				break;
49
50
			case ")":
51
				break;
52
			default:
53
				operand.push(Double.parseDouble(s));
54
				break;
55
			}
56
57
		}
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
		
92
	}
93
94
}
95
96
97
98
package projekt;
99
100
public class HParantes extends Operator{
101
102
	@Override
103
	public int prioritet() {
104
		// TODO Auto-generated method stub
105
		return 0;
106
	}
107
108
	@Override
109
	public void evaluera(Stack operand) {
110
		// TODO Auto-generated method stub
111
		
112
	}
113
114
	@Override
115
	public void hantera(Stack operator, Stack operand) {
116
		while(operator.empty()!=true&&((Operator) operator.top()).prioritet()!=3){
117
			((Operator) operator.pop()).evaluera(operand);
118
		}
119
		operator.push(this);
120
	}
121
122
}