⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expression.c

📁 ARM 下的表达式计算的不错的代码
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               北京北方联创高科汽车电子研究所
**                                 http://www.touchbon.com
**
**--------------File Info-------------------------------------------------------------------------------
** File Name: 		Expression.c
** Created By: 		ZhaoSong
** Created date:   	2007-08-1
** Version:			T10
** Descriptions: 	
**
**------------------------------------------------------------------------------------------------------
** Modified by:		
** Modified date:	
** Version:			
** Descriptions:
**
********************************************************************************************************/

#include "Expression.h"

//运算符优先级列表
char	PriorityTable[OPRATOR_CNT+1][OPRATOR_CNT+1]={
						{0,  '+',  '-',  '*',  '/',  '(',  ')',  '%',  '&',  '^',  '|',  '>',  '<',  'b',  's',  'a',  'o',  'e',  'n',  '#'},
						{'+','>',  '>',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'-','>',  '>',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'*','>',  '>',  '>',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'/','>',  '>',  '>',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'(','<',  '<',  '<',  '<',  '<',  '=',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  ' '},
						{')','>',  '>',  '>',  '>',  ' ',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'%','>',  '>',  '>',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'&','<',  '<',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '<',  '<',  '<',  '<',  '>',  '>',  '<',  '<',  '>'},
						{'^','<',  '<',  '<',  '<',  '<',  '>',  '<',  '<',  '>',  '>',  '<',  '<',  '<',  '<',  '>',  '>',  '<',  '<',  '>'},
						{'|','<',  '<',  '<',  '<',  '<',  '>',  '<',  '<',  '<',  '>',  '<',  '<',  '<',  '<',  '>',  '>',  '<',  '<',  '>'},
						{'>','<',  '<',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'<','<',  '<',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'b','<',  '<',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'s','<',  '<',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>',  '>'},
						{'e','<',  '<',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '<',  '<',  '<',  '<',  '>',  '>',  '>',  '>',  '>'},
						{'n','<',  '<',  '<',  '<',  '<',  '>',  '<',  '>',  '>',  '>',  '<',  '<',  '<',  '<',  '>',  '>',  '>',  '>',  '>'},
						{'a','<',  '<',  '<',  '<',  '<',  '>',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '>',  '>',  '<',  '<',  '>'},
						{'o','<',  '<',  '<',  '<',  '<',  '>',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '>',  '<',  '<',  '>'},
						{'#','<',  '<',  '<',  '<',  '<',  ' ',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '<',  '>'}
						
};

/*********************************************************************************************************
** Function name:		EvaluateExpression		
** Descriptions:		计算表达式的值
** Input parameters:		pExpression				表达式串
							pPara					表达式参数	
** Returned value:			返回表达式的值
** Used global variables:	NONE
** Created by:				ZhaoSong
** Created Date:			2007-08-1
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************************/
float EvaluateExpression(char *pExpression,float *pPara)
{
	u32	  dwTmp;
	u8	  uPos;
	float fLeftValue,fRightValue;
	
	S_OprandStack	G_sOprand;
	S_OpratorStack  G_sOprator;
	G_sOprator.uTop=0;
	G_sOprator.cBuff[G_sOprator.uTop]='#';
	
	G_sOprand.uTop=0;
	
	while(*pExpression !='#' || G_sOprator.cBuff[G_sOprator.uTop]!='#')
	{
		if(*pExpression == 0x20)						//跳过空格
		{
			pExpression++;
			continue;
		}
			
		if(*pExpression >='0' && *pExpression <='9')	//数字
		{
			uPos=GetInt(pExpression,&dwTmp);
			pExpression+=uPos;
			
			G_sOprand.fBuff[++G_sOprand.uTop]=(float)dwTmp;
			
			
		}
		else
		if(*pExpression >='A' && *pExpression <='Z')	//变量
		{
			G_sOprand.fBuff[++G_sOprand.uTop]=pPara[*pExpression-'A'];
			
			pExpression++;
		}
		else											//运算符
		{
			switch(Precede(G_sOprator.cBuff[G_sOprator.uTop],*pExpression))
			{
			case '<':		//栈顶元素优先权低
				G_sOprator.cBuff[++G_sOprator.uTop]=*pExpression;
				pExpression++;
				break;
			case '=':		//'('遇见')'
				G_sOprator.uTop--;	
				pExpression++;
				break;
			case '>':
				fRightValue=G_sOprand.fBuff[G_sOprand.uTop--];
				fLeftValue=G_sOprand.fBuff[G_sOprand.uTop--];
				
				G_sOprand.fBuff[++G_sOprand.uTop]=Operate(G_sOprator.cBuff[G_sOprator.uTop--],fLeftValue,fRightValue);
				
				break;
			case 0:
			case 0x20:									//不支持的运算符
				*pExpression ='#';						
				G_sOprator.cBuff[G_sOprator.uTop]='#';
				break;
			}	
		}
	}
	
	return G_sOprand.fBuff[G_sOprand.uTop];
}

/*********************************************************************************************************
** Function name:		GetInt		
** Descriptions:		取得字符串中的整数
** Input parameters:		pExpression				表达式串
							pValue					取得的整数指针	
** Returned value:			返回整数在字符串中所占的字符数
** Used global variables:	NONE
** Created by:				ZhaoSong
** Created Date:			2007-08-1
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************************/
u8	GetInt(char *pExpression,u32 *pValue)
{
	u8 uCnt=0;
	u8	uValue=0;
	
	*pValue=0;
	
	while(*pExpression >='0' && *pExpression <='9')
	{
		uValue=*pExpression - '0';
		*pValue=(*pValue)*10 + uValue;
		pExpression++;
		uCnt++;
	}
	
	return uCnt;
}
/*********************************************************************************************************
** Function name:		Precede	
** Descriptions:		比较运算符栈顶的运算符与当前表达式串的运算符的优先级
** Input parameters:		cStackOp				运算符栈顶运算符
							cExOp					正在操作的运算符
** Returned value:			'>'						栈顶运算符高于正在操作的运算符
							'<'						栈顶运算符低于正在操作的运算符
							'='						栈顶运算符等于正在操作的运算符
							' '						两个运算符没有关系
** Used global variables:	NONE
** Created by:				ZhaoSong
** Created Date:			2007-08-1
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************************/
char Precede(char cStackOp,char cExOp)
{
	u8 uTmp,uTmp1;
	char cResult=0;
	
	for(uTmp=1;uTmp<OPRATOR_CNT+1;uTmp++)
	{
		if(PriorityTable[uTmp][0]==cStackOp)
		{
			for(uTmp1=1;uTmp1<OPRATOR_CNT+1;uTmp1++)
			{
				if(PriorityTable[0][uTmp1]==cExOp)
					cResult=PriorityTable[uTmp][uTmp1];
					
			}
		}
	}
	
	return cResult;
}

/*********************************************************************************************************
** Function name:		Operate
** Descriptions:		根据运算符计算两个数的值
** Input parameters:		cOprator				计算用的运算符
							fLeftValue				运算符左边的数
							fRightValue				运算符右边的数
** Returned value:			返回表达式的值
							返回0表示不支持此运算符
** Used global variables:	NONE
** Created by:				ZhaoSong
** Created Date:			2007-08-1
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************************/
float Operate(char cOprator,float fLeftValue,float fRightValue)
{
	switch(cOprator)
	{
	case '+':
		return (float)(fLeftValue + fRightValue);
	case '-':
		return (float)(fLeftValue - fRightValue);
	case '*':
		return (float)(fLeftValue * fRightValue);
	case '/':
		return (float)(fLeftValue / fRightValue);
	case '%':
		return (float)((u32)fLeftValue % (u32)fRightValue);
	case '&':
		return (float)((u32)fLeftValue & (u32)fRightValue);
	case '^':
		return (float)((u32)fLeftValue ^ (u32)fRightValue);
	case '|':
		return (float)((u32)fLeftValue | (u32)fRightValue);
	case '>':
		return (float)((u32)fLeftValue > (u32)fRightValue);
	case '<':
		return (float)((u32)fLeftValue < (u32)fRightValue);
	case 'b':
		return (float)((u32)fLeftValue >= (u32)fRightValue);
	case 's':
		return (float)((u32)fLeftValue <= (u32)fRightValue);
	case 'a':
		return (float)((u32)fLeftValue && (u32)fRightValue);
	case 'o':
		return (float)((u32)fLeftValue || (u32)fRightValue);
	case 'e':
		return (float)((u32)fLeftValue == (u32)fRightValue);
	case 'n':
		return (float)((u32)fLeftValue != (u32)fRightValue);
	default:
		return 0;
	
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -