用堆栈把中缀表达式转化为后缀表达式,之后再求后缀表达式结果

红色部分为在上一篇文章上补充的部分

#include "stdafx.h"


#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100


//转换中缀表达式用的符号(+-/*)堆栈,为了最后转换成后缀表达式链表用
typedef struct Stack
{int arr[MAXSIZE];
int top;
}Stack;
Stack stack;  




int topVal(Stack *stack)
{
  return stack->arr[stack->top];
}


int position(Stack *stack)
{
  return stack->top;
}
int isEmpty(Stack *stack)
{
if (stack->top>-1) return 0;
else return 1;
}
void push(int num,Stack *stack)
{if( stack->top>=MAXSIZE)
  return ;
 else
 {stack->top++;
 (stack->arr[stack->top]=num);
 }
}


int pop(Stack *stack)
{  int ret;
if( stack->top<=-1)
  return -1;
 else
 {ret=stack->arr[stack->top]; stack->top--;}
 return ret;
}


//用链表实现后缀表达式,
typedef struct Node

union
{char exp;
int a;
} data;
 struct Node *next;
}Node;


/*
void push(double num,Node *head)
{if( head->next=NULL)
  return ;
 else
 {
Node* newnode=(Node*)malloc(sizeof(Node));
                   newnode->next=head->next;
 newnode->data.a=num;    
    head->next=newnode;
head->top++;
 }
}
double pop(Node *head)
{double ret=0;
if( head->top==-1)
  return -1 ;
 else
 {
Node* newnode=head->next;
ret=  newnode->data.a;    
head->next=newnode->next;
head->top--;
free(newnode);
 }
 return ret;
}


*/


         






int isExpression(int tmpExp){
if(tmpExp=='+'||tmpExp=='-'||tmpExp=='/'||tmpExp=='*'||tmpExp=='('||tmpExp==')')
  return 1;
else
return 0;
}
int isSuperior(int tmpExp,int tmpExp2){


if(tmpExp2=='('||(tmpExp=='/'||tmpExp=='*')&&(tmpExp2=='+'||tmpExp2=='-'))
 return 1;
else return 0;


}
void printNode(Node* node)
{
if(node)
{char tmpExp=node->data.exp;
 if(isExpression(tmpExp))
      printf("%c",node->data.exp);
 else
   printf("%d",node->data.a);
if(node)
  printNode(node->next);
}
}


//转换为后缀表达式
Node * head=NULL;
Node *  surfixExpCalculate(Node* node)
{
       Node * newnode=(Node*)malloc(sizeof(Node));
  Node * tmpNode;
head=tmpNode=newnode;
head->data.exp=' ';
head->data.a=0;
while(node=node->next)
{
  
       
if(!(isExpression(node->data.exp)))
{
 newnode=(Node*)malloc(sizeof(Node));
                   newnode->next=0;
 newnode->data.a=node->data.a;    
    tmpNode->next=newnode;
                 tmpNode=newnode;
}else
{ char tmpExp=node->data.exp;
if(tmpExp=='(')
{
  push(tmpExp,&stack);

}else if(tmpExp==')')
{
while(!isEmpty(&stack)&&topVal(&stack)!='(')
{
newnode=(Node*)malloc(sizeof(Node));
                            newnode->next=0;
newnode->data.exp=pop(&stack);
     
   tmpNode->next=newnode;
                            tmpNode=newnode;
}
pop(&stack);
}
else
{
if(isEmpty(&stack)||isSuperior(tmpExp,topVal(&stack)))
{
push(tmpExp,&stack);
}
else
{
   while(!isEmpty(&stack)&&!isSuperior(tmpExp,topVal(&stack)))

newnode=(Node*)malloc(sizeof(Node));
                            newnode->next=0;
newnode->data.exp=pop(&stack);
     
   tmpNode->next=newnode;
                            tmpNode=newnode;

};
 
push(tmpExp,&stack);
}
}


}


}
while (!isEmpty(&stack))
    {
newnode=(Node*)malloc(sizeof(Node));
                            newnode->next=0;
newnode->data.a=pop(&stack);
     
   tmpNode->next=newnode;
                            tmpNode=newnode;
    }


return head;


}
typedef struct Stack2                                                           //为了求后缀表达式结果,增加一个含double的成员的stack2
{double arr[MAXSIZE];
int top;
}Stack2; 
void push(double num,Stack2 *stack)
{if( stack->top>=MAXSIZE)
  return ;
 else
 {stack->top++;
 (stack->arr[stack->top]=num);
 }
}


double pop(Stack2 *stack)
{  double ret;
if( stack->top<=-1)
  return -1;
 else
 {ret=stack->arr[stack->top]; stack->top--;}
 return ret;
}
// 后缀表达式求值程序
double postFixEval(Node * head)
{ double val=0;
  Node * node=head->next;
  Stack2 stack2;                                                       //用到another堆栈,计算后缀表达式结果
  stack2.top=-1;                                                         //记得设置为-1,调试时发现的问题
while(node)
{
if(!isExpression(node->data.exp))
{
push(node->data.a,&stack2);
}
else
{
double op1=pop(&stack2);
double op2=pop(&stack2);


switch (node->data.exp)
{
case '+':
val = op1 + op2;
break;
case '-':
val = op2 - op1;
break;
case '*':
val = op1 * op2;
break;
case '/':
val = op2 / op1;
break;
}
                push(val,&stack2);
}
node=node->next;


}


return val=pop(&stack2);
}


int main(int argc, char* argv[])
{ stack.top=-1;


Node * head;
int i=0,length=0;
 char buf[1024];
char* tmpExp,sep[]=" ";
char *p=NULL;
  head=(Node*)malloc(sizeof(Node));
  head->data.exp=' ';
  head->data.a=0;
 head->next=0;


gets(buf);
tmpExp=buf;

Node* tmpExpNode=head;


//输入为中缀表达式

while(p=strtok(tmpExp,sep))
{


  Node * node=(Node*)malloc(sizeof(Node));
  node->next=0;
 // printf("%s",p); p is a point to  string (not a point to a char)
if(strcmp(p,"+")==0||strcmp(p,"+")==0||strcmp(p,"-")==0||strcmp(p,"*")==0||strcmp(p,"/")==0||strcmp(p,"(")==0||strcmp(p,")")==0)
sscanf(p,"%c",&node->data.exp);
else
 sscanf(p,"%d",&node->data.a);
   
tmpExpNode->next=node;
   tmpExpNode=node;
   tmpExp=0;
 }
head=surfixExpCalculate(head);
//printNode(head->next);


printf("%5.2f\n",postFixEval(head));


  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值