# rpn.awk^ # RPN CALC of awk # # by guel 09/22/95 BEGIN { # stack[t = 0] = 0 print "RPN CALC of awk, ready." } { for (i = 1; i<= NF; i++) if (isNum($i)) push($i) else if ($i == "+") { lastX = pop(); push(pop() + lastX) } else if ($i == "-") { lastX = pop(); push(pop() - lastX) } else if ($i == "*") { lastX = pop(); push(pop() * lastX) } else if ($i == "/") { lastX = pop(); push(pop() / lastX) } else if ($i == "^") { lastX = pop(); push(pop() ^ lastX) } else if ($i~ /^(+-|[lL])$/) { lastX = pop(); push( - lastX) } else if ($i~ /^(1\/x|[xX])$/) { lastX = pop(); push( 1 / lastX) } else if ($i~ /^(sqrt|[zZ])$/) { lastX = pop(); push(sqrt(lastX)) } else if ($i~ /^(last|[bB])$/) push(lastX) else if ($i~ /^[nN]\+.*/) REG[substr($i, 3)] += getX() # STO+ else if ($i~ /^[nN]-.*/) REG[substr($i, 3)] -= getX() # STO- else if ($i~ /^[nN]\*.*/) REG[substr($i, 3)] *= getX() # STO* else if ($i~ /^[nN]\/.*/) REG[substr($i, 3)] /= getX() # STO/ else if ($i~ /^[nN].*/) REG[substr($i, 2)] = getX() # STO else if ($i~ /^[mM].*/) push(REG[substr($i, 2)]) # RCL else if ($i~ /^del$/) for (i in REG) delete REG[i] else if ($i~ /^(down|[vV])$/) pop() # « else if ($i~ /^(clr|[aA])$/) t = 0 # Clear else if ($i~ /^(xy|[cC])$/) { # xÌy a = pop(); b = pop(); push(a); push(b) } if ($0 == "") { push(getX()) } # CR printf("%g\t(stack_depth = %d)\n\n[", stack[t - 1], t) for (i = 0; i < t; i++) printf("%g ", stack[i]); printf("]\n") for (j in REG) printf("r.%s=%g ", j, REG[j]); printf("LastX=%g\n", lastX) } function push(x) { stack[t++] = x } function pop() { if (t > 0) return stack[--t] else printf("error: poped empty stack\n") } function getX() { if (t > 0) return stack[t - 1] else printf("error: poped empty stack\n") } function isNum(x) { return match(x, /^([+-]?[0-9]+[.]?[0-9]*[eE]?[+-]?[0-9]*|[.][0-9]+)$/) } # rpn.awk$