v06i061: rpn - A reverse polish notation calculator
Brandon S. Allbery - comp.sources.misc
allbery at uunet.UU.NET
Wed Mar 8 11:32:42 AEST 1989
Posting-number: Volume 6, Issue 61
Submitted-by: mtymp01 at ux.acss.umn.edu
Archive-name: rpn
[Notes: (1) copyrighted (and rejected by r$); (2) uses ANSI C (pcc users
beware!). ++bsa]
[I know dc exists, I like this better, it has optional display of
top of stack]
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: Makefile README rpn.1 rpn.c
# Wrapped by mtymp01 at ux.acss.umn.edu on Fri Feb 24 16:47:38 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(159 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
XDESTMAN=/usr/man/man1
XDESTDIR=/usr/bin
X
Xrpn: rpn.o
X cc -o rpn rpn.o
X
Xrpn.o: rpn.c
X cc -c rpn.c
X
Xinstall: rpn
X mv rpn ${DESTDIR}
X rm rpn.o
X mv rpn.1 ${DESTMAN}
END_OF_FILE
if test 159 -ne `wc -c <'Makefile'`; then
echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'README' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(195 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XJust edit the makefile and do 'make install' and have everything
Xhappen. any problems, please mail to mtymp01 at ux.acss.umn.edu, or
X...!rutgers!umn-cs!ux.acss!mtymp01, or mtymp01 at UMNACUX.BITNET.
X
X
END_OF_FILE
if test 195 -ne `wc -c <'README'`; then
echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'rpn.1' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'rpn.1'\"
else
echo shar: Extracting \"'rpn.1'\" \(357 characters\)
sed "s/^X//" >'rpn.1' <<'END_OF_FILE'
X.TH RPN LOCAL "", ""
X.AT 3
X.SH NAME
X.PP
Xrpn - a reverse polish notation calculator
X.SH SYNOPSIS
Xrpn [-]
X.PP
X.SH DESCRIPTION
X.PP
X.I Rpn
Xtakes the standard input and is a RPN calculator. If you want verbose
Xoff, use 'rpn\ -'. For a more detailed description, type in 'help' from
Xwithin rpn.
X.SH BUGS
X.PP
X.SH MAN AUTHOR
XNils McCarthy (mtymp01 at ux.acss.umn.edu)
END_OF_FILE
if test 357 -ne `wc -c <'rpn.1'`; then
echo shar: \"'rpn.1'\" unpacked with wrong size!
fi
# end of 'rpn.1'
fi
if test -f 'rpn.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'rpn.c'\"
else
echo shar: Extracting \"'rpn.c'\" \(3850 characters\)
sed "s/^X//" >'rpn.c' <<'END_OF_FILE'
X/* rpn - a reverse polish notation calculator */
X/* By Nils McCarthy with special thanks to:
X THE ZOO */
X/* COPYRIGHT 1898 NILS MCCARTHY */
X/* THIS CODE MAY BE DISTRIBUTED PROVIDED THIS HEADER IS KEPT WITH IT */
X/* THIS CODE, EXCLUDING THIS HEADER MAY BE MODIFIED */
X/* any questions about the copyright, mail to mtymp01 at ux.acss.umn.edu */
X
X
X#include <stdio.h>
X
Xint stacklength,*stack;
X
Xint top(void)
X{
X return stack[stacklength-1];
X}
X
Xpr(options)
Xchar *options;
X{
X if(*options==' ') options++;
X if(*options==NULL) {
X printf("%d\n",top());
X return;
X }
X if((stacklength-1-atoi(options))<0) fprintf(stderr,"ERROR: INVALID %s",
X "STACK ADDRESSING\n");
X printf("%d\n",stack[stacklength-1-atoi(options)]);
X}
X
Xpush(thing)
Xint thing;
X{
X stacklength++;
X stack[stacklength-1]=thing;
X if(stacklength>=99) {
X fprintf(stderr,"ERROR: STACK OVERFLOW\n");
X /*exit(-1);*/
X }
X}
X
Xint pop(void)
X{
X int thing;
X thing=stack[stacklength-1];
X stacklength--;
X if(stacklength<0) {
X fprintf(stderr,"ERROR: STACK UNDERFLOW\n");
X /*exit(-1);*/
X }
X return thing;
X}
X
Xvoid add(void)
X{
X int thing;
X thing=pop();
X push(pop()+thing);
X}
X
Xvoid sub(void)
X{
X int temp;
X temp=pop();
X push(pop()-temp);
X}
X
Xvoid mul(void)
X{
X int temp;
X temp=pop();
X push(pop()*temp);
X}
X
Xvoid div(void)
X{
X int temp;
X temp=pop();
X push(pop()/temp);
X}
X
Xvoid mod(void)
X{
X int temp;
X temp=pop();
X push(pop()%temp);
X}
X
Xvoid not(void)
X{
X push(~pop());
X}
X
Xvoid xor(void)
X{
X int thing;
X thing=pop();
X push(pop()^thing);
X}
X
Xvoid and(void)
X{
X int thing;
X thing=pop();
X push(pop()&thing);
X}
X
Xvoid or(void)
X{
X int thing;
X thing=pop();
X push(pop()|thing);
X}
X
Xvoid help(void)
X{
X printf("\n\nWELCOME TO RPN, a reverse polish notation calculator.\n");
X printf("the functions are as follows:\n");
X printf("add - add the top two numbers on the stack\n");
X printf("sub - subtract the top two numbers on the stack\n");
X printf("mul - multiply the top two numbers on the stack\n");
X printf("div - divide the top two numbers on the stack\n");
X printf("pop - discard the top element of the stack\n");
X printf("mod - take the remainder of the division of the top two numbers on the stack\n");
X printf("not - take the compliment of the top element on the stack\n");
X printf("help - display this help\n");
X printf("xor - exclusive or\n");
X printf("or - not-exclusive or\n");
X printf("and - bitwise and\n");
X printf("pr [number] - print the number [number] deep in the stack. defaults to 0\n");
X printf("sw - switch printing top of stack on/off\n");
X printf("(not that you'll ever need this, but:)\n");
X printf("end - terminate the progran\n\n");
X}
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X char showstack=(~0); /* closest i can get to boolean */
X int stackspace[100];
X char input[100];
X stack=stackspace;
X/* argument stuff */
X if(argc>1) showstack=0;
X/* end of argument stuff (short, eh?) */
X printf("\nWELCOME TO RPN!!! (Rpn version 1) by Nils McCarthy\n\n");
X while (scanf("%s",input)!=EOF) {
X if(!strcmp(input,"add")) add();
X else if(!strcmp(input,"sub")) sub();
X else if(!strcmp(input,"mul")) mul();
X else if(!strcmp(input,"div")) div();
X else if(!strcmp(input,"pop")) pop();
X else if(!strcmp(input,"?")) help();
X else if(!strcmp(input,"h")) help();
X else if(!strcmp(input,"sw")) showstack=~showstack;
X else if(!strncmp(input,"pr",2)) pr(input+2);
X else if(!strcmp(input,"help")) help();
X else if(!strcmp(input,"xor")) xor();
X else if(!strcmp(input,"or")) or();
X else if(!strcmp(input,"and")) and();
X else if(!strcmp(input,"x")) break;
X else if(!strcmp(input,"q")) break;
X else if(!strcmp(input,"exit")) break;
X else if(!strcmp(input,"quit")) break;
X else if(!strcmp(input,"end")) break;
X else push(atoi(input));
X if(showstack) {
X printf("\t\tstack top: %d\n",top());
X printf("\t\t\tstacksize: %d\n",stacklength);
X }
X }
X printf("Bye Bye, now! Please try rpn again!\n\n");
X}
END_OF_FILE
if test 3850 -ne `wc -c <'rpn.c'`; then
echo shar: \"'rpn.c'\" unpacked with wrong size!
fi
# end of 'rpn.c'
fi
echo shar: End of shell archive.
exit 0
---Nils McCarthy---mtymp01 at ux.acss.umn.edu---...!rutgers!umn-cs!ux.acss!mtymp01
More information about the Comp.sources.misc
mailing list