-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calc.java
104 lines (93 loc) · 3.1 KB
/
Calc.java
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
package St;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
class num{
String a;
int b;
num(String a, int b){
this.a = a;
this.b = b;
}
String print(){
return "<" + a + ">: " + b;
}
public boolean equals(Object o){
num toCheck = (num)o;
return this.a.equals(toCheck.a);
}
}
public class Calc{
//Main functional.
private Properties commandsNames = null;
private Scanner scan = null;
private Stack stack = null;
private Parser parser = null;
{
FileInputStream in_d = null;
commandsNames = new Properties();
try {
in_d = new FileInputStream("E:\\Proga\\Stack\\src\\St\\LoadClassesNames.properties");
commandsNames.load(in_d);
} catch (IOException e) {
e.printStackTrace();
}
stack = new Stack();
parser = new Parser();
}
String getProperty(String s) {
return commandsNames.getProperty(s);
}
Calc() {
this.scan = new Scanner(System.in);
}
Calc(String s) {
this.scan = new Scanner(s);
}
void calculations() {
while (scan.hasNext()) {
String s = scan.nextLine();
if (s.equals("EXIT")) {
break;
}
//parsing
Scanner strings = new Scanner(s);
ArrayList<String> ArrStr = new ArrayList<>();
while (strings.hasNext()) {
String tmp = strings.next();
if(!tmp.contains("#"))
ArrStr.add(tmp);
else {
tmp = (tmp.indexOf('#') == 0) ? "#" : tmp.substring(0, tmp.indexOf('#'));
ArrStr.add(tmp);
break;
}
}
int i = 0;
String commandName = null;
while (i < ArrStr.size()) {
if (this.getProperty(ArrStr.get(i)) != null) {
commandName = this.getProperty(ArrStr.get(i));
break;
} else if(ArrStr.get(i).substring(0,1).equals("#")){
commandName ="#";
break;
}
i++;
}
ArrStr.remove((String)"#");
if(commandName == null){
System.out.println("WrongCommand");
continue;
}
if (commandName.equals("#")) {
continue;
}
stack.setNumStr(ArrStr);
//call parser
parser.parsing(stack, commandName);
}
}
}