运行结果不正确!查不出问题出在那!!求解答
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR -1
typedef float status;
typedef struct
{
char *base, *top;
int stacksize;
}optrstack;
typedef struct
{
float *base, *top;
int stacksize;
}opndstack;
int optrinitstack(optrstack *s)
{
s->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if (!s->base)
exit(0);
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
return 1;
}
int opndinitstack(opndstack *s)
{
s->base = (float *)malloc(STACK_INIT_SIZE * sizeof(float));
if (!s->base)
exit(0);
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
return 1;
}
int opndpush(opndstack *s, float e)
{
if (s->top - s->base >= s->stacksize)
{
s->base = (float *)realloc(s->base, (s->stacksize + STACKINCREMENT)*sizeof(float));
if (!s->base)exit(0);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*(s->top++) = e;
return 1;
}
int optrpush(optrstack *s, char e)
{
if (s->top - s->base >= s->stacksize)
{
s->base = (char *)realloc(s->base, (s->stacksize + STACKINCREMENT)*sizeof(char));
if (!s->base)exit(0);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*(s->top++) = e;
return 1;
}
int opndpop(opndstack *s, float *e)
{
if (s->top == s->base)return 0;
*e = *(--s->top);//加一个*号
return 1;
}
int optrpop(optrstack *s, char *e)
{
if (s->top == s->base) return ERROR;//ERROR未定义,我帮你宏定义了一个-1
*e = *--s->top;//加一个*号
return 1;
}
char optrgettop(optrstack s)
{
char e;
if (s.top == s.base)return 0;
e = *(s.top - 1);
return e;
}
float opndgettop(opndstack s)
{
float e;
if (*s.top = *s.base)return 0;
e = *(s.top - 1);
return e;
}
/*char Precede(char a, char b) //判断运算符优先级
{
int i, j;
char Table[8][8] = {
{ ' ', '+', '-', '*', '/', '(', ')', '#'},
{ '+', '>', '>', '<', '<', '<', '>', '<'},
{ '-', '>', '>', '<', '<', '<', '>', '<'},
{ '*', '>', '>', '>', '>', '<', '>', '<'},
{ '/', '>', '>', '>', '>', '<', '>', '<'},
{ '(', '<', '<', '<', '<', '<', '=', '<'},
{ ')', '>', '>', '>', '>', '=', '>', '<'},
{ '#', '>', '>', '>', '>', '>', '>', '>'}
}; //优先级表格
for (i = 0; i < 7; i++)
if (Table[0][i] == a) //纵坐标寻找
break;
for (j = 0; j < 7; j++) //横坐标寻找
if (Table[j][0] == b)
break;
return Table[j][i];
}*/
char Precede(char c1,char c2) {
if(c1 = '(') {//compare (
if(c2 == ')')return '=';
else return '<';
}
else if(c1 == '#') {//compare #
if(c2 != c1) {
return '<';
}
else return '=';
}
else if(c1 == '*' || c1 == '/' ) {//compare */
if(c2 == '(') {
return '<';
}
else return '>';
}
else if(c1 == '+'||c1 == '-') {//compare + -
if(c2 == '('|| c2 == '*' || c2 =='/') {
return '<';
}
else
return '>';
}
return ERROR;
}
float opreate(float a, char theta, float b) //计算二元表达式的值
{
float c;
if (theta == '+')
c = a + b;
else if (theta == '-')
c = a - b;
else if (theta == '*')
c = a*b;
else
c = a / b;
return c;
}
float In(char ch) //判断字符ch是否为运算符
{
char ptr[10] = { '+', '-', '*', '/', '(', ')', '=' };
int i;
for (i = 0; i < 7; i++)
{
if (ch == ptr[i])
return 1;
}
return 0;
}
float T()//取值符啊,因为你用的是指针啊!
{
char x, theta, c;
float a, b;
optrstack optr;
opndstack opnd;
optrinitstack(&optr);//取值符
optrpush(&optr, '#');//# -> '#' 和取值符
opndinitstack(&opnd);//取值符
c = getchar();
while (c != '#' || optrgettop(optr) != '#')
{
if (!In(c)){ opndpush(&opnd, c); c = getchar(); }//取值符
else
switch (Precede(optrgettop(optr), c))//大写P
{
case'<':optrpush(&optr, c);//取值符
c = getchar();
break;
case'=':
optrpop(&optr, &x);//取值符
c = getchar();
break;
case'>':
optrpop(&optr, &theta);//取值符
opndpop(&opnd, &b);//取值符
opndpop(&opnd, &a);//取值符
opndpush(&opnd, opreate(a, theta, b));//取值符
break;
}//switch
}//while
return opndgettop(opnd);
}
void main()
{
float a;
a = T();
printf("%f\n", a);
}