#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Stack//结构体
{
int top;
double data[30];
};
double pop(struct Stack* S2)//弹出函数
{
--S2->top;
return S2->data[S2->top];
}
void push(struct Stack* A1, double z)//推进函数
{
A1->data[A1->top] = z;
++(A1->top);
}
int isNumber(char A)//判断是不是数字
{
int sub = A - '0';
if (sub >= 0 && sub <= 9)
{
return 1;
}
else
{
return 0;
}
}
void Backward(char* S, int n)//逆序
{
int i = 0;
int j = n - 1;
while (i != j)
{
S[i] = S[j];
i++;
j--;
}
}
int main()
{
struct Stack s1;
s1.top = 0;
char s2[30] = { 0 };
gets(s2);//先将前缀和表达式输入一个字符数组
int len = strlen(s2);//测量字符串长度
Backward(s2, len);//逆序
int i = 0;
int count = 0;
while (s2[i] != '\0')
{
if (s2[i] != NULL)//排除空格情况
{
char c = s2[i];
int back = isNumber(c);//判断是不是数字
if (back == 1)//数字入栈
{
double num = c - '0';
push(s1.data, num);
}
else//符号弹出运算、装入
{
if (s1.top < 2)
{
printf("ERROR");
return 0;
}
else
{
double a = pop(s1.data);
double b = pop(s1.data);
double re = 0;
if (c == '+')//运算
{
re = a + b;
}
else if (c == '-')
{
re = a - b;
}
else if (c == '*')
{
re = a * b;
}
else if (c == '/')
{
re = a / b;
}
push(s1.data, re);
}
}
}
}
printf("%.1lf", s1.data[s1.top]);
return 0;
}
调用的地方看下呢,像是传了没初始化的指针