- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStackDemoClass.java
More file actions
Latest commit
240 lines (230 loc) · 7.21 KB
/
Copy pathLinkedStackDemoClass.java
File metadata and controls
240 lines (230 loc) · 7.21 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//Alec Arroyo
/**
* Infix to Postfix converter
* Postfix to Infix converter
* Prints Equations
* Exits program
**/
importjava.util.Scanner;
importjava.util.Stack;
publicclassLinkedStackDemoClass<T> extendsLinkedStackClass<T>
{
publicstaticvoidmain(String[] args)
{
/** Declared all stacks and linked lists **/
//Stack for num = 1
LinkedStackClass<String> stack = newLinkedStackClass();
//Stack for num = 2
LinkedStackClass<String> s = newLinkedStackClass();
//Stack linked list to print all equaitons
LinkedStackClass<String> allEqu = newLinkedStackClass();
//Stack linked list to print all conversions
LinkedStackClass<String> linked = newLinkedStackClass();
//Declared string variables
Stringvalue1, end;
//Declared num variable; holds user input for number option.
intnum = 0;
//Declared string
Stringlinkval = "";
Scannerkeyboard = newScanner(System.in);
//Keep looping until user wants to exit(4)
while (num != 4)
{
Stringword1 = "";
Stringword2 = "";
charc = 'a';
charp = 'b';
booleanvalue = stack.isEmptyStack();
//Asks user to input option number
System.out.println();
System.out.println("Please select what type of conversion you would like to do: ");
System.out.println("1.Infix to postfix");
System.out.println("2.Postfix to infix");
System.out.println("3.Print Equations");
System.out.println("4.Exit");
num = keyboard.nextInt();
//Option #1
if(num == 1)
{
System.out.println("Enter equation without the x = portion of it for infix.(add ; at end) ");
word1 = keyboard.next();
//Error checking if user puts semicolon at end
if(!word1.contains(";"))
{
System.out.println("Error no ; variable at the end of equation. Try again");
System.exit(0);
}
//Push equation onto stack
allEqu.push(word1);
intnext = 0;
for(inti = 0; i <= next; i++)
{
c = word1.charAt(i);
if(c != ';')
{
if(c == '^' || c == '*' || c == '/' || c == '+' || c == '-')
{
if(value == true)
{
Stringa_letter = Character.toString(c);
stack.push(a_letter);
value = false;
}
elseif(value == false)
{
Stringa_letter = Character.toString(c);
if(stack.peek().equals(a_letter))
{
System.out.print(stack.peek());
linkval = linkval + stack.peek();
stack.pop();
stack.push(a_letter);
}
elseif(stack.peek().equals("+") && a_letter.equals("-"))
{
System.out.print(stack.peek());
linkval = linkval + stack.peek();
stack.pop();
stack.push(a_letter);
}
elseif(stack.peek().equals("-") && a_letter.equals("+"))
{
System.out.print(stack.peek());
linkval = linkval + stack.peek();
stack.pop();
stack.push(a_letter);
}
elseif(stack.peek().equals("*") && a_letter.equals("/"))
{
System.out.print(stack.peek());
linkval = linkval + stack.peek();
stack.pop();
stack.push(a_letter);
}
elseif(stack.peek().equals("/") && a_letter.equals("*"))
{
System.out.print(stack.peek());
linkval = linkval + stack.peek();
stack.pop();
stack.push(a_letter);
}
else
{
stack.push(a_letter);
}
}
}
else
{
System.out.print(c);
linkval = linkval + c;
}
next++;
}
else
{
//Prints postfix notation; stores in linked list
while (!stack.isEmptyStack()) {
System.out.print(stack.peek());
linkval = linkval + stack.peek();
stack.pop();
if(stack.first == null)
{
linked.push(linkval);
linkval = "";
}
}
}
}
}
//Option #2
elseif(num == 2)
{
value = true;
System.out.println("Enter equation without the x = portion of it for postfix.(add ; at end) ");
word2 = keyboard.next();
if(!word2.contains(";"))
{
System.out.println("Error no ; variable at the end of equation. Try again");
System.exit(0);
}
allEqu.push(word2);
intnex = 0;
for(inti = 0; i <= nex; i++)
{
p = word2.charAt(i);
if(p != ';')
{
if(p != '^' && p != '*' && p != '/' && p != '+' && p != '-')
{
Stringa_letter = Character.toString(p);
s.push(a_letter);
}
else
{
Stringa_letter = Character.toString(p);
if(value == true)
{
System.out.print(s.peek());
linkval = linkval + s.peek();
s.pop();
s.push(a_letter);
System.out.print(a_letter);
linkval = linkval + a_letter;
s.pop();
System.out.print(s.peek());
linkval = linkval + s.peek();
value = false;
}
else
{
s.push(a_letter);
}
}
nex++;
}
else
{
//Prints infix notation; stores in linked list
while(!s.isEmptyStack())
{
System.out.print(p);
linkval = linkval + p;
s.pop();
}
if(s.first == null)
{
linked.push(linkval);
linkval = "";
}
}
}
}
//Option #3
elseif(num == 3)
{
//Pops linkedstack and prints all equations till linkedstack is null
while(!allEqu.isEmptyStack())
{
System.out.println(allEqu.peek());
allEqu.pop();
}
}
elseif(num == 4)
{
//While loop to pop stack and print all conversions
while(!linked.isEmptyStack())
{
System.out.println(linked.peek());
linked.pop();
}
}
//Error checking
else
{
System.out.println("Invalid number. Enter number between 1 and 4");
System.exit(0);
}
}
}
}