the following,as copied from page 143 of 'awk a programming language' by(AKW) doesn not wish to cooperate.i know it is me but i do not know why.
{ for (i =1; i <= NF; i++)
if ($i ~/^[+-]?([0-9] + [.]?[0-9]*|[.][0-9]+)$/){
stack[++top] = $i
} else if ($i == "+" && top > 1 ) {
stack[top-1] += stack[top];top--
} else if ($i == "-" && top > 1 ) {
stack[top-1] -= stack[top];top--
} else if ($i == "*" && top > 1 ) {
stack[top-1] *= stack[top];top--
} else if ($i == "/" && top > 1 ) {
stack[top-1] /= stack[top];top--
} else if ($i == "^" && top > 1 ) {
stack[top-1] ^= stack[top];top--
} else {
printf("error: cannot evaluate %s\n", $i)
top = 0
next
}
if (top ==1)
printf("\t%.8g\n",stack[top--1])
else if ( top > 1) {
printf("error: too many operands\n")
top = 0
}
}
by the way no syntactical error occurs.all i get is 'cannot evaluate 1'
input:as shown in book is 1 2 + 3 4 - * 5 /
spaces are required.any help.thank you.
P.S.i thought all { had to be paired with a }.
i count 10 { and only 8 }.am i missing something?
{ for (i =1; i <= NF; i++)
if ($i ~/^[+-]?([0-9] + [.]?[0-9]*|[.][0-9]+)$/){
stack[++top] = $i
} else if ($i == "+" && top > 1 ) {
stack[top-1] += stack[top];top--
} else if ($i == "-" && top > 1 ) {
stack[top-1] -= stack[top];top--
} else if ($i == "*" && top > 1 ) {
stack[top-1] *= stack[top];top--
} else if ($i == "/" && top > 1 ) {
stack[top-1] /= stack[top];top--
} else if ($i == "^" && top > 1 ) {
stack[top-1] ^= stack[top];top--
} else {
printf("error: cannot evaluate %s\n", $i)
top = 0
next
}
if (top ==1)
printf("\t%.8g\n",stack[top--1])
else if ( top > 1) {
printf("error: too many operands\n")
top = 0
}
}
by the way no syntactical error occurs.all i get is 'cannot evaluate 1'
input:as shown in book is 1 2 + 3 4 - * 5 /
spaces are required.any help.thank you.
P.S.i thought all { had to be paired with a }.
i count 10 { and only 8 }.am i missing something?