Skip to content

adding program for conversion #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions conversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
class myStack {
static final int MAX = 10;
int top;
char[] a = new char[MAX];

myStack() {
top = -1;
}

boolean push(char x) {
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
} else {
a[++top] = x;
return true;
}
}

int pop() {
if (top < 0) {
System.out.println("Stack Underflow");
return Integer.MIN_VALUE;
} else {
int x = a[top--];
return x;
}
}

char peek() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
} else {
char x = a[top];
return x;
}
}

int size() {
return (top + 1);
}

boolean isEmpty() {
return top == -1;
}

boolean isFull() {
return top == MAX - 1;
}
}

class conversion {
static boolean checkIfOperand(char ch) {
return Character.isLetterOrDigit(ch);
}

static int precedence(char ch) {
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}

static void covertInfixToPostfix(String expr) {
int i;
myStack s = new myStack();
StringBuilder result = new StringBuilder(new String(""));
for (i = 0; i < expr.length(); ++i) {
if (checkIfOperand(expr.charAt(i)))
result.append(expr.charAt(i));
else if (expr.charAt(i) == '(' || expr.charAt(i) == '[' || expr.charAt(i) == '{')
s.push(expr.charAt(i));
else if (expr.charAt(i) == ')' || expr.charAt(i) == ']' || expr.charAt(i) == '}') {
if (expr.charAt(i) == ')') {
while (!s.isEmpty() && s.peek() != '(') {
result.append(s.peek());
s.pop();
}
s.pop();
}
if (expr.charAt(i) == ']') {
while (!s.isEmpty() && s.peek() != '[') {
result.append(s.peek());
s.pop();
}
s.pop();
}
if (expr.charAt(i) == '}') {
while (!s.isEmpty() && s.peek() != '{') {
result.append(s.peek());
s.pop();
}
s.pop();
}
} else {
while (!s.isEmpty() && precedence(expr.charAt(i)) <= precedence(s.peek())) {
result.append(s.peek());
s.pop();
}
s.push(expr.charAt(i));
}
}
while (!s.isEmpty()) {
result.append(s.peek());
s.pop();
}
System.out.println(result);
}

public static void main(String[] args) {
String expression = "A+B*(C^D-E)";
covertInfixToPostfix(expression);
}
}