數據結構_表達式求值

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define dx 105
#define zl 10
char Prior[7][7] =  // 運算符優先級表
{    
    '>','>','<','<','<','>','>',   
    '>','>','<','<','<','>','>',   
    '>','>','>','>','<','>','>',   
    '>','>','>','>','<','>','>',   
    '<','<','<','<','<','=',' ',   
    '>','>','>','>',' ','>','>',   
    '<','<','<','<','<',' ','='     
};
typedef struct  //構造棧運算符棧 
{
	char *base;
	char *top;
	int stacksize;
}fqstack;
int finitqstack(fqstack *s) //初始化運算符棧
{
	s->base=(char*)malloc(dx*sizeof(char));
	if(!s->base) return -2;
	s->top=s->base;
	s->stacksize=dx;
	return 1;
}
typedef struct  //構造棧運算數棧 
{
	double *base;
	double *top;
	int stacksize;
}sqstack;
int sinitqstack(sqstack *s) //初始化運算數棧
{
	s->base=(double*)malloc(dx*sizeof(double));
	if(!s->base) return -2;
	s->top=s->base;
	s->stacksize=dx;
	return 1;
}
int fpush(fqstack *s,char e) //運算符棧入棧 
{
	if(s->top-s->base>=dx)
	{
		s->base=(char*)realloc(s->base,(dx+zl)*sizeof(char));
		if(!s->base) return -2;
		s->top=s->base+s->stacksize;
		s->stacksize+=zl;
	}
	*s->top++=e;
	return 1;
}
int spush(sqstack *s,double e) //運算數棧入棧 
{
	if(s->top-s->base>=dx)
	{
		s->base=(double*)realloc(s->base,(dx+zl)*sizeof(double));
		if(!s->base) return -2;
		s->top=s->base+s->stacksize;
		s->stacksize+=zl;
	}
	*s->top++=e;
	return 1;
}
char ftop(fqstack *s) //運算符訪問棧頂 
{
    if(s->base==s->top) return -2;
	return *(s->top-1);	
}
double stop(sqstack *s) //運算數訪問棧頂 
{
    if(s->base==s->top) return -2;
	return *(s->top-1);	
}  
int fpop(fqstack *s,char *x) //運算符彈棧 
{
	if(s->base==s->top) return -2;
	*x=*--s->top;
	return 1;
}
int spop(sqstack *s,double *x) //運算數彈棧 
{
	if(s->base==s->top) return -2;
	*x=*--s->top;
	return 1;
}
double Operate(double a, char theta, double b)  //計算  
{  
    switch(theta)  
    {  
    case '+': return a+b;   
    case '-': return a-b;   
    case '*': return a*b;   
    case '/': return a/b;     
    default : return 0;   
    }   
}
int is_lx(char c) //判斷是運算符還是運算數 
{
	if(c>='0'&&c<='9') return 1;
	return 0;
}
char is_yxj(char c1,char c2) //取下標數組並比較優先級 
{
	string ss="+-*/()#";  
	return Prior[ss.find(c1)][ss.find(c2)];
}
int main()
{
	fqstack optr;
	sqstack opnd;
	finitqstack(&optr);
	fpush(&optr,'#');
	sinitqstack(&opnd);
	char c,x,theta;
	double a,b;
	c=getchar();
	while(c!='#'||ftop(&optr)!='#')
	{
		if(is_lx(c))
		{
			int xx=0;
			while(is_lx(c))
			{
			   xx=xx*10+(c-'0');
			   c=getchar();
			}
			   spush(&opnd,xx);
		}
		else
		{
			switch(is_yxj(ftop(&optr),c))
			{
				case '<':
					fpush(&optr,c);
					c=getchar();
					break;
				case '=':
					fpop(&optr,&x);
					c=getchar();
					break;
				case '>':
				    fpop(&optr,&theta);
					spop(&opnd,&b);
					spop(&opnd,&a);
					spush(&opnd,Operate(a,theta,b));
					break;	
			}
		}
	}
	printf("%.2lf\n",stop(&opnd));
    return 0;
}

發佈了25 篇原創文章 · 獲贊 10 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章