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

📄 express.cpp

📁 任意数学表达式计算的源码及测试软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Express.cpp: implementation of the CExpress class.
//
//////////////////////////////////////////////////////////////////////



/* 表达式计算类:  CExpress
版权声明:该类版权属于 南京航空航天大学 能源与动力学院 庄三少
用于其它商业用途前,请先通知作者,否则视为违法。

  庄三少 TEL:13512524413  09.3.19
*/
#include "stdafx.h"
#include "Express.h"
#include <iostream.h>
#include <math.h>


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CExpress::CExpress()
{
	stepNum=0;

}

CExpress::~CExpress()
{

}

CString CExpress::GetStrFromStartAndEnd(CString str,int start, int end)
{


	CString s;
	s="";
	for (int i=start+1;i<end;i++)
	{
		s+=str.GetAt(i);
	}
	return s;
}
void CExpress::GetBracketPos(CString str,int *start,int *end)
{
	int pos;
	pos=str.Find("(");
	str.SetAt(pos,'d');
	while(str.Find("(",0)!=-1)
	{
		pos=str.Find("(",0);
		str.SetAt(pos,'d');
	}
	*start=pos;
	pos=str.Find(")");
	while(pos<*start)
	{
		pos=str.Find(")",0);
		str.SetAt(pos,'d');
	}
	*end=pos;

}
CString CExpress::ReplaceBetweenPos(CString toBeReplace,CString str,int start,int end)
{
	static fg=0;
	fg++;

	CString s;
	toBeReplace.Delete(start+1,end-start);
	toBeReplace.SetAt(start,'@');
	if (start>0)
	{
    	if (str.GetAt(0)=='-'&&toBeReplace.GetAt(start-1)=='-')
		{
			str.Delete(0,1);
			toBeReplace.SetAt(start-1,'+');
		}
		if (str.GetAt(0)=='-'&&toBeReplace.GetAt(start-1)=='+')
		{
			str.Delete(0,1);
			toBeReplace.SetAt(start-1,'-');
		}
		if (str.GetAt(0)=='+'&&toBeReplace.GetAt(start-1)=='-')
		{
			str.Delete(0,1);
			toBeReplace.SetAt(start-1,'-');
		}

		if (str.GetAt(0)=='-'&&toBeReplace.GetAt(start-1)=='*')//problem
		{

		//	
			CString s1,s2;
			int s_s,s_e,p_s,p_e,p_m;
			double d1,d2,d3;
			GetJJCCTwoParm(toBeReplace,start-1,&s1,&s2,&p_s,&p_e);
            d1=atof(s1);
			if (p_s-1>0)
			{
				//AfxMessageBox("00");
				if (toBeReplace.GetAt(p_s-1)=='-')
				{
					toBeReplace.SetAt(p_s-1,'+');

				}
				if (toBeReplace.GetAt(p_s-1)=='+')
				{
					toBeReplace.SetAt(p_s-1,'-');
				}
				toBeReplace.Delete(start-1,1);
				str.Delete(0,1);
		    	d2=atof(str);
				d3=d2*d1;
			}
			else
			{
				toBeReplace.Delete(start-1,1);
				d2=atof(str);
				d3=d2*d1;

			}
			s1.Format("%f",d3);
            toBeReplace.Delete(p_s,p_e-p_s-1);
			toBeReplace.Replace("@",s1);
			return toBeReplace;
		}

		//////////////////////
		if (str.GetAt(0)=='-'&&toBeReplace.GetAt(start-1)=='/')//problem
		{
			
			//	
			CString s1,s2;
			int s_s,s_e,p_s,p_e,p_m;
			double d1,d2,d3;
			GetJJCCTwoParm(toBeReplace,start-1,&s1,&s2,&p_s,&p_e);
            d1=atof(s1);
			if (p_s-1>0)
			{
				//AfxMessageBox("00");
				if (toBeReplace.GetAt(p_s-1)=='-')
				{
					toBeReplace.SetAt(p_s-1,'+');
					
				}
				if (toBeReplace.GetAt(p_s-1)=='+')
				{
					toBeReplace.SetAt(p_s-1,'-');
				}
				toBeReplace.Delete(start-1,1);
				str.Delete(0,1);
				d2=atof(str);
				d3=d1/d2;
			}
			else
			{
				toBeReplace.Delete(start-1,1);
				d2=atof(str);
				d3=d1/d2;
				
			}
			s1.Format("%f",d3);
            toBeReplace.Delete(p_s,p_e-p_s-1);
			toBeReplace.Replace("@",s1);
			return toBeReplace;
		}
		
		

		
       

			
	}
	toBeReplace.Replace("@",str);
	return toBeReplace;
}
bool CExpress::IsContainSign(CString str)
{
	bool f=false;
	if (str.Find("sin")!=-1)
		f=true;
	if (str.Find("cos")!=-1)
		f=true;
	if (str.Find("tan")!=-1)
		f=true;
	if (str.Find("log")!=-1)
		f=true;
	if (str.Find("exp")!=-1)
		f=true;
	if (str.Find("sign")!=-1)
		f=true;
	if (str.Find("asin")!=-1)
		f=true;
	if (str.Find("acos")!=-1)
		f=true;
	if (str.Find("atan")!=-1)
		f=true;
	if (str.Find("pow")!=-1)
		f=true;
	return f;
	
}
int CExpress::GetSignStyle(CString str,int *sing_s,int *sign_e,int *p_s,int *p_e)
{
	CString nane[10]={"sin","cos","tan","exp","log","asin",
		"acos","atan","sign","pow"};
	int pos,j;
	j=-1;
	CString s1=str;

	pos=str.Find("]");
	*p_e=pos;
	while (1)
	{
		if (str.GetAt(pos+j)=='[')
		{
		
			break;
		}
		else
			j--;
		
	}
	pos=pos+j;
	*p_s=pos;
	int pos1,pos2;
	j=-1;
	while (1)
	{
		if (str.GetAt(*p_s+j)=='+'||str.GetAt(*p_s+j)=='-'||str.GetAt(*p_s+j)=='('||str.GetAt(*p_s+j)=='['||str.GetAt(*p_s+j)=='*'||str.GetAt(*p_s+j)=='/'||*p_s+j==0)
			break;
		else
			j--;
		
	}
 	*sing_s=*p_s+j+1;
	*sign_e=*p_s-1;
    
	CString sg;
	sg="";
	if(*sing_s==1)  *sing_s=0;
	for (j=*sing_s;j<=*sign_e;j++)
	{
		sg+=str.GetAt(j);
	}
	for (int i=0;i<10;i++)
	{
		if (sg==nane[i])
		{

			return i;
		}
					
	}
    	

}
CString CExpress::GetParmString(CString str,int start,int end)
{
	
	return GetStrFromStartAndEnd(str,start,end);
	

}
void CExpress::GetParmTwo(CString str,int start,int end,CString *s1,CString *s2)
{

    CString s=GetStrFromStartAndEnd(str,start,end);
	int p1=s.Find(",");
	*s1=GetStrFromStartAndEnd(s,-1,p1);
	*s2=GetStrFromStartAndEnd(s,p1,s.GetLength());
}
bool CExpress::IsJJCC(CString str)
{
	if (str.Find("+")!=-1)
	{
		return true;
	}
	if (str.Find("-")!=-1)
	{
		return true;
	}
	if (str.Find("*")!=-1)
	{
		return true;
	}
	if (str.Find("/")!=-1)
	{
		return true;
	}
	return false;
}
 void CExpress::GetJJCCTwoParm(CString str,int pos,CString *s1,CString*s2,int *p_start,int *p_end)
 {
 	int start,end,i;
 	i=-1;
 	while (1)
	{
		if (str.GetAt(pos+i)=='+'||str.GetAt(pos+i)=='-'||str.GetAt(pos+i)=='*'||str.GetAt(pos+i)=='/'||pos+i==0)
	    break;
		else
		i--;
			
	}
 	start=pos+i;
	if (start==0)
	{
		start=-1;
	}
 	i=1;
	while (1)
	{
		if (str.GetAt(pos+i)=='+'||str.GetAt(pos+i)=='-'||str.GetAt(pos+i)=='*'||str.GetAt(pos+i)=='/'||pos+i==str.GetLength()-1)
			break;
		else
			i++;
		
	}
 	end=pos+i;
	if (end==str.GetLength()-1)
	{
		end++;
	}
	*p_start=start+1;
	*p_end=end-1;
 	*s1=GetStrFromStartAndEnd(str,start,pos);
 	*s2=GetStrFromStartAndEnd(str,pos,end);
	//str.Format("%d %d %d ",start,pos,end);
	//AfxMessageBox(str);
	

 }
bool  CExpress::IsAddExist(CString str)
{
	if (str.Find("+")!=-1)
	{
		return true;
	}
	
	return false;

}
bool  CExpress::IsPulsExist(CString str)
{

	str.SetAt(0,'@');
	if (str.Find("-")!=-1)
	{
		return true;
	}
	return false;

}
bool  CExpress::IsTimesExist(CString str)
{

	if (str.Find("*")!=-1)
	{
		return true;
	}
	return false;

}
bool  CExpress::IsDivideExist(CString str)
{
	
	if (str.Find("/")!=-1)
	{
		return true;
	}
	return false;

}
int  CExpress::GetJJCCPos(CString str,CString style)
{
	if (style=="+")
	{
		return str.Find("+");
	}
	if (style=="-")
	{
		str.SetAt(0,'@');
		return str.Find("-");
	}
	if (style=="*")
	{
		return str.Find("*");
	}
	if (style=="/")
	{
		return str.Find("/");
	}
	return -1;
}
void CExpress::GetSingleValue(CString str,double &result)
{
	bool falg=false;
    if (IsAddExist(str)||IsTimesExist(str)||IsDivideExist(str))
    {
		falg=true;
    }
	if (str.GetAt(0)=='-'&&falg&&GetJJCCNum(str,"-")>1)
	{
		str.SetAt(0,'@');
		str.Replace("@","0-");
	}
	int pos;
	CString s1,s2;
	double d1,d2,d3;
	int start,end;
	if (IsTimesExist(str)&&IsDivideExist(str))
	{
		int pos1=GetJJCCPos(str,"*");
		pos=GetJJCCPos(str,"/");
		if (pos>pos1)
		{
			
			pos=GetJJCCPos(str,"*");
			GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
			d1=atof(s1);
			d2=atof(s2);
			d3=d1*d2;
			s1.Format("%f",d3);
			str=ReplaceBetweenPos(str,s1,start,end);
			GetSingleValue(str,result);

		}
		else
		{
			pos=GetJJCCPos(str,"/");
			GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
			d1=atof(s1);

⌨️ 快捷键说明

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