Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
8
Добавлен:
01.05.2014
Размер:
7.71 Кб
Скачать
#include "stdafx.h"
#include "Malgin_kurs.h"
#include "EnterFunction.h"

#include "math.h"

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


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

CEnterFunction::CEnterFunction()
{	
	m_strStandardFunction.Add("");
	m_strStandardFunction.Add("ABS");
	m_strStandardFunction.Add("SQR");
	m_strStandardFunction.Add("SINH");
	m_strStandardFunction.Add("COSH");
	m_strStandardFunction.Add("TANH");
	m_strStandardFunction.Add("ARCTAN");
	m_strStandardFunction.Add("LN");
	m_strStandardFunction.Add("LOG");
	m_strStandardFunction.Add("EXP");
	m_strStandardFunction.Add("SIN");
	m_strStandardFunction.Add("COS");
	m_strStandardFunction.Add("TAN");
	m_strStandardFunction.Add("ARCSIN");
	m_strStandardFunction.Add("ARCCOS");
	m_strStandardFunction.Add("INT");
}

CEnterFunction::~CEnterFunction()
{

}


double CEnterFunction::Snak(WORD& nPosition, CString& strCharacter)
{
  if (strCharacter == "-")
	{
	  Probel(nPosition, strCharacter);
    return (-1.0) * Factor(nPosition, strCharacter);
	}
  else return Factor(nPosition, strCharacter);
}

double CEnterFunction::Vichislenie(CString strFormula)
{
	WORD nPosition;
	CString strCharacter;
	double	a;

	m_strFunction = strFormula + Form_string(0);
	nPosition = 0;
	Probel(nPosition, strCharacter);
	a = PLUS_MINUS(nPosition, strCharacter);

	return a;
}

double CEnterFunction::Raschet(CString strFormula, WORD& ErrorPosition, CString& Errortext)
{
  WORD nPosition;
	CString strCharacter;
	double	a;
	char buffer[32];

	m_strErrortext.Empty();

	try
	{
		strFormula.TrimLeft();
		strFormula.TrimRight();

		m_strFunction = strFormula + Form_string(0);
		nPosition = 0;
		Probel(nPosition, strCharacter);
		a = PLUS_MINUS(nPosition, strCharacter);
		if (strCharacter == Form_string(0))
		{
			Errortext = m_strErrortext;
			ErrorPosition = 0;
		}
		else
		{
			ErrorPosition = nPosition;
			sprintf(buffer, "Найдена ошибка в %d-й позиции", (int) ErrorPosition);
			Errortext = buffer;
			Errortext += m_strErrortext;
			Errortext += "!";
		}

		return a;
	}
	catch (CException* ex)
	{
		TCHAR szCause[255];        
		ex->GetErrorMessage(szCause, 255);
		Errortext = _T("Ошибка в функции Raschet()");
		Errortext += szCause;        
		return 0;
	}
}

double CEnterFunction::PLUS_MINUS(WORD& nPosition, CString& strCharacter)
{
  CString strOperator;
  double erg = Delit_Umnogit(nPosition, strCharacter);
  while (strCharacter == "+" || strCharacter == "-")
  {
    strOperator = strCharacter;
    Probel(nPosition, strCharacter);
    if (strOperator == "+")
		  erg += Delit_Umnogit(nPosition, strCharacter);
		else if (strOperator == "-")
		  erg -= Delit_Umnogit(nPosition, strCharacter);
	}
  return erg;
}

double CEnterFunction::Delit_Umnogit(WORD& nPosition, CString& strCharacter)
{
  double s,dum;
	CString strOperator;
  s = Stepen(nPosition, strCharacter);
  while (strCharacter == "*" || strCharacter == "/")
	{
    strOperator = strCharacter;
    Probel(nPosition, strCharacter);
    if (strOperator == "*")
			s = s * Stepen(nPosition, strCharacter);
    else if (strOperator == "/") 
		{
			dum = Stepen(nPosition, strCharacter);
			if (dum != 0)  
				s = s / dum;
			else
				m_strErrortext = " (Деление на ноль)";
		}  
	}
  return s;
}

double CEnterFunction::Stepen(WORD& nPosition, CString& strCharacter)
{
  double t,vz;
  t = Snak(nPosition, strCharacter);
  while (strCharacter == "^")
  {
	  Probel(nPosition, strCharacter);
    vz = Snak(nPosition, strCharacter);
    
		if ((t <= 0 && fabs(vz) <= 1) || (t <= 0 && vz != int(vz)))
			m_strErrortext = " (Неверно введена формула)";
    else 
			t = pow(t,vz);		
	}
  return t;
}

double CEnterFunction::Probel(WORD& nPosition, CString& strCharacter)
{
  do
	{
    nPosition ++;	
		if (nPosition <= m_strFunction.GetLength())
			strCharacter = m_strFunction.Mid(nPosition - 1, 1);			
		else 
			strCharacter = Form_string(0);
 	}
  while (strCharacter == " "); 

	return nPosition;
}

void CEnterFunction::SetFormula(CString Formula)
{
	m_strFormula = Formula;
}

CString CEnterFunction::GetFormula()
{
	return m_strFormula;
}

double CEnterFunction::Factor(WORD& nPosition, CString& strCharacter)
{
  double f = 0.0;
	BOOL erfolg = false;
	WORD wI=0, wL=0, wBeginn=0, wError=0;
	wError = 0;

	if (strCharacter >= "0" && strCharacter <= "9" || strCharacter == ".")
	{
		wBeginn = nPosition;

		do
		{
			Probel(nPosition, strCharacter);
		} 
		while ((strCharacter >= "0" && strCharacter <= "9" || strCharacter == "."));
			
		if (strCharacter == ".")
		{
			do
			{
				Probel(nPosition, strCharacter);
			} 
			while (!((BYTE)strCharacter[0] >= (BYTE)"0") && ((BYTE)strCharacter[0] <=  (BYTE)"9")  || ((BYTE)strCharacter[0] == (BYTE)"."));
		}
		f = atof(m_strFunction.Mid(wBeginn - 1, nPosition - wBeginn ));
	} 
	else
	{
		CString strCharacterUpper = strCharacter;
		strCharacterUpper.MakeUpper();
		if (strCharacter == "(")
		{
			Probel(nPosition, strCharacter);
			f = PLUS_MINUS(nPosition, strCharacter);
			if (strCharacter == ")")
				Probel(nPosition, strCharacter);
		}
		else
		{
			erfolg = false;
			int AnzStdFunctions = m_strStandardFunction.GetSize() - 1;
			for (wI = 1; wI <= AnzStdFunctions; wI++)
			{
				if (!erfolg)
				{ 
					wL = m_strStandardFunction[wI].GetLength();
					CString strFunktionUpper = m_strFunction.Mid(nPosition - 1, wL);
					strFunktionUpper.MakeUpper();
					if (strFunktionUpper == m_strStandardFunction[wI])
					{
						nPosition = nPosition + wL - 1;
						Probel(nPosition, strCharacter);
						f = Factor(nPosition, strCharacter);
						if (strFunktionUpper == "ABS")
							f = fabs(f);
						else if (strFunktionUpper == "SQR")
							if (f >= 0)
								f = sqrt(f);
							else
								wError = -1;
						else if (strFunktionUpper == "SINH")
							f = sinh(f);
						else if (strFunktionUpper == "COSH")
							f = cosh(f);
						else if (strFunktionUpper == "TANH")
							f = tanh(f);
						else if (strFunktionUpper == "ARCTAN")
							f = atan(f);
						else if (strFunktionUpper == "LN")
						{
							if (f >= 0)
								f = log(f);
							else
								wError = -1;
						}
						else if (strFunktionUpper == "LOG")
						{
							if (f >= 0)
								f = log10(f);
							else
								wError = -1;
						}
						else if (strFunktionUpper == "EXP")
						{
							if (f <= 41)
								f = exp(f);
							else
								wError = -1;
						}
						else if (strFunktionUpper == "SIN")
							f = sin(f);
						else if (strFunktionUpper == "COS")
							f = cos(f);
						else if (strFunktionUpper == "TAN")
						{
							if (cos(f) != 0)
								f = tan(f);
							else 
								wError = -1;
						}
						else if (strFunktionUpper == "ARCSIN")
						{
							if (fabs(f) < 1) 
								f = asin(f);
							else
								wError = -1;
						}
						else if (strFunktionUpper == "ARCCOS")
						{
							if (fabs(f) <= 1)
								f = acos(f);
							else 
								wError = -1;
						}
						else if (strFunktionUpper == "INT") 
							f = int(f);
						erfolg = true;
					}
				}
			}
		}
	}
	
	if (wError == -1) 
		m_strErrortext = " (Найдена ошибка)";

  return f;
}


CString CEnterFunction::Form_string(WORD form_znak)
{
	CString string;
	string.Format("%c", form_znak);
	return string;
}
Соседние файлы в папке Malgin_kurs