-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathExpression.java
More file actions
212 lines (192 loc) · 5.31 KB
/
Copy pathMathExpression.java
File metadata and controls
212 lines (192 loc) · 5.31 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
import java.lang.*;
public class MathExpression {
public static String[] opa={"+","-","x","/"};
public static String[] linearr;
public static String[] temp1;
public static String[] temp2;
public static int tcount=0;
public static int lcount=0;
public static void main(String[] args) throws FileNotFoundException {
Scanner sc=null;
try{
sc=new Scanner(new FileReader("C:\\Users\\jsc\\Desktop\\420Lab\\2\\Input.txt"));
}
catch(Exception e){
}
String line=sc.nextLine().trim();
String[] arr=line.split("\\ |\\=");
lcount++;
int j=Integer.parseInt(arr[0]);
temp1=new String[j];
temp2=new String[j];
for(int i=0;i<j;i++){
line=sc.nextLine().trim();
arr=line.split("\\ |\\=");
temp1[tcount]=arr[0];
temp2[tcount]=arr[3];
tcount++;
lcount++;
}
line=sc.nextLine().trim();
arr=line.split("\\ |\\=");
j=Integer.parseInt(arr[0]);
linearr=new String[j];
for(int i=0;i<j;i++){
line=sc.nextLine().trim();
line=line.replaceAll("\\s","");
linearr[i]=line;
}
String[] p=postFix(linearr);
expression(p);
}
public static String[] postFix (String[] a){
String[] postfix=new String[a.length];
for(int i=0;i<a.length;i++){
String line =a[i];
String[] stack;
int sp=0;
String[] output;
int op=0;
output=new String[line.length()];
stack=new String[line.length()];
for(int j=0;j<line.length();j++){
if(!check(opa,line.charAt(j))){
output[op]=Character.toString(line.charAt(j));
op++;
}
else if(sp==0){
stack[sp]=Character.toString(line.charAt(j));
sp++;
}
else{
int p1=indexOf(opa,stack[sp-1]);
int p2=indexOf(opa,Character.toString(line.charAt(j)));
if(p1%2!=1){
p1++;
}
if(p2%2!=1){
p2++;
}
if(p2>p1){
stack[sp]=Character.toString(line.charAt(j));
sp++;
}
else{
output[op]=stack[sp-1];
op++;
sp--;
stack[sp]=Character.toString(line.charAt(j));
sp++;
}
if(sp>1){
if(check(opa,stack[sp-1].charAt(0)) && check(opa,stack[sp-2].charAt(0))){
p1=indexOf(opa,stack[sp-1]);
p2=indexOf(opa,stack[sp-2]);
if(p1%2!=1){
p1++;
}
if(p2%2!=1){
p2++;
}
if(p2>=p1){
output[op]=stack[sp-2];
op++;
stack[sp-2]=stack[sp-1];
sp--;
}
}
}
}
if(j==(line.length()-1)){
for(int k=0;k<sp;k++){
//System.out.println(stack[sp-k]);
output[op]=stack[sp-1-k];
op++;
}
}
}
postfix[i]= arrToStr(output);
//System.out.println(postfix[i]);
}
return postfix;
}
public static boolean check(String[] a,char s){
for(int i=0;i<a.length;i++){
if(Character.toString(s).equals(a[i])){
return true;
}
}
return false;
}
public static int indexOf(String[] a, String c) {
int index = -1;
for (int i = 0; i < a.length; ++i) {
if (c.equals( a[i])) {
index = i;
break;
}
}
return index;
}
public static String arrToStr(String[] a){
String s="";
for(int i=0;i<a.length;i++){
s=s+a[i];
}
return s;
}
public static void expression(String[] a){
for(int i=0;i<a.length;i++){
String line =a[i];
String[] stack=new String[line.length()];
int sp=0;
for(int j=0;j<line.length();j++){
if(!check(opa,line.charAt(j))){
if(!check(temp1,line.charAt(j))){
System.out.println("Compilation Error");
break;
}
else{
stack[sp]=temp2[indexOf(temp1,Character.toString(line.charAt(j)))];
//System.out.println(stack[sp]);
sp++;
}
}
else{
if(sp<2){
System.out.println("error");
break;
}
int y=Integer.parseInt(stack[sp-1]);
sp--;
int x=Integer.parseInt(stack[sp-1]);
sp--;
int result=0;
char s=line.charAt(j);
// System.out.println(s);
switch(s){
case '+': result=x+y;
break;
case '-': result=x-y;
//System.out.println(x);
//System.out.println(y);
break;
case 'x': result=x*y;
break;
case '/': result=x/y;
break;
}
//System.out.println(result);
stack[sp]=String.valueOf(result);
if(j==line.length()-1){
System.out.println(stack[sp]);
}
sp++;
}
}
}
}
}