用队和栈把中缀表达式化为后缀表达式后求表达式的值,我这里不知怎么,求不了!
//表达式求值#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define MAXQSIZE 100
#define ERROR 0
#define OK 1
#define OVERFLOW -1
typedef char DataType;
//队列的相关操作
typedef char DataType;
typedef struct
{
char *base;
int front;
int rear;
} SqQueue; //定义队列类型
int InitQueue(SqQueue &Q)
{
Q.base = (char *)malloc(MAXQSIZE * sizeof (char));
if (!Q.base)
{
exit(OVERFLOW);
}
Q.front = Q.rear = 0;
return OK;
}
int QueueEmpty(SqQueue &Q) //判断空队列//
{
return Q.rear == Q.front;
}
int EnQueue(SqQueue &Q, char e)
{
if ((Q.rear + 1) % MAXQSIZE == Q.front)
{
return ERROR;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
char DeQueue(SqQueue &Q)
{
char e;
if (Q.front == Q.rear)
{
return ERROR;
}
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
//return OK;
return e;
}
typedef struct
{
char *base; //在栈构造之前和销毁之后,base的值为NULL
char *top; //栈顶指针
int stacksize; //当前分配的存储空间,以元素为单位
} SqStack;
//
typedef struct
{
int *base; //在栈构造之前和销毁之后,base的值为NULL
int *top; //栈顶指针
int stacksize; //当前分配的存储空间,以元素为单位
} SeqStack;
int InitStack(SqStack &S)
{
S.base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if (!S.base)
{
exit(OVERFLOW);
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
} //InitStack
//
int InitSeStack(SeqStack &S)
{
S.base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if (!S.base)
{
exit(OVERFLOW);
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
} //InitStack
int Push(SqStack &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(OVERFLOW);
}
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
} //Push
char Pop(SqStack &S) //删除栈顶元素,用e返回其值,并返回OK;否则返回ERROR;
{
char e;
if (S.top == S.base)
{
return ERROR;
}
e = * --S.top;
return e; //
} //Pop
char GetTop(SqStack S, char &e) //返回类型不对就会出错!
{
if (S.top == S.base)
{
return ERROR;
}
e = *(S.top - 1);
return e; //返回类型不对就会出错!
}
int Priority(DataType op)
{
switch (op)
{
case '(' :
case '#' : return (0); //这里的意思!
case '-' :
case '+' : return (1);
case '*' : //
case '/' :return (2);
}
return 0;
}
void CTPostExp(SqQueue &Q)
{
SqStack OS; //运算符栈
char c;
char t;
char e; //
SqStack S;
S = OS;
InitStack(S); //初始化栈
Push(S,'#'); //压入栈底元素'#'
printf("输入以#结束的中缀表达式:"); //
do //扫描中缀表达式
{
c = getchar();
switch(c)
{
case ' ' : break; //去除空格
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
EnQueue(Q, c); break; //这里的意思 !
case '(' : Push(S, c); break;
case ')' :
case '#' :
do
{
t = Pop(S); //
if (t != '(' && t != '#')
{
EnQueue(Q, t);
}
} while (t != '(' && S.top != S.base); break;
case '+' :
case '-' :
case '*' :
case '/' :
while (Priority(c) <= Priority(GetTop(S, e)))
{
t = Pop(S); //
EnQueue(Q, t);
}
Push(S, c);
break;
} //switch
}
while (c != '#'); //以’#‘号结束表达式扫描
}
//
int GetTop(SeqStack &S) //返回类型不对就会出错!
{
int e;
if (S.top == S.base)
{
return ERROR;
}
e = *(S.top - 1);
return e; //返回类型不对就会出错!
}
//
int Push(SeqStack &S, int e)
{
if (S.top - S.base >= S.stacksize)
{
S.base = (int *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(int));
if(!S.base)
{
exit(OVERFLOW);
}
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
} //Push
//
int Pop(SeqStack &S) //删除栈顶元素,用e返回其值,并返回OK;否则返回ERROR;
{
int e;
if (S.top == S.base)
{
return ERROR;
}
e = * --S.top;
return e; //
} //Pop */
int CPostExp(SqQueue &Q)
{
SeqStack VS,S1;
char ch;
//char e;
//char m;
//int num; //
int x, y;
S1 = VS;
InitSeStack(S1);
while (!QueueEmpty(Q))
{
ch = DeQueue(Q);
if (ch >= '0' && ch <= '9')
{
Push(S1, ch - '0'); //转换
}
else
{
y = Pop(S1);
x = Pop(S1);
switch (ch)
{
case '+' : Push(S1, x + y); break; //num = atoi("string") 字符转整型
case '-' : Push(S1, x - y); break;
case '*' : Push(S1, x * y); break;
case '/' : Push(S1, x / y); break;
}
}
} //while
//num = atoi("GetTop (S, m)"); //
return GetTop(S1);
}
void main()
{
//char e;
SqQueue Q;
SqQueue PostQ; //定义队列,存放后缀表达式
Q = PostQ;
InitQueue(Q); //初始化队列
CTPostExp(Q); //调用转换函数将中缀表达式转换成后缀表达式
while (!QueueEmpty(Q))
{
printf("%c",DeQueue(Q)); //
}
printf("\n");
CPostExp(Q);
printf("The result is:");
printf("%d",CPostExp(Q));
printf("\n");
}