程序目的是为了实现算数表达式转换为后缀表达式再求其值
程序正常运行了但是没有将结果输出,三个输出都没输出,调试的报错没看懂什么意思,希望有人解答一下
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 200
#define false 0
#define true 1
typedef struct Node
{
char data[MaxSize];
int top;
}SqStack;//创建一个运算符栈
typedef struct Node1
{
double data[MaxSize];
int top;
}SqStack_double;//创建操一个作数栈
void InitStack(SqStack* head)//运算符栈的初始化
{
head = (SqStack*)malloc(sizeof(SqStack));
head->top = -1;
}
void InitStack_double(SqStack_double* head)//操作数栈的初始化
{
head = (SqStack_double*)malloc(sizeof(SqStack_double));
head->top = -1;
}
int StackEmpty(SqStack* head)//判断栈是否为空的函数
{
if (head->top == -1) {
return false;
}
else {
return true;
}
}
int GetTop(SqStack* head, char* n)//取运算符栈顶函数
{
if (head->top == -1) {
return false;
}
*n = head->data[head->top];
return true;
}
int GetTop_double(SqStack_double* head, double* n)//取操作数栈顶函数
{
if (head->top == -1) {
return false;
}
*n = head->data[head->top];
return true;
}
int Push(SqStack* head,char n)//运算符栈入栈函数
{
if (head->top == MaxSize - 1) {
return false;
}
head->top++;
head->data[head->top] = n;
return true;
}
int Push_double(SqStack_double* head, double *n)//操作数栈入栈函数
{
if (head->top == MaxSize - 1) {
return false;
}
head->top++;
head->data[head->top] = *n;
return true;
}
int Pop(SqStack* head, char *n)//运算符栈出栈函数
{
if (head->top == -1) {
return false;
}
*n = head->data[head->top];
head->top--;
return true;
}
int Pop_double(SqStack_double* head, double* n)//操作数栈出栈函数
{
if (head->top == -1) {
return false;
}
*n = head->data[head->top];
head->top--;
return true;
}
void DestroyStack(SqStack* head)//销毁运算符栈
{
free(head);
}
void DestroyStack_double(SqStack_double* head)//销毁操作数栈
{
free(head);
}
int main()
{
void transform(char* exp, char* postexp);//声明将表达式转换为后缀表达式的函数
double evaluation(char* postexp);//声明计算后缀表达式的函数
char exp[] = "(56-20)/(4+2)";//存放表达式的数组
char postexp[MaxSize];//存放后缀表达式的数组
transform(exp, postexp);//将算数表达式转换为后缀表达式
printf("Arithmetic expression:%s\n", exp);//输出算数表达式
printf("Postfix Expression:%s\n", postexp);//输出后缀表达式
printf("Value of expression:%lf\n", evaluation(postexp));//输出最终结果
return 1;
}
void transform(char* exp, char* postexp)//将算术表达式转换为后缀表达式
{
char e;
SqStack Optr;//定义存放运算符的栈
InitStack(&Optr);//初始化运算符栈
int i=0;//作为postexp的下标
while (*exp != '\0')
{
switch (*exp)
{
case '(':
Push(&Optr,'(');//将运算符‘(’存入运算符栈中
exp++;//继续遍历其它字符
break;
case ')':
Pop(&Optr, &e);//将运算符栈中的栈顶元素出栈
while (e != '(')//当出栈元素不为右括号时,将运算符栈中的元素的全部出栈
{
postexp[i++] = e;//并存入后缀表达式中
Pop(&Optr, &e);//继续出栈下一个元素
}
exp++;//继续遍历表达式数组中的下一个元素
break;
case '-':
case '+':
while (StackEmpty(&Optr) != 0)//当运算符栈空时停止循环
{
GetTop(&Optr, &e);//取栈顶运算符元素
if (e != '(') {//如果栈顶元素不为左括号
postexp[i++] = e;//将栈顶运算符存入后缀表达式中
Pop(&Optr, &e);//将该运算符出栈
}
else {//如果是左括号中止将运算符存入后缀表达式的循环
break;
}
}
Push(&Optr, *exp);//将+或-运算符存入运算符栈中
exp++;//继续遍历表达式下一个元素
break;
case '*':
case '/':
while (StackEmpty(&Optr) != 0)//运算符栈不为空时循环
{
GetTop(&Optr, &e);//取栈顶元素
if (e == '*' || e == '/') {//由于乘除优先级高当出栈运算符为*或/时
postexp[i++] = e;//存入后缀表达式中
Pop(&Optr, &e);//存入后将该运算符出栈
}
else {//若不为乘除运算符中止循环
break;
}
}
Push(&Optr, *exp);//将新遍历到的*或/运算符入栈
exp++;//继续遍历表达式剩下的元素
break;
default://处理数字字符
while (*exp >= '0' && *exp <= '9')//当数组exp当前位置存放的是操作数时
{
postexp[i++] = *exp;//存入后缀表达式中
exp++;//表达式数组往下一位移动
}
postexp[i++] = '#';//用字符#把相邻的操作数隔开表示一个数字串结束
}
}
//此时表达式遍历完毕
while (StackEmpty(&Optr) != 0)//若运算符栈不为空,将栈中剩余的运算符存入后缀表达式
{
Pop(&Optr, &e);//运算符出栈
postexp[i++] = e;//存入后缀表达式
}
postexp[i++] = '\0';//给后缀表达式结尾加上字符结束符
DestroyStack(&Optr);//销毁运算符栈
}
double evaluation(char* postexp)//计算后缀表达式
{
double a, b, c, d, e;//定义操作数
SqStack_double opnd;//定义操作数栈
InitStack_double(&opnd);
while (*postexp != '\0')
{
switch (*postexp)
{
case '+':
Pop_double(&opnd, &a);
Pop_double(&opnd, &b);
c = a + b;
Push_double(&opnd, &c);
break;
case '-':
Pop_double(&opnd, &a);
Pop_double(&opnd, &b);
c = a - b;
Push_double(&opnd, &c);
break;
case '*':
Pop_double(&opnd, &a);
Pop_double(&opnd, &b);
c = a * b;
Push_double(&opnd, &c);
break;
case '/':
Pop_double(&opnd, &a);
Pop_double(&opnd, &b);
if (a != 0)
{
c = b/a;
Push_double(&opnd, &c);
break;
}
else {
printf("Divide by zero error\n");
exit(0);//异常退出
}
break;
default://处理数字字符
d = 0;
while (*postexp>='0'&&*postexp<='9')//当后缀表达式中元素为数字字符时进入循环
{
d = d * 10 + *postexp - '0';//将两个连续的数字字符转换为对应的数值
postexp++;//遍历下一个操作数
}
Push_double(&opnd, &d);//将两个操作数合并成的运算数存入操作数栈中
break;//结束循环
}
postexp++;//继续处理其它元素
}
//当后缀表达式运算结束后
GetTop_double(&opnd, &e);//将结果从操作数栈中取出
DestroyStack_double(&opnd);//销毁操作数栈
return e;//将最终结果返回
}