一个简单的完整c程序如下,已在win-tc下运行通过。
#include
#include
#define Max 100
int *p;
int *tos;
int *bos;
/*添加一个数据放到堆栈最顶端*/
void push(int i)
{
if(p > bos)
{
printf("The stack is full!\n");
return;
}
*p=i;
p++;
}
/*从堆栈顶端取出一个数据*/
int pop(void)
{
p--;
if(p < tos)
{
printf("The stack is bottom overflow!\n");
return 0;
}
return *p;
}
void main(void)
{
int a,b;
char s[80];
p=(int *)malloc(Max*sizeof(int));
if(!p)
{
printf("Allocation error!");
exit(1);
}
tos=p;
bos=p + Max -1;
printf("Please input number a:\n");
scanf("%d",&a);
push(a);
printf("Please input number b:\n");
scanf("%d",&b);
push(b);
printf("Please input arithmetic sign:\n");
scanf("%s",s);
switch (*s)
{
case '+':
b=pop();
a=pop();
printf("The result of a+b= %d\n",(a+b));
push(a+b);
break;
case '-':
b=pop();
a=pop();
printf("The result of a-b= %d\n",(a-b));
push(a-b);
break;
case '*':
b=pop();
a=pop();
printf("The result of a*b= %d\n",(a*b));
push(a*b);
break;
case '/':
b=pop();
a=pop();
printf("The result of a/b= %d\n",(a/b));
push(a/b);
break;
default:
printf("Please input the correct arithmetic sign.\n");
}
getch();
}