Arithmetic Expression evaluation using java
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class customcalculator
{
static int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
static String inftopos(String exp)
{
String result = new String("");
Stack<Character> stack = new Stack<>();
for (int i = 0; i<exp.length(); ++i)
{
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c))
result += c;
else if (c == '(')
stack.push(c);
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression";
else
stack.pop();
}
else
{
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek()))
result += stack.pop();
stack.push(c);
}
}
while (!stack.isEmpty())
result += stack.pop();
return result;
}
static void evaluate(String postfix)
{
int ans=0;
char[] pf_arr = postfix.toCharArray();
int len = postfix.length();
Stack<Integer> mystack = new Stack<>();
for(int i=0;i<len;i++)
{
if(Character.isLetterOrDigit(pf_arr[i]))
{
mystack.push(Character.getNumericValue(pf_arr[i]));
}
else if(pf_arr[i]=='+')
{
ans = do_operation('+',mystack.pop(),mystack.pop());
mystack.push(ans);
}
else if(pf_arr[i]=='-')
{
ans = do_operation('-',mystack.pop(),mystack.pop());
mystack.push(ans);
}
else if(pf_arr[i]=='*')
{
ans = do_operation('*',mystack.pop(),mystack.pop());
mystack.push(ans);
}
else if(pf_arr[i]=='/')
{
ans = do_operation('/',mystack.pop(),mystack.pop());
mystack.push(ans);
}
}
System.out.print(mystack.pop());
}
private static int do_operation(char c, Integer pop, Integer pop2)
{
int ans=0;
switch(c)
{
case '+':
ans = pop2+pop;
break;
case '-':
ans = pop2-pop;
break;
case '*':
ans = pop2*pop;
break;
case '/':
ans = pop2/pop;
break;
}
return ans;
}
public static void main(String args[])
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter an expression to evaluate");
String infixexper = scn.nextLine();
String postfix = inftopos(infixexper);
//System.out.println(postfix);
evaluate(postfix);
scn.close();
}
}
import java.util.Scanner;
import java.util.Stack;
public class customcalculator
{
static int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
static String inftopos(String exp)
{
String result = new String("");
Stack<Character> stack = new Stack<>();
for (int i = 0; i<exp.length(); ++i)
{
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c))
result += c;
else if (c == '(')
stack.push(c);
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression";
else
stack.pop();
}
else
{
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek()))
result += stack.pop();
stack.push(c);
}
}
while (!stack.isEmpty())
result += stack.pop();
return result;
}
static void evaluate(String postfix)
{
int ans=0;
char[] pf_arr = postfix.toCharArray();
int len = postfix.length();
Stack<Integer> mystack = new Stack<>();
for(int i=0;i<len;i++)
{
if(Character.isLetterOrDigit(pf_arr[i]))
{
mystack.push(Character.getNumericValue(pf_arr[i]));
}
else if(pf_arr[i]=='+')
{
ans = do_operation('+',mystack.pop(),mystack.pop());
mystack.push(ans);
}
else if(pf_arr[i]=='-')
{
ans = do_operation('-',mystack.pop(),mystack.pop());
mystack.push(ans);
}
else if(pf_arr[i]=='*')
{
ans = do_operation('*',mystack.pop(),mystack.pop());
mystack.push(ans);
}
else if(pf_arr[i]=='/')
{
ans = do_operation('/',mystack.pop(),mystack.pop());
mystack.push(ans);
}
}
System.out.print(mystack.pop());
}
private static int do_operation(char c, Integer pop, Integer pop2)
{
int ans=0;
switch(c)
{
case '+':
ans = pop2+pop;
break;
case '-':
ans = pop2-pop;
break;
case '*':
ans = pop2*pop;
break;
case '/':
ans = pop2/pop;
break;
}
return ans;
}
public static void main(String args[])
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter an expression to evaluate");
String infixexper = scn.nextLine();
String postfix = inftopos(infixexper);
//System.out.println(postfix);
evaluate(postfix);
scn.close();
}
}
OUTPUT
Leave a Comment