CS Student here. I want to be able to take a string such as '2+2*3/2-2' and evaluate it (= 3). But I'm not sure how to structure the code to follow the proper order of operations. Here's code for multiplication and division:
int r = 1;
int n = 0;
char op = '*';
for (int i = 0; i < E.length(); i++)
if (E.charAt(i)=='*'||E.charAt(i)=='/')
{
if (op == '*')
r *= n;
else
r /= n;
n = 0;
op = E.charAt(i);
}
else
n = n*10 + (E.charAt(i)-'0');
if (op == '*')
r *= n;
else
r /= n;
return r;
Thanks for reading!