#include
#define MAXSIZE 30
using namespace std;
char str[MAXSIZE];
typedef struct{
float *base;
float *top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=new float[MAXSIZE];
S.top=S.base;
S.stacksize=MAXSIZE;
}
void Push(SqStack &S,int e)
{
*S.top++=e;
}
float Pop(SqStack &S)
{
float e=0;
e=*--S.top;
return e;
}
int main()
{
SqStack S1;
InitStack(S1);
char temp;
int k = 0;
while ((temp = getchar()) != '\n')
{
str[k++] = temp;
}
str[k] = '\0';
int cnt = 0;
for (int i = k - 1; i >= 0; --i)
{
if (str[i] == ' ')
continue;
if (str[i] >= '0' && str[i] <= '9')
{
double num = str[i] - '0', mul = 10;
i--;
for (; i >= 0; --i)
{
if (str[i] >= '0' && str[i] <= '9')
{
num += (str[i] - '0') * mul;
mul *= 10;
}
else if (str[i] == '.')
{
num /= mul;
mul = 1;
}
else if (str[i] == '-')
{
num = -num;
}
else
break;
}
Push(S1,num);
}
else
{
float l = Pop(S1);
float r = Pop(S1);
float res;
if (str[i] == '+')
res = l + r;
if (str[i] == '-')
res = l - r;
if (str[i] == '*')
res = l * r;
if (str[i] == '/')
{
if (r == 0)
{
printf("ERROR\n");
return 0;
}
res = l * 1.0 / r;
}
Push(S1,res);
}
}
float solution=Pop(S1);
printf("%.1lf\n", solution);
return 0;
}
如果用#include头文件,然后S1.push(num);S1.pop()就不会出错
这句话具体说的是什么意思?
不知道你这个问题是否已经解决, 如果还没有解决的话: