当前位置:文档之家› 数据结构表达式求值(栈)

数据结构表达式求值(栈)

switch(ch)
{
case '+':c=a+b;break;
case '-':c=a-b;break;
case '*':c=a*b;break;
case '/':if(b!=0) c=a/b;
else
{
printf("Error!出现分母为0情况。\n");
return 0;
}
break;
case '%':c=(float)((int)a%(int)b);break;
w=*infixexp;
while((GetTop_SeqStackc(S,&c),c)!='#'||w!='=')
{
if(w==' ')
{
infixexp++;
w=*infixexp;
continue;
}//过滤空格字符。
if(w=='.')
{
*postfixexp=w;
postfixexp++;
}
Push_SeqStackf(S,c);
}
ch=*A++;
}
GetTop_SeqStackf(S,&result);
Destroy_SeqStackf(&S);
return (result);
}//后辍表达式求表达式的值,通过一个浮点型栈实现的。
void main()
{
printf("******************************************************************************\n");
printf("******************************算术表达式求值****************************\n");
printf("******************************************************************************\n");
puts(str2);
result=postfix_exp(str2);
cout<<"答案是:"<<result<<"\n";
}
return (0);
else
*x=S->data[S->top];
return (1);
}//取栈顶元素。
void Destroy_SeqStackc(PseqStackc *S)
{
if (*S)
free(*S);
*S=NULL;
return ;
}//销毁栈。
typedef struct
{
float data[MAXSIZE];
{
if (*S)
free(*S);
*S=NULL;
return ;
}//销毁栈。
源程序
#include<stdio.h>
#include <iostream.h>
#include<string.h>
#include"cacula.h"
int isnum(char c)
{
if(c>='0'&&c<='9') return 1;
return (0);
else
{
S->top++;
S->data[S->top]=x;
return (1);
}
}//入栈。
int Pop_SeqStackf(PseqStackf S,float *x)
{
if(Empty_SeqStackf(S))
return (0);
else
{
*x=S->data[S->top];
int top;
}SeqStackf,*PseqStackf;//再定义一个线性表栈。
PseqStackf Init_SeqStackf(void)
{
PseqStackf S;
S=(PseqStackf)malloc(sizeof(SeqStackf));
if(S)
S->top=-1;
return(S);
else return 0;
}
int priority(char op)
{
switch(op)
{
case '=':return 1;
case ')':return 2;
case '+':
case '-':return 3;
case '*':
case '%':
case '/':return 4;
case '(':return 5;
return 1;
}//中辍表达式转化成后辍表达式。通过字符型栈来实现。
float postfix_exp(char *A)
{
PseqStackf S;
float result,a,b,c;
char ch;
ch=*A++;
S=Init_SeqStackf();
while(ch!='=')
{
if(isnum(ch))
}
}
else
{
if((GetTop_SeqStackc(S,&c),c)=='('&&w==')')
{
Pop_SeqStackc(S,&topelement);
w=*(++infixexp);
}
else
if((GetTop_SeqStackc(S,&c),c)=='('||priority((GetTop_SeqStackc(S,&c),c))<priority(w))
{
Push_SeqStackc(S,w);
w=*(++infixexp);
}
else
{
Pop_SeqStackc(S,&topelement);
*postfixexp=topelement;
postfixexp++;
}
}
}
*postfixex43;postfixexp)='\0';
Destroy_SeqStackc(&S);
char str1[100];
char str2[MAXSIZE];
float result;
printf("输入算术表达式(须以等号结束,可包含整数、小数的加减乘除取余及带括号的运算):\n");
gets(str1);
infix_exp_value(str1,str2);
printf("输出后辍算术表达式:");
}//判断是否栈空。
int Push_SeqStackc(PseqStackc S,char x)
{
if(S->top==MAXSIZE-1)
return (0);
else
{
S->top++;
S->data[S->top]=x;
return (1);
}
}//入栈。
int Pop_SeqStackc(PseqStackc S,char *x)
w=*(++infixexp);
continue;
}//小数点'.'字符随前后的数一起放入后辍表达式中。
if(isnum(w))
{
*postfixexp=w;
postfixexp++;
w=*(++infixexp);
if(!isnum(w)&&w!='.')
{
*postfixexp=' ';
postfixexp++;
a=a+(ch-'0')/b;
}
}//如果小数点后有数字字符,将其转化为对应位置上的小数加在a上。
Push_SeqStackf(S,a);
ch=*A++;
}//保证像"12.5"这样的字符串转化为单精度数12.5,并放入栈中.
else
{
Pop_SeqStackf(S,&b);
Pop_SeqStackf(S,&a);
default :return 0;
}
}
int infix_exp_value(char *infixexp,char *postfixexp)
{
if(infixexp[strlen(infixexp)-1]!='=')
{
int z;
z=strlen(infixexp);
printf("没有以'='结尾!系统自动为你添加'='。\n");
{
PseqStackc S;
S=(PseqStackc)malloc(sizeof(SeqStackc));
相关主题