I'm trying to make a simple calculator with order of operations. I had read in the Internet and found the algorithm of RPN (Reverse Polish notation).
EDIT:
Lets take an example: 2 * 5 - 3 + 4
Ok, I did as you both said check it out now:
Proc is string array of both numbers and operations. Proc will be {2, *, 5, -, 3, +, 4}
This is the code:
int tempt = 0;
Stack <Double> stc = new Stack <Double>();
while (tempt < proc.length)
{
try
{
Double num = Double.parseDouble(proc[tempt]);
stc.push(num);
tempt++;
}
catch (Exception e)
{
char [] stcs = proc[tempt].toCharArray();
switch (stcs[0])
{
case '+':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 + a2);
tempt++;
break;
}
case '-':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 - a2);
tempt++;
break;
}
case 'x':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 * a2);
tempt++;
break;
}
case '÷':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 / a2);
tempt++;
break;
}
}
}
STILL DOESNT WORK
How can I make it work as well? HELP ME PLS!