Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a program that reads prefix expression and return its value. the expressi

ID: 3653562 • Letter: I

Question

I need a program that reads prefix expression and return its value. the expression only contains +,* and single digits. For instance: + 5 * 6 7 returns 47, in other words it multiplies 6 * 7 first then adds 5. Here is my program but it is not working, please fix, thanks. See my program below:

import java.util.Scanner;
public class lab7
{
public static void main (String[] args)
{
char[] a;
a = new char[6];
a[0] = '0';
a[1] = '6';
a[2]= '*';
a[3]= '7';
a[4]='+';
a[5]= '5';
{
System.out.print (prefixcalc(a,0));
}

public class prefixcalc (char [] a, int i)
{
if ((int)a[i]>=48&&(int)a[i]<=57))
return a[i]-'0'; //or `(int)a[i]-48
if (int)a[i] == "+"))
return (5+prefixcalc(a[i],i+2));
else
return (6*prefixcalc(a[i],i+2))
else
return 7;

}


}
}
}
}

Explanation / Answer

double result() { if(scanner.hasNext()) token = scanner.next(); if(token.equals("+")) { double a = result(); double b = result(); return a+b; } else if(token.equals("-")) { double c = result(); double d = result(); return c-d; } else if(token.equals("*")) { double e = result(); double f = result(); return e*f; } else if(token.equals("/")) { double g = result(); double h = result(); return g/h; } else return Double.parseDouble(token); }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote