home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume6
/
rpn
/
rpn.c
< prev
Wrap
C/C++ Source or Header
|
1989-03-07
|
4KB
|
176 lines
/* rpn - a reverse polish notation calculator */
/* By Nils McCarthy with special thanks to:
THE ZOO */
/* COPYRIGHT 1898 NILS MCCARTHY */
/* THIS CODE MAY BE DISTRIBUTED PROVIDED THIS HEADER IS KEPT WITH IT */
/* THIS CODE, EXCLUDING THIS HEADER MAY BE MODIFIED */
/* any questions about the copyright, mail to mtymp01@ux.acss.umn.edu */
#include <stdio.h>
int stacklength,*stack;
int top(void)
{
return stack[stacklength-1];
}
pr(options)
char *options;
{
if(*options==' ') options++;
if(*options==NULL) {
printf("%d\n",top());
return;
}
if((stacklength-1-atoi(options))<0) fprintf(stderr,"ERROR: INVALID %s",
"STACK ADDRESSING\n");
printf("%d\n",stack[stacklength-1-atoi(options)]);
}
push(thing)
int thing;
{
stacklength++;
stack[stacklength-1]=thing;
if(stacklength>=99) {
fprintf(stderr,"ERROR: STACK OVERFLOW\n");
/*exit(-1);*/
}
}
int pop(void)
{
int thing;
thing=stack[stacklength-1];
stacklength--;
if(stacklength<0) {
fprintf(stderr,"ERROR: STACK UNDERFLOW\n");
/*exit(-1);*/
}
return thing;
}
void add(void)
{
int thing;
thing=pop();
push(pop()+thing);
}
void sub(void)
{
int temp;
temp=pop();
push(pop()-temp);
}
void mul(void)
{
int temp;
temp=pop();
push(pop()*temp);
}
void div(void)
{
int temp;
temp=pop();
push(pop()/temp);
}
void mod(void)
{
int temp;
temp=pop();
push(pop()%temp);
}
void not(void)
{
push(~pop());
}
void xor(void)
{
int thing;
thing=pop();
push(pop()^thing);
}
void and(void)
{
int thing;
thing=pop();
push(pop()&thing);
}
void or(void)
{
int thing;
thing=pop();
push(pop()|thing);
}
void help(void)
{
printf("\n\nWELCOME TO RPN, a reverse polish notation calculator.\n");
printf("the functions are as follows:\n");
printf("add - add the top two numbers on the stack\n");
printf("sub - subtract the top two numbers on the stack\n");
printf("mul - multiply the top two numbers on the stack\n");
printf("div - divide the top two numbers on the stack\n");
printf("pop - discard the top element of the stack\n");
printf("mod - take the remainder of the division of the top two numbers on the stack\n");
printf("not - take the compliment of the top element on the stack\n");
printf("help - display this help\n");
printf("xor - exclusive or\n");
printf("or - not-exclusive or\n");
printf("and - bitwise and\n");
printf("pr [number] - print the number [number] deep in the stack. defaults to 0\n");
printf("sw - switch printing top of stack on/off\n");
printf("(not that you'll ever need this, but:)\n");
printf("end - terminate the progran\n\n");
}
main(argc,argv)
int argc;
char *argv[];
{
char showstack=(~0); /* closest i can get to boolean */
int stackspace[100];
char input[100];
stack=stackspace;
/* argument stuff */
if(argc>1) showstack=0;
/* end of argument stuff (short, eh?) */
printf("\nWELCOME TO RPN!!! (Rpn version 1) by Nils McCarthy\n\n");
while (scanf("%s",input)!=EOF) {
if(!strcmp(input,"add")) add();
else if(!strcmp(input,"sub")) sub();
else if(!strcmp(input,"mul")) mul();
else if(!strcmp(input,"div")) div();
else if(!strcmp(input,"pop")) pop();
else if(!strcmp(input,"?")) help();
else if(!strcmp(input,"h")) help();
else if(!strcmp(input,"sw")) showstack=~showstack;
else if(!strncmp(input,"pr",2)) pr(input+2);
else if(!strcmp(input,"help")) help();
else if(!strcmp(input,"xor")) xor();
else if(!strcmp(input,"or")) or();
else if(!strcmp(input,"and")) and();
else if(!strcmp(input,"x")) break;
else if(!strcmp(input,"q")) break;
else if(!strcmp(input,"exit")) break;
else if(!strcmp(input,"quit")) break;
else if(!strcmp(input,"end")) break;
else push(atoi(input));
if(showstack) {
printf("\t\tstack top: %d\n",top());
printf("\t\t\tstacksize: %d\n",stacklength);
}
}
printf("Bye Bye, now! Please try rpn again!\n\n");
}