-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
79 lines (75 loc) · 2.76 KB
/
Copy pathMain.java
File metadata and controls
79 lines (75 loc) · 2.76 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean run = true;
System.out.print("Please enter the stack size: ");
int stackSize = sc.nextInt();
Stack s = new Stack(stackSize);
while (run) {
System.out.println("\n=========== Choose an option ===========\n");
System.out.println("A) Re-initialize the stack");
System.out.println("B) Push to the stack");
System.out.println("C) Pop from the stack");
System.out.println("D) Peek at the top element");
System.out.println("E) Check an expression");
System.out.println("F) Reverse a string");
System.out.println("G) Check if stack is full");
System.out.println("H) Check if stack is empty");
System.out.println("I) Exit");
String input = sc.nextLine().trim();
input = input.toUpperCase();
switch(input) {
case "A":
System.out.print("Please enter the stack size: ");
int size = sc.nextInt();
sc.nextLine();
s = new Stack(size);
break;
case "B":
System.out.print("Please enter the element you want to push: ");
String element = sc.nextLine();
s.push(element);
break;
case "C":
if (s.is_empty()) {
System.out.println("The stack is empty");
break;
}
Object c = s.pop();
System.out.print(c + " has been popped");
break;
case "D":
if (s.is_empty()) {
System.out.println("The stack is empty");
break;
}
Object top = s.peek();
System.out.print(top + " is at the top of the stack");
break;
case "E":
System.out.print("Enter the expression to check: ");
String expression = sc.nextLine();
System.out.println("Expression "+ expression +" is "+(BalancedParenthesisChecker.isBalanced(expression)?"balanced":"NOT balanced"));
break;
case "F":
System.out.print("Enter the string to reverse: ");
String string = sc.nextLine();
System.out.println("String "+ string +" reversed is "+(StringInversion.inverseString(string)));
break;
case "G":
System.out.println("Stack is "+(s.is_full()?"full":"not full"));
break;
case "H":
System.out.println("Stack is "+(s.is_empty()?"empty":"not empty"));
break;
case "I":
run = false;
break;
default:
System.out.println("Unknown command");
break;
}
}
}
}