I\'m currently stuck in this code. The program is suppose to take 4 integers and
ID: 3887984 • Letter: I
Question
I'm currently stuck in this code. The program is suppose to take 4 integers and use opertors to make a equation to = to 24. An example is (2 * 1) * (3 * 4) =24. I don't know how to further persue this code.
Code below::
//Exercise20_17.java
//
import java.util.Arrays;
public class Exercise20_17 {
static int i=0,j=0,k=0;
final char[] operators = {'+','-','*','/'};
static boolean isSolved = false;
public static void main(String[] args) {
int[] list = {1, 2, 3, 4};
permute(list, 0, list.length - 1);
}
// the permute method
public static void permute(int[] list, int i, int n) {
int j;
if (i == n) {
System.out.println(Arrays.toString(list));
// tryOperators here! (Should call separate method)
//tryOperators(a, b, c);
}
else {
for (j = i; j <= n; j++) {
swap(list, i, j);
permute(list, i + 1, n);
swap(list, i, j);
}
}
}
// the swap method
public static void swap(int[] list, int i, int j) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
public double tryOperation(double a , double b, char operators) {
switch (operators) {
case '+':
return a + b;
case '-':
return b - a;
case '*':
return a * b;
case '/':
return b / a;
default:
return 0;
}
}
public static String getOperators(char operators) {
switch (operators) {
case 0:
return "+";
case 1:
return "-";
case 2:
return "*";
case 3:
return "/";
default:
return "";
}
}
}
Explanation / Answer
Source Code:
import java.util.*;
public class CalculateExpressionValue
{
public static int EvaluateExpression(String expression)
{
char[] chartokens = expression.toCharArray();
// Stack for numbers: 'values'
Stack<Integer> stkvalues = new Stack<Integer>();
// Stack for Operators: 'ops'
Stack<Character> ops = new Stack<Character>();
for (int i = 0; i < chartokens.length; i++)
{
// Current token is a whitespace, skip it
if (chartokens[i] == ' ')
continue;
// Current token is a number, push it to stack for numbers
if (chartokens[i] >= '0' && chartokens[i] <= '9')
{
StringBuffer sbuf = new StringBuffer();
// There may be more than one digits in number
while (i < chartokens.length && chartokens[i] >= '0' && chartokens[i] <= '9')
sbuf.append(chartokens[i++]);
stkvalues.push(Integer.parseInt(sbuf.toString()));
}
// Current token is an opening brace, push it to 'ops'
else if (chartokens[i] == '(')
ops.push(chartokens[i]);
// Closing brace encountered, solve entire brace
else if (chartokens[i] == ')')
{
while (ops.peek() != '(')
stkvalues.push(applyOp(ops.pop(), stkvalues.pop(), stkvalues.pop()));
ops.pop();
}
// Current token is an operator.
else if (chartokens[i] == '+' || chartokens[i] == '-' ||
chartokens[i] == '*' || chartokens[i] == '/')
{
// While top of 'ops' has same or greater precedence to current
// token, which is an operator. Apply operator on top of 'ops'
// to top two elements in values stack
while (!ops.empty() && hasPrecedence(chartokens[i], ops.peek()))
stkvalues.push(applyOp(ops.pop(), stkvalues.pop(), stkvalues.pop()));
// Push current token to 'ops'.
ops.push(chartokens[i]);
}
}
// Entire expression has been parsed at this point, apply remaining
// ops to remaining values
while (!ops.empty())
stkvalues.push(applyOp(ops.pop(), stkvalues.pop(), stkvalues.pop()));
// Top of 'values' contains result, return it
return stkvalues.pop();
}
// Returns true if 'op2' has higher or same precedence as 'op1',
// otherwise returns false.
public static boolean hasPrecedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
return true;
}
// A utility method to apply an operator 'op' on operands 'a'
// and 'b'. Return the result.
public static int applyOp(char op, int b, int a)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new
UnsupportedOperationException("Cannot divide by zero");
return a / b;
}
return 0;
}
// Driver method to test above methods
public static void main(String[] args)
{
int a,b,c,d;
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Integer");
a=sc.nextInt();
System.out.println("Enter Second Integer");
b=sc.nextInt();
System.out.println("Enter Third Integer");
c=sc.nextInt();
System.out.println("Enter Fourth Integer");
d=sc.nextInt();
String val="2 * 1 * 3 * 4";
System.out.println("The Total SUM Is "+CalculateExpressionValue.EvaluateExpression(a+" * "+ b+" * "+ c +" *"+d));
}
}
Output:
Enter First Integer
2
Enter Second Integer
1
Enter Third Integer
3
Enter Fourth Integer
4
The Total SUM Is 24
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.