模拟堆栈 实现简单的加减乘除 差错望大家指出谢谢
#include<iostream>#include<string.h>
int result(int len,char * str)
{
int result=0;
char * fuhao=new char [len];
int * shuzi=new int [len];
memset (fuhao,0,sizeof(char )*len);
memset (shuzi,0,sizeof(int )*len);
int shuzitop=-1;
int fuhaotop=-1;
for(int i=0;i<len;i++)
{
if('0'<=str[i]<='9')
shuzi[++shuzitop]=str[i]-'0';
if(str[i]=='-'||str[i]=='+'||str[i]=='*'||str[i]=='/')
{
if(str[i]=='*')
{
int num1=shuzi[shuzitop];
int num2=str[++i];
num1*=num2;
shuzi[shuzitop]=num1;
}
if(str[i]=='/')
{
int num1=shuzi[shuzitop];
int num2=str[++i];
num1/=num2;
shuzi[shuzitop]=num1;
}
else
fuhao[++fuhaotop]=str[i];
}
}
while(fuhaotop!=-1)
{
int a=shuzi[shuzitop--];
int b=shuzi[shuzitop];//此处报错了
if(fuhao[fuhaotop--]=='+')
{
b+=a;
shuzi[shuzitop]=b;
}
if(fuhao[fuhaotop--]=='-')
{
b+=a;
shuzi[shuzitop]=b;
}
}
result=shuzi[shuzitop];
delete [] shuzi;
delete [] fuhao;
return result;
}
void main()
{
char *a="1+2";
printf("the result is :%d ",result(5,a));
}