You are on page 1of 27

Evaluate Postfix Expression

Algorithm
1. Scan expression from left to right and repeat
steps 2 and 4 for each element encountered.
2. If it is an operand, push it to the stack.
3. If it is an operator(x), pop the top of the stack
assign it to variable a. Pop the next top most
element from the stack and assign it to variable
b. Perform operation b x a. Push the result to
the stack.
4. Pop the value from the stack. This is the output.
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 0

item =

operand =

a =

b =

c = 0.00
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 0

item = 2

operand =

a =

b =

c = 0.00
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 0

item = 2

operand = 2

a =

b =

c = 0.00
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 1

item = 1

operand = 21

a =

b =

c = 0.00
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 2

item =

operand = 21

a =

b =

c = 0.00
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 3

item =

operand =

a =

b =

c = 0.00
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 5

item =

operand =

a =

b =

c = 0.00
2
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 7

item = *

operand =

a =

b =

c = 0.00
4
2
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 7

item = *

operand =

a = 4

b =

c = 0.00
2
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 7

item = *

operand =

a = 4

b = 2

c = 0.00
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 7

item = *

operand =

a = 4

b = 2

c = 8.00
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 9

item = *

operand =

a = 4

b = 2

c = 8.00
8.00
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 9

item = -

operand =

a = 4

b = 2

c = 8.00
8.00
21
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 11

item = -

operand =

a = 8.00

b = 21

c = 13
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 11

item = ~

operand =

a = 8.00

b = 21

c = 13
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 12

item = 1

operand = -

a = 8.00

b = 21

c = 13
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 14

item = 1

operand =

a = 8.00

b = 21

c = 13
-1
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 17

item = 1

operand =

a = 8.00

b = 21

c = 13
41
-1
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 19

item = 3

operand =

a = 8.00

b = 21

c = 13
3
41
-1
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 21

item = *

operand =

a = 3

b = 41

c = 123
123
-1
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 23

item = 8

operand = 8

a = 3

b = 41

c = 123
8
123
-1
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 25

item = +

operand = 8

a = 8

b = 123

c = 131
131
-1
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 25

item = *

operand = 8

a = 131

b = -1

c = -131
-131
13
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 27

item = /

operand = 8

a = -131

b = 13

c = -0.0992366
-0.0992366
ms
public static String evalPostfix(String postfix) {
int i=0;
char item;
postfix = postfix + ";";
String operand = "";
String a = "";
String b = "";
double c;
if (postfix!="ERROR") {
try {
while (postfix.charAt(i)!=';') {
item = postfix.charAt(i);
if (!isOperator(item)) {
do {
if (item!='~') {
operand = operand + item;
} else {
operand = operand + "-";
}
i++;
item = postfix.charAt(i);
} while (postfix.charAt(i)!=' ');
ms.push(operand);
operand = "";
i++;
} else {
a = ms.pop();
b = ms.pop();
c = compute(a, b, item);
ms.push(DtoS(c));
i++;
if(postfix.charAt(i)==' ') i++;
}
}
a = ms.pop();
} catch (StringIndexOutOfBoundsException e) {
a = "ERROR";
} catch (NumberFormatException n) {
a = "ERROR";
}
} else {
a = "ERROR";
}
return a;
}
PF = 21 2 4 * - ~1 41 3 * 8 + * /;
i = 27

item = /

operand = 8

a = -131

b = 13

c = -0.0992366
-0.0992366
ms
A = -0.0992366

You might also like