-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.java
164 lines (139 loc) · 5 KB
/
Core.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Created by root on 11/18/16.
* Author: Rishi Javia
*/
public class Core extends Stack{
private Stack myStack;
public Core(String filename){
myStack = new Stack();
try {
Scanner input = new Scanner(new File(filename));
while(input.hasNext()){
Scanner line = new Scanner(input.nextLine());
while(line.hasNext()){
String word = line.next();
switch (word){
case "pop": myStack.pop(); break;
case "exch": exch(); break;
case "dup": dup(); break;
case "pstack": myStack.print(); break;
case "index": index(); break;
case "copy": copy(); break;
case "roll": roll(); break;
case "add": add(); break;
case "sub": sub(); break;
case "mul": mul(); break;
case "div": div(); break;
case "idiv": idiv(); break;
case "mod": mod(); break;
case "sqrt": sqrt(); break;
case "sin": sin(); break;
case "cos": cos(); break;
default: myStack.push(word); break;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void add() {
double value1 = Double.parseDouble(myStack.pop());
double value2 = Double.parseDouble(myStack.pop());
myStack.push(String.valueOf(Math.round((value2 + value1)*10000.0)/10000.0));
}
private void sub() {
double value1 = Double.parseDouble(myStack.pop());
double value2 = Double.parseDouble(myStack.pop());
myStack.push(String.valueOf(Math.round((value2 - value1)*10000.0)/10000.0));
}
private void mul() {
double value1 = Double.parseDouble(myStack.pop());
double value2 = Double.parseDouble(myStack.pop());
myStack.push(String.valueOf(Math.round((value2 * value1)*10000.0)/10000.0));
}
private void div() {
double value1 = Double.parseDouble(myStack.pop());
double value2 = Double.parseDouble(myStack.pop());
myStack.push(String.valueOf(Math.round((value2/value1)*10000.0)/10000.0));
}
private void idiv() {
int value1 = Integer.parseInt(myStack.pop());
int value2 = Integer.parseInt(myStack.pop());
myStack.push(String.valueOf(value2/value1));
}
private void mod() {
int value1 = Integer.parseInt(myStack.pop());
int value2 = Integer.parseInt(myStack.pop());
myStack.push(String.valueOf(value2%value1));
}
private void sqrt() {
double value1 = Double.parseDouble(myStack.pop());
myStack.push(String.valueOf(Math.sqrt(value1)));
}
private void sin() {
double value1 = Double.parseDouble(myStack.pop());
myStack.push(String.valueOf(Math.sin(Math.toRadians(value1))));
}
private void cos() {
double value1 = Double.parseDouble(myStack.pop());
myStack.push(String.valueOf(Math.cos(Math.toRadians(value1))));
}
private void roll() {
int places = Integer.parseInt(myStack.pop());
int elements = Integer.parseInt(myStack.pop());
if (places == 0 || elements == 0){
return;
}
String[] arr = new String[elements];
int counter = 0;
for(int i= elements - 1; i >= 0; i--){
arr[counter++] = myStack.peek(i);
}
for(int j= 0; j < places; j++){
String lastElement = arr[elements - 1];
for(int i= elements - 2; i >= 0; i--){
arr[i+1] = arr[i];
}
arr[0] = lastElement;
}
for(int i= 0; i<elements; i++){
myStack.pop();
}
for(int i=0; i < elements; i++){
myStack.push(arr[i]);
}
}
private void copy() {
int size = Integer.parseInt(myStack.pop());
String[] arr = new String[size];
int counter = 0;
for(int i= size - 1; i >= 0; i--){
arr[counter++] = myStack.peek(i);
}
for(int i=0; i < size; i++){
myStack.push(arr[i]);
}
}
private void index() {
myStack.push(myStack.peek(Integer.parseInt(myStack.pop())));
}
private void dup() {
myStack.push(myStack.peek(0));
}
private void exch() {
String value1 = myStack.pop();
String value2 = myStack.pop();
myStack.push(value1);
myStack.push(value2);
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter name of a file: ");
String filename = reader.next();
Core intpt = new Core (filename);
}
}