本帖最后由 mogur 于 2021-11-14 10:20 编辑
代码如下,代码我在别人中缀转后缀的代码上加了计算后缀的代码,不会调试我就在可能有数值改动的地方写个printf输出的数看看,
不知道为什么这个代码(188行)这个地方要减去48才可以算对
(不减去48的话在那几行增加个输出的代码的话,如果是1就会输出49)
[C] 纯文本查看 复制代码 Push_Stack(&s2,GetTop(&s1)-48);
Pop_Stack(&s1);
也没有大佬可以解答,谢谢。
[C] 纯文本查看 复制代码 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//----------------------栈的实现--------------------------
typedef char ElemType;
#define STACKSIZE 50
typedef struct Stack //定义栈的结构体
{
ElemType *data;
ElemType top;
int stacksize;
}*StackPtr;
int Init_Stack(StackPtr stack) //初始化
{
if(stack == NULL) exit(0);
stack->data = (ElemType*)malloc(sizeof(ElemType)*STACKSIZE);
stack->stacksize = STACKSIZE;
stack->top = 0;
return true;
}
int Push_Stack(StackPtr stack,ElemType val) //入栈
{
if(stack == NULL) exit(0);
stack->data[stack->top] = val;
stack->top++;
return true;
}
int Pop_Stack(StackPtr stack) //出栈
{
if(stack==NULL) exit(0);
stack->top--;
return true;
}
int GetTop(StackPtr stack) //获取栈顶
{
if(stack == NULL) exit(0);
return stack->data[stack->top-1];
}
int Empty_Stack(StackPtr stack) //判空
{
if(stack == NULL) exit(0);
if(stack->top == 0)
{
return true;
}
else
{
return false;
}
}
static int Apply_Stack(StackPtr stack) //申请空间
{
ElemType* p = (ElemType*)malloc(sizeof(ElemType)*stack->stacksize*2);
if(p == NULL) return false;
for(int i=0;i<stack->top;i++)
{
p[i] = stack->data[i];
}
free(stack->data);
stack->data = p;
p = NULL;
stack->stacksize*=2;
return true;
}
static int Full_Stack(StackPtr stack) //判满
{
if(stack->stacksize == stack->top&&stack->top != 0)
{
Apply_Stack(stack);
return true;
}
else
{
return false;
}
}
int Clear_Stack(StackPtr stack) //清空
{
if(stack == NULL) exit(0);
stack->top = 0;
return true;
}
int Destroy_Stack(StackPtr stack) //销毁
{
if(stack == NULL) exit(0);
free(stack->data);
stack->data = NULL;
stack->top = 0;
stack->stacksize = 0;
return true;
}
//------------------------------------------------------
//--------------------逆波兰式实现------------------------
int OperPre(char oper) //处理优先级
{
int tmp;
switch(oper)
{
case '+':
case '-':
tmp = 1; break;
case '*':
case '/':
tmp = 2;break;
default:
tmp = 3;
}
return tmp;
}
void RPNotation(char *buff) //逆波兰式实现
{
Stack s1,s2; //定义两个栈是s1,s2
Init_Stack(&s1); //初始化栈
Init_Stack(&s2);
while(1) //逆波兰实现
{
if(isdigit(*buff)) //将数字存入s2
{
Push_Stack(&s2,*buff);
}
if(!isdigit(*buff))
{
while(OperPre(*buff)<=OperPre(GetTop(&s1)))
{
if(s1.top == 0) break; //若s1没数据则直接入栈
if(GetTop(&s1)=='(') break; //处理‘(’问题
Push_Stack(&s2,GetTop(&s1)); //将s1的栈顶压入s2
Pop_Stack(&s1); //s1出栈
}
if(*buff != ')') //若出现‘)’则交给下一个if条件语句处理
{
Push_Stack(&s1,*buff);
}
}
if(*buff == ')') //处理‘(’,‘)’问题
{
while(GetTop(&s1) != '(') //将‘(’之前的运算符压入s2中
{
Push_Stack(&s2,GetTop(&s1));
Pop_Stack(&s1);
}
Pop_Stack(&s1); //运算符处理完后,将‘(’出栈
}
buff++;
if(*buff == '\n') break; //结束条件
}
while(s2.top != 0) //将(栈)s2压入(栈)s1
{
Push_Stack(&s1,GetTop(&s2));
Pop_Stack(&s2);
}
//==============================================================
char op;
while(s1.top != 0)
{ op=GetTop(&s1);
int num1,num2,sum;
if(isdigit(op) )
{
Push_Stack(&s2,GetTop(&s1)-48);
Pop_Stack(&s1);
}
else
{
if (op=='+')
{
Pop_Stack(&s1);
num1 = GetTop(&s2);
Pop_Stack(&s2);
num2 = GetTop(&s2);
Pop_Stack(&s2);
sum=num1+num2;
Push_Stack(&s2,sum);
}
else if (op=='-')
{
Pop_Stack(&s1);
num1 = GetTop(&s2);
Pop_Stack(&s2);
num2 = GetTop(&s2);
Pop_Stack(&s2);
sum=num1-num2;
Push_Stack(&s2,sum);
}
else if (op=='*')
{
Pop_Stack(&s1);
num1 = GetTop(&s2);
Pop_Stack(&s2);
num2 = GetTop(&s2);
Pop_Stack(&s2);
sum=num1*num2;
Push_Stack(&s2,sum);
}
else if (op=='/')
{
Pop_Stack(&s1);
num1 = GetTop(&s2);
Pop_Stack(&s2);
num2 = GetTop(&s2);
Pop_Stack(&s2);
sum=num1/num2;
Push_Stack(&s2,sum);
}
}
}
while(s2.top != 0)
{
printf("%d",(int)GetTop(&s2));
Pop_Stack(&s2);
}
}
int main()
{
char buff[128];
printf("中缀表达式:\n");
fgets(buff,127,stdin);
RPNotation(buff);
return 0;
}
|