-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolverExt.java
More file actions
127 lines (113 loc) · 4.19 KB
/
Copy pathSolverExt.java
File metadata and controls
127 lines (113 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package seminar1;
import seminar1.collections.ArrayStack;
import seminar1.collections.IStack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) = 101
* ( 1 + ( 5 * ( 4 * 5 ) ) )
* ( 1 + ( 5 * 20 ) ) = 101
* ( 1 + 100 ) = 101
*
* 1 + ( 2 + 3 ) * 4 * 5 = 101
* 1 + 5 * 4 * 5 = 101
* 1 + 5 * 20 = 101
* 1 + 100 = 101
* 20 / 4 = 5
* (101 - 1) / 5 = 20
*
* Считаем, что операции деления на ноль отсутствуют
*/
public class SolverExt {
private static final String QUIT = "q";
private static final char LEFT_PAREN = '(';
private static final char RIGHT_PAREN = ')';
private static final char PLUS = '+';
private static final char MINUS = '-';
private static final char TIMES = '*';
private static final char DIVISION = '/';
private static double eval(char operation,ArrayStack<Double> oper){
switch(operation){
case MINUS:
return (-oper.pop()+oper.pop());
case PLUS :
return (oper.pop()+oper.pop());
case DIVISION:
double tmp=oper.pop();
return (oper.pop()/tmp);
case TIMES:
return oper.pop()*oper.pop();
default:return 0;
}
}
private static double evaluate(String[] values) {
ArrayStack<Integer> operators=new ArrayStack<>();
ArrayStack<Double> operands=new ArrayStack<>();
Scanner str=new Scanner(values[0]);
double res;
char tmp=0;
while(str.hasNext()){
if(str.hasNextInt()){
operands.push(str.nextDouble());
continue;
}
if(str.hasNext()){
tmp=str.next().charAt(0);
}
switch (tmp) {
case LEFT_PAREN:
operators.push((int) LEFT_PAREN);
break;
case RIGHT_PAREN:
while(operators.show()!=LEFT_PAREN){
operands.push(eval((char)(int)operators.show(),operands));
operators.pop();}
operators.pop();
break;
case PLUS:
if(operators.size()!=0&&(operators.show()==TIMES||operators.show()==DIVISION))
{
operands.push(eval((char)(int)operators.show(),operands));
operators.pop();}
operators.push((int)PLUS);
break;
case MINUS:
if(operators.size()!=0&&(operators.show()==TIMES||operators.show()==DIVISION))
{
operands.push(eval((char)(int)operators.show(),operands));
operators.pop();}
operators.push((int)MINUS);
break;
case TIMES:
if(operators.size()!=0&&(operators.show()==TIMES||operators.show()==DIVISION))
{
operands.push(eval((char)(int)operators.show(),operands));
operators.pop();}
operators.push((int)TIMES);
break;
case DIVISION:
if(operators.size()!=0&&(operators.show()==TIMES||operators.show()==DIVISION))
{
operands.push(eval((char)(int)operators.show(),operands));
operators.pop();}
operators.push((int)DIVISION);
break;
}
}
while(operands.size()!=1)
operands.push(eval((char)(int)operators.pop(),operands));
return operands.pop();
}
public static void main(String[] args) {
try (BufferedReader lineReader = new BufferedReader(new InputStreamReader(System.in))) {
String sequence;
while (!QUIT.equals(sequence = lineReader.readLine())) {
System.out.println(evaluate(sequence.split("\n")));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}