Похоже, что следующий код в основном работает так, как я думаю, вы ожидаете:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int *p;
int *tos;
int *bos;
void push(int i);
int pop(void);
int main ()
{
int a,b;
char s[80];
p = (int *) calloc(MAX,sizeof(int)); /* get stack memory */
if (!p)
{
printf("Allocation Failure\n");
exit(1);
}
tos = p;
bos = p + MAX-1;
printf("\nRPN Calculator\n");
printf("Enter 'i' for integer mode\n");
printf("Enter 'f' for floating point mode\n");
printf("Enter 'q' to quit\n");
char *endptr;
p++;
do {
printf("> ");
scanf("%s", s);
int val = strtol(s, &endptr, 10);
if (*endptr == '\0')
{
//printf("Got only the integer: %d\n", val);
}
else{ printf("operator: %s\n", endptr);
printf("Got the integer: %d\n", val);
}
/* tests */
if( endptr != s )
{
push(val);
}
switch(*endptr) {
case 'i':
printf("(Integer Mode)\n");
break;
case 'f':
printf("(Floating Point Mode)\n");
break;
case '+':
a = pop();
b = pop();
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n", a+b);
push(a+b);
break;
case '-':
a = pop();
b = pop();
printf("%d\n", b-a);
push(b-a);
break;
case '*':
a = pop();
b = pop();
printf("%d\n", a*b);
push(a*b);
break;
case '/':
a = pop();
b = pop();
if(a == 0){
printf("Cannot divide by zero\n");
break;
}
printf("%d\n", b/a);
push(b/a);
break;
case '.':
a = pop();
push(a);
printf("Current value on top of stack: %d\n", a);
break;
}
} while (*s != 'q');
//end do while loop
return 0;
}
// Put an element on the stack
void push (int i)
{
if (p > bos){
printf("Stack Full\n");
return;
}
*p = i;
printf("pushed %d\n", *p);
p++;
}
// Get the element from the top of the stack
int pop (void)
{
p--;
if(p < tos) {
printf("Stack Underflow\n");
return 0;
}
printf("popped %d\n", *p);
return *p;
}