红色部分为在上一篇文章上补充的部分
#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 * 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;
}
#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;
//输入为中缀表达式
{
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;
}