调了很长时间都没调好,看来我不是这个料~急需,程序如下:
#include "stdlib.h"
#define MAX 100
int *stack;
int *t;
void push();
main()
{
int x,y,m,i;
char s[50];
stack=(int *)malloc(MAX*sizeof(int));
if (!stack)
{
printf("\n can't create stack!\n");
exit(1);
}
t=stack;
printf("\t 《数据结构课程设计--利用栈进行多种运算》\n\n \t(可用的符号有+、-、*、/、?、%、^,输入q退出。)\n\n");
do{
printf("请输入数值或符号:");
printf(s);
switch(*s)
{
case'+':x=pop();
y=pop();
printf("相加结果为:%d+%d=%d\n",x,y,x+y);
push(x+y);
break;
case'-':x=pop();
y=pop();
printf("相减结果为:%d-%d=%d\n",x,y,x-y);
push(x-y);
break;
case'*':x=pop();
y=pop();
printf("相乘结果为:%d*%d=%d\n",x,y,x*y);
push(x*y);
break;
case'/':x=pop();
y=pop();
if(y==0)
{
printf("分母不能为零!\n");
break;}
printf("相除结果为:%d/%d=%.4f\n",x,y,(float)x/y);
push(x/y);
break;
case'?':x=pop();
y=pop();
printf("栈是 %d\n",x);
break;
case'^':x=pop();
y=pop();
m=1;i=y;
while (i>0)
{m=m*x;
i=i-1;
}
printf("乘方结果为:%d^(%d)=%d\n",x,y,m);
push(x^y);
break;
case'%':x=pop();
y=pop();
if(y==0)
{printf("分母不能为零!\n");
break;}
printf("求余结果为:%dmod(%d)=%d\n",x,y,x%y);
push(x%y);
break;
default:push(atoi(s));
}
}while(*s!='q');
}
void push(int n)
{
if(stack>stack+MAX-1)
{
printf("栈满!\n");
return;
}
*stack=n;
stack++;
}
pop()
{
stack--;
if(stack<t)
{printf("栈空!\n");
return(0);
}
return(*stack);
}
两个错误如下:
c:\documents and settings\administrator\桌面\修改过的程序\栈(未改完).c(13) : warning C4013: 'printf' undefined; assuming extern returning int
c:\documents and settings\administrator\桌面\修改过的程序\栈(未改完).c(23) : warning C4013: 'pop' undefined; assuming extern returning int