Skip to content

Commit a6cfda5

Browse files
committed
RPN Generator
1 parent f1247fa commit a6cfda5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1732
-22
lines changed

ArrayStackT03_17/.idea/workspace.xml

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ArrayStackT03_17/Exercise4.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package inter;
2+
3+
public class Exercise4 {
4+
public static void main(String[] args) {
5+
RPNGenerator rpn = new RPNGenerator(256);
6+
String onp = rpn.generate("D:\\CodeProjects\\DataStructures\\DataStructuresJava\\ArrayStackT03_17\\expression01.txt");
7+
System.out.println(onp);
8+
}
9+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package inter;
2+
3+
import javax.naming.OperationNotSupportedException;
4+
import java.util.EmptyStackException;
5+
import java.util.Scanner;
6+
import java.util.zip.DataFormatException;
7+
8+
public class RPNCalculator
9+
{
10+
private TArrayStack<Integer> stack;
11+
12+
public RPNCalculator(int stackSize)
13+
{
14+
stack = new TArrayStack<Integer>(stackSize > 126 ? stackSize : 256);
15+
}
16+
17+
public int compute(String rpn) throws OperationNotSupportedException, DataFormatException{
18+
Scanner sc = new Scanner(rpn);
19+
int result = 0;
20+
while(sc.hasNext()){
21+
if(sc.hasNextInt()){
22+
stack.push(sc.nextInt());
23+
}else{
24+
String word= sc.next();
25+
char sign = word.charAt(0);
26+
if(operation(sign)){
27+
executeOperation(sign);
28+
}else{
29+
throw new OperationNotSupportedException();
30+
}
31+
}
32+
}
33+
if(!stack.isEmpty())
34+
result = stack.pop();
35+
if(!stack.isEmpty())
36+
throw new DataFormatException();
37+
return result;
38+
}
39+
40+
private void executeOperation(char sign) throws OperationNotSupportedException{
41+
int a1, a2;
42+
if(!stack.isEmpty()){
43+
a2 = stack.pop();
44+
}else{
45+
throw new EmptyStackException();
46+
}
47+
if(!stack.isEmpty()){
48+
a1 = stack.pop();
49+
}else{
50+
throw new EmptyStackException();
51+
}
52+
53+
switch(sign){
54+
case '+':
55+
stack.push(a1 + a2);
56+
return;
57+
case '-':
58+
stack.push(a1 - a2);
59+
return;
60+
case '*':
61+
stack.push(a1 * a2);
62+
return;
63+
case '/':
64+
if(a2!=0)
65+
stack.push(a1 / a2);
66+
else
67+
throw new ArithmeticException();
68+
return;
69+
70+
case '%':
71+
if(a2!=0)
72+
stack.push(a1 % a2);
73+
else
74+
throw new ArithmeticException();
75+
return;
76+
default:
77+
throw new OperationNotSupportedException();
78+
}
79+
}
80+
81+
private boolean operation(char sign)
82+
{
83+
switch (sign)
84+
{
85+
case '+':
86+
case '-':
87+
case '/':
88+
case '%':
89+
case '*': return true;
90+
default: return false;
91+
}
92+
}
93+
}

ArrayStackT03_17/RPNGenerator.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package inter;
2+
3+
import javax.naming.OperationNotSupportedException;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.util.Scanner;
8+
import java.util.zip.DataFormatException;
9+
10+
public class RPNGenerator {
11+
private TArrayStack<Character> stack;
12+
private StringBuilder str;
13+
14+
public RPNGenerator(int stackSize){
15+
stack = new TArrayStack<Character>(stackSize > 126 ? stackSize : 256);
16+
str = new StringBuilder();
17+
}
18+
19+
public String generate(String fileName) {
20+
str.setLength(0);
21+
try (FileInputStream fin = new FileInputStream(fileName)) {
22+
Scanner sc = new Scanner(fin);
23+
while (sc.hasNext()) {
24+
String word = sc.next();
25+
char sign = word.charAt(0);
26+
if (sign == '(') {
27+
stack.push(sign);
28+
} else if (sign == ')') {
29+
removeUntillBracker();
30+
System.out.println("Stack contents: " + stack.toString()); // <-- add this line
31+
System.out.println("StringBuilder contents: " + str.toString()); // <-- add this line
32+
} else if (numberOrVariable(sign)) {
33+
str.append(word).append(" ");
34+
} else if (operation(sign)) {
35+
pushOperationToStack(sign);
36+
System.out.println("Stack contents: " + stack.toString()); // <-- add this line
37+
System.out.println("StringBuilder contents: " + str.toString()); // <-- add this line
38+
} else {
39+
throw new DataFormatException();
40+
}
41+
}
42+
while (!stack.isEmpty()) {
43+
str.append((stack.pop())).append(" ");
44+
}
45+
return str.toString();
46+
} catch (FileNotFoundException e) {
47+
e.printStackTrace();
48+
} catch (DataFormatException e) {
49+
e.printStackTrace();
50+
} catch (OperationNotSupportedException e) {
51+
e.printStackTrace();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
return "err";
56+
}
57+
58+
private void pushOperationToStack(char sign) throws OperationNotSupportedException{
59+
while(!stack.isEmpty()){
60+
if(priority(sign) > priority(stack.top())){
61+
stack.push(sign);
62+
return;
63+
}else{
64+
str.append(stack.pop()).append(" ");
65+
}
66+
}
67+
stack.push(sign);
68+
}
69+
70+
private int priority(char sign) throws OperationNotSupportedException{
71+
switch(sign){
72+
case '(': return 0;
73+
case '+':
74+
case '-':
75+
case ')': return 1;
76+
case '*':
77+
case '/':
78+
case '%': return 2;
79+
default:throw new OperationNotSupportedException();
80+
}
81+
}
82+
83+
private boolean operation(char sign){
84+
switch(sign){
85+
case '+':
86+
case '-':
87+
case '/':
88+
case '%':
89+
case '*': return true;
90+
default: return false;
91+
}
92+
}
93+
94+
private boolean numberOrVariable(char sign){
95+
if(Character.isLetter(sign)||Character.isDigit(sign))
96+
return true;
97+
else
98+
return false;
99+
}
100+
101+
public void removeUntillBracker() throws DataFormatException{
102+
while(!stack.isEmpty() ){
103+
if(stack.top() == '(' ){
104+
stack.pop();
105+
return;
106+
}else{
107+
str.append(stack.pop()).append(" ");
108+
}
109+
}
110+
throw new DataFormatException();
111+
}
112+
}

0 commit comments

Comments
 (0)