招聘的问题
64288 2003-04-16 09:50:54 请解决一下,计算器的编程,错在哪?
#include<stdio.h>
typedef struct{
int *top;
int *base;
}sqtack1;
typedef struct{
char *top;
char *base;
}sqtack2;
initstack1(sqtack1 wet )
{wet.top=wet.base=(int*)malloc(100*sizeof(int));
}
initstack2(sqtack2 wet)
{wet.top=wet.base=(char*)malloc(100*sizeof(char));
}
push1(sqtack1 wet ,int n )
{*++wet.top=n;
}
push2(sqtack2 wet,char n )
{*++wet.top=n;
}
int gettop1(sqtack1 wet)
{int n;
n=*wet.top;
return(n);
}
char gettop2(sqtack2 wet)
{ char n ;
n=*wet.top;
return(n);
}
int pop1(sqtack1 wet)
{int n;
n=*wet.top--;
return(n);
}
char pop2(sqtack2 wet)
{char n;
n=*wet.top--;
return(n);
}
int in(char *c,int *b )
{int a,s,i;
sqtack1 wet;
initstack1(wet);
if(*c>47&&*c<58)
{
for(i=0;*c>47&&*c<58;i++)
{a=*c-48;
push1(wet,a);
*c=getchar();
}
for(;i>0;i--,s=s*10)
{s=1;
*b=0;
*b=pop1(wet)*s+*b;
}
return(0);
}
else return (1);
}
char precede(char a,char b)
{if(a=='=')
return('<');
else if(b=='=')
return('>');
else if(a=='(')
{if(b==')')
return('=');
return('<');
}
else if((b=='*'||b=='/')&&(a=='+'||a=='-'))
return('<');
else return('>');
}
int operate(int a,char theta,int b)
{int c;
switch(theta)
{case'+':
c=a+b;break;
case'-':
c=a-b;break;
case'*':
c=a*b;break;
case'/':
c=a/b;break;
}
return(c);
}
evaluateexpression()
{char theta,c;int one,other;
char *p=&c;
int result,b;
int *q=&b;
sqtack1 opnd;
sqtack2 optr;
initstack2(optr);push2(optr,'=');
initstack1(opnd); c=getchar();
while(c!='='||gettop2(optr)!='=')判断是否结束
{if(!in(p,q))判断是否是数字,还是字母
push1(opnd,b);
else switch (precede(gettop2(optr),c))
{case'<':
push2(optr,c);c=getchar();break;
case'=':
pop2(optr);c=getchar();break;
case'>':
theta=pop2(optr);
other=pop1(opnd);one=pop1(opnd);
push1(opnd,operate(one,theta,other));
break;
}
}
result=gettop1(opnd);
printf("%d\n",result);
}
main()
{ evaluateexpression();
}