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

📄 calculation.cpp

📁 最优化方法newton法求极值的vc++实现 环境:vs2008
💻 CPP
字号:
// Calculation.cpp: implementation of the CCalculation class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Calculation.h"
#include "math.h"
#include "string.h"

//#include <string.h>

using namespace std; 

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define FUNCNUM 15 //函数个数
#define CONSTNUM 11 //常数个数
#define E 2.7182818284590452353
#define PI 3.1415926535897932384


CCalculation::CCalculation()
{	//初始化函数和常数
	m_bDegree=0;
	m_nOutputFlag=0;
	opt[0]="e^";
	opt[1]="ch";
	opt[2]="sqr";
	opt[3]="sh";
	opt[4]="log";
	opt[5]="cos";
	opt[6]="sin";
	opt[7]="tan";
	opt[8]="ln";
	opt[9]="abs";
	opt[10]="at";
	opt[11]="as";
	opt[12]="ac";
	opt[13]="th";
	opt[14]="exp";
	opt2[0]='^';
	opt2[1]='/';
	opt2[2]='*';
	opt2[3]='%';
	opt2[4]='+';
	opt2[5]='-';
	m_strConValue[0]="3.1415926535897932384";	m_strConName[0]="PI";
	m_strConValue[1]="9.80665";					m_strConName[1]="GG";
	m_strConValue[2]="(8.988*10^9)";			m_strConName[2]="EF";
	m_strConValue[3]="0.02241383";				m_strConName[3]="VM";
	m_strConValue[4]="(1.380662*10^(-23))";		m_strConName[4]="BC";
	m_strConValue[5]="8.3144";					m_strConName[5]="MG";
	m_strConValue[6]="(6.6720*10^(-11))";		m_strConName[6]="GR";
	m_strConValue[7]="(6.022045*10^23)";		m_strConName[7]="NA";
	m_strConValue[8]="(2.99792458*10^8)";		m_strConName[8]="LS";
	m_strConValue[9]="(8.854187818*10^(-12))";	m_strConName[9]="DC";
	m_strConValue[10]="2.7182818284590452353";	m_strConName[10]="E";

}

CCalculation::~CCalculation()
{

}
bool CCalculation::IsDigital(CString str)   	//*判断表达式中是否有函数或运算符*
{
	int i;
	while(str.GetAt(0)=='-') str.Delete(0);
	for(i=0;i<FUNCNUM;i++) if(str.Find(opt[i])!=-1) return 0;
	for(i=0;i<6;i++) if(str.Find(opt2[i])!=-1) return 0;
	return 1;
}

CString CCalculation::NtoS(double d)		//*数字转字串*
{
	int  decimal, sign;
	char *buffer;
	CString temp;
	buffer = _ecvt( d, 16, &decimal, &sign );//转换浮点数据到字符串,正返回sign=0负为1 ,decimal返回小数点在第一个数字的后几位,第二个参数为返回字符串的位数,不足补0,去符号去小数点
	CString str=(LPCTSTR)buffer;
		temp=".";
	if(decimal>=0 && decimal<=16) str.Insert(decimal,temp);
	else if(decimal>16)
	{
		temp="0";
		for(int i=str.GetLength();i<decimal;i++) str+=temp;
		temp=".0";
		str+=temp;
	}
	else 
	{
		temp="0";
		for(int i=0;i<-decimal;i++)	str.Insert(0,temp);
		temp=".";
		str.Insert(0,temp);
	}
	temp="-";
	if(sign==1) str.Insert(0,temp);
	return str;
}
CString CCalculation::DeleteChar(CString str,int pos,int NUM)//删除从pos开始的NUM个字符
{
	int len=str.GetLength();
	CString str1;
	str1=="";
	for(int i=0;i<len;i++)
	{
		if((i>=pos)&&(i<NUM)); 
			
		else 	str1=str1+str.GetAt(i);
	}

	return str1;
		

}



double CCalculation::StoN(CString str)		//*字串转数字*
{
	char   *stopstring;
	double x;
    x = strtod(str.GetBuffer(0), &stopstring );
	m_strTmp=stopstring;
	return x;
}

CString CCalculation::TwoE(CString strExp)		//*二元运算*
{
	double x2,y,x1=StoN(strExp);
	strExp=m_strTmp;
	char op=strExp.GetAt(0);
	strExp.Delete(0);
	x2=StoN(strExp);
	if(m_strTmp!="") return "ERROR_"+m_strTmp+"未知符号_";
	if(op=='+') {y=x1+x2;}
	else if(op=='*') {y=x1*x2;}
	else if(op=='-') {y=x1-x2;}
	else if(op=='^') {y=pow(x1,x2);}
	else if(op=='/') {y=x1/x2;}
	else if(op=='%') {y=fmod(x1,x2);}
	else return "ERROR_"+m_strTmp+"未知运算符_";
	return NtoS(y);
}

CString CCalculation::SingleE(CString op,double dx)		//*一元运算*
{
	if(op=="ln") 
	{
		return NtoS(log(dx));
	}
	else if(op=="log") 
	{
		return NtoS(log10(dx));
	}
	else if(op=="sqr") 
	{
		return NtoS(sqrt(dx));
	}
	else if(op=="e^") 
	{
		return NtoS(exp(dx));
	}
	else if(op=="abs") 
	{
		return NtoS(fabs(dx));
	}
	else if(op=="ac") 
	{
		return NtoS(acos(dx));
	}
	else if(op=="as") 
	{
		return NtoS(asin(dx));
	}
	else if(op=="at") 
	{
		return NtoS(atan(dx));
	}

	if(m_bDegree) dx=dx*PI/180;
	if(op=="tan") 
	{
		return NtoS(tan(dx));
	}
	else if(op=="sin") 
	{	
		return NtoS(sin(dx));			
	}
	else if(op=="cos") 
	{
		return NtoS(cos(dx));		
	}
	else if(op=="cot") 
	{
		return NtoS(1/tan(dx));		
	}
	else if(op=="sh") 
	{
		return NtoS(sinh(dx));				
	}	
	else if(op=="ch") 
	{
		return NtoS(cosh(dx));
	}
	else if(op=="th") 
	{
		return NtoS(sinh(dx)/cosh(dx));
	}
	else if(op=="exp") 
	{
		return NtoS(pow(10,dx));
	}
	return "ERROR"+op+"_未知函数_";
}

int CCalculation::LocateLBra(CString strExp)			//*定位最后一个左括号*
{
	int len=strExp.GetLength();
	int pos=-1;
	for(int i=0;i<len;i++)
	{
		if(pos>=0 && strExp.GetAt(i)==')') break;
		if(strExp.GetAt(i)=='(')
		{
			pos=i;
		}
	}
	return pos;
}

void CCalculation::DelBracket(CString *strExp)		//*用计算结果替换表达式*
{
	int pos=LocateLBra(*strExp);
	int i,len=strExp->GetLength();
	CString temp;
	temp="ERROR_无效表达式_";
	if(pos==-1) {*strExp=temp;return;}
	for(i=pos;i<len;i++) if(strExp->GetAt(i)==')') break;
	CString str;
	str=strExp->Mid(pos+1,i-pos-1);
	MultiE(&str);
	strExp->Delete(pos,i-pos+1);
	strExp->Insert(pos,str);
}

void CCalculation::MultiE(CString *strExp)			//*多元运算*
{
	int i;
	CString temp,temp1;
	temp="ERROR_函数表达式为空_";
	if(strExp->IsEmpty()) *strExp=temp;
	if(IsDigital(*strExp)) {return;}
	while(1)/*处理所有的一元运算*/
	{
		for( i=0;i<FUNCNUM;i++)
		{
			int pos=strExp->Find(opt[i]);
			if(pos!=-1)
			{
				CString str=strExp->Right(strExp->GetLength()-pos-opt[i].GetLength());
				double dx=StoN(str);
				temp="ERROR";
				temp1="_无法识别的函数_";
				if(m_strTmp==str) {*strExp=temp+str+temp;return;}
				strExp->Delete(pos,strExp->GetLength()-pos);
				*strExp+=SingleE(opt[i],dx)+m_strTmp;
				MinusMinus(strExp);
				if(pos>=1)
				{
					char ch=strExp->GetAt(pos-1);
					if(ch>=48 && ch<=57)
						temp="ERROR_缺少二元运算符_";
					{*strExp=temp;return;}
				}
				break;
			}
		}
		if(i==FUNCNUM) break;
	}
	//按运算符优先级处理二元运算
	int pos=-1;
	while(1)
	{
		pos=strExp->Find('^');
		if(pos==-1) break;
		else Calcu(strExp,pos);
	}
	while(1)
	{
		pos=strExp->Find('/');
		if(pos==-1) break;
		else Calcu(strExp,pos);
	}
	while(1)
	{
		pos=strExp->Find('*');
		if(pos==-1) break;
		else Calcu(strExp,pos);
	}
	while(1)
	{
		pos=strExp->Find('%');
		if(pos==-1) break;
		else Calcu(strExp,pos);
	}
	pos=0;
	if(strExp->GetAt(0)=='-' ||strExp->GetAt(0)=='+') strExp->Insert(0,"0");
	while(1)
	{
		int tmpos=strExp->Right(strExp->GetLength()-pos).Find('-');
		if(tmpos==-1) break;
		else pos+=tmpos;
		if(pos==0 && strExp->GetAt(pos+1)=='-' || pos>0)
		{
			if(strExp->GetAt(pos+1)=='+')
			{
				strExp->Delete(pos+1);
				pos=0;
				continue;
			}
			/*********处理连减(如:----1)的情况***********/
			int nCount=0;
			while(1)
			{
				if(++pos>0)
				{
					if(strExp->GetAt(pos)!='-') break;
					else nCount++;
				}
				else break;
			}
			if(nCount>0) 
			{
				strExp->Delete(pos-nCount-1,nCount+1);
				if(nCount%2==0) strExp->Insert(pos-nCount-1,"-");
				else if(pos-nCount-1>0) strExp->Insert(pos-nCount-1,"+");
				pos=0;
				continue;
			}
			else pos--;
			/***********************************************/
			if(pos>0 && strExp->GetAt(pos-1)=='+') 
			{
				pos++;
				continue;
			}
			Calcu(strExp,pos);
		}
		else pos++;
	}
	pos=0;
	while(1)
	{
		pos=strExp->Find('+');
//		int tmpos=strExp->Right(strExp->GetLength()-pos).Find('+');
		if(pos==-1) break;
//		else pos+=tmpos;
		if(pos==0 && strExp->GetAt(pos+1)=='+' || pos>0)
		{
			/*********处理连加(如:++++1)的情况***********/
			int nCount=0;
			while(1)
			{
				if(++pos>0)
				{
					if(strExp->GetAt(pos)!='+') break;
					else nCount++;
				}
				else break;
			}
			if(nCount>0) 
			{
				strExp->Delete(pos-nCount-1,nCount+1);
				strExp->Insert(pos-nCount-1,"+");
				pos=0;
				continue;
			}
			else pos--;
			/***********************************************/
			Calcu(strExp,pos);
		}
		else pos++;
	}
}

void CCalculation::Calcu(CString *strExp,int pos)		//*二元运算的预处理函数*
{
	char ch;
	int j,k;
	CString temp;
	temp="ERROR_缺少参数_";
	for(j=pos-1;j>=0;j--)
	{
		ch=strExp->GetAt(j);
		if(ch=='+' ||ch=='-' ||ch=='*'||ch=='/' ||ch=='%' ||ch=='^' )
		{
			if(j==0 && ch!='-') {*strExp=temp;return;}
			if(j==0 && ch=='-') j=-1;//防止把负号误判为减号
			else if(j>0 && ch=='-' && !IsDigital(strExp->Mid(j-1,1))) j--;
			break;
		}
	}
	for( k=pos+1;k<strExp->GetLength();k++)
	{
		ch=strExp->GetAt(k);
		if(ch=='+' ||ch=='-' ||ch=='*'||ch=='/' ||ch=='%' ||ch=='^' )
		{
			if(ch=='-' && k>pos+1) break;
			if(ch=='-' && k>0 &&  IsDigital(strExp->Mid(k+1,1)) ) k++;
			else break;
		}
	}
	CString strExp2=strExp->Mid(j+1,k-j-1);
	*strExp=strExp->Left(j+1)+TwoE(strExp2)+strExp->Right(strExp->GetLength()-k);
	if(strExp->Find("#IN")!=-1) {*strExp="ERROR_结果有溢出或值域越界_";return;}
	if(!SynRes(strExp)) {*strExp="ERROR_缺少运算符_";return;}
	MinusMinus(strExp);
}

CString CCalculation::MainPro(CString strExp,double x)		//*主处理函数*
{
	if(strExp.IsEmpty()) return "表达式不能为空";
	Macro(&strExp);
	strExp.MakeLower(); //表达式全部小写
	//string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串 
	//	strExp=strExp.&erase(0,2);//
	//strExp=DeleteChar(strExp,0,2);
	/**********给表达式加上保护括号************/
	CString str1=NtoS(x);
	strExp.Replace("x",str1);
	strExp.Insert(0,"(");
	strExp+=")";
	/******************************************/
	int pos=strExp.Find(" ");
	int n=BraCheck(strExp);     //*计算左右括号的差值*
	CString str;
	str.Format("%d",abs(n));
	if(n==1) strExp+=")";
	else if(n==-1) strExp.Insert(0,"(");
	else if(n>0) return "缺少"+str+"个右括号";
	else if(n<0) return "缺少"+str+"个左括号";
	while(pos!=-1) //去掉表达式中的空格符
	{
		strExp.Delete(pos);
		pos=strExp.Find(" ");
	}

	while(!IsDigital(strExp))//*判断表达式中是否有函数或运算符*
	{
		DelBracket(&strExp);//*用计算结果替换表达式*
		if(!SynRes(&strExp)) return strExp;//*判断表达式是否合法*
	}
	if(!SynRes(&strExp)) return strExp;	//*判断表达式是否合法*
	else return ModiResult(strExp);//*在格式上处理最后的计算结果*

}

void CCalculation::Macro(CString *strExp)			//*常数宏代换*
{
	int pos;
	for(int i=0;i<CONSTNUM;i++)
	{
		pos=strExp->Find(m_strConName[i]);
		while(pos!=-1)
		{
			strExp->Delete(pos,m_strConName[i].GetLength());
			strExp->Insert(pos,m_strConValue[i]);
			if(pos>=1)
			{
				char ch=strExp->GetAt(pos-1);
				if(ch>=48 && ch<=57 || ch==41)
				{*strExp="缺少二元运算符";return;}
			}
			pos=strExp->Find(m_strConName[i]);
		}
	}
}

CString CCalculation::ModiResult(CString strRes)    	//*在格式上处理最后的计算结果*
{
	int i;
	if(strRes.Find("#IN")!=-1) return "结果有溢出或值域越界";
	/*****************去掉保护括号**********************/
	if(strRes.GetAt(0)=='(') strRes.Delete(0);
	if(strRes.GetAt(strRes.GetLength()-1)==')') strRes.Delete(strRes.GetLength()-1);
	/***************************************************/
	int pos=strRes.Find(".");CString str;
	str=="";
	if(pos!=-1)
	{
		if(pos==0) strRes="0"+strRes;
		else if(strRes.GetAt(0)=='-' && strRes.GetAt(1)=='.') strRes.Insert(1,"0");
	}
	if(pos>16)
	{
		strRes.Delete(pos);
		strRes.Insert(1,".");
		str.Format("%d",pos-1);
		str=" E"+str;
	}
	pos=strRes.Find(".");
	if(pos==0 || pos==1 && strRes.GetAt(0)=='0')
	{
		for(i=pos+1;i<strRes.GetLength();i++)
		{
			if(strRes.GetAt(i)!='0') break;
		}
		if(i>4)
		{
			str.Format(" E-%d",i-2);
			strRes.Delete(pos,i-1);
			strRes.Insert(1,".");
		}
	}
	strRes=strRes.Left(pos+16)+str;//截取小数点后16位
	return strRes;
}

bool CCalculation::SynRes(CString *strExp)			//*判断表达式是否合法*
{
	int pos=strExp->Find("ERROR");
	if(pos!=-1)
	{
		pos=strExp->ReverseFind('_');
		strExp->Delete(pos,strExp->GetLength()-pos);
		pos=strExp->ReverseFind('_');
		strExp->Delete(0,pos+1);
		return 0;
	}
	return 1;
}

void CCalculation::MinusMinus(CString *strExp)		//*处理负负得正*
{
	int pos=strExp->Find("--");
	if(pos!=-1)
	{
		strExp->Delete(pos,2);
		strExp->Insert(pos,"+");
		if(strExp->GetAt(0)=='+') strExp->Delete(0);
	}
}

int CCalculation::BraCheck(CString str)				//*计算左右括号的差值*
{
	int lb=0,rb=0,len=str.GetLength();
	for(int i=0;i<len;i++)
	{
		if(str.GetAt(i)=='(') lb++;
		else if(str.GetAt(i)==')') rb++;
	}
	return lb-rb;
}

⌨️ 快捷键说明

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