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

📄 prefixcalculator.cpp

📁 穷举法进行三八二十四的计算。列举所有可能的表达式进行计算筛选。
💻 CPP
字号:
// PrefixCalculator.cpp: implementation of the CPrefixCalculator class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RepComb.h"
#include "PrefixCalculator.h"

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

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

CPrefixCalculator::CPrefixCalculator()
{
  m_nCur=-1;
  m_nFlagLength=m_nExpressionLength=0;
  m_pFlag=NULL;
}

CPrefixCalculator::~CPrefixCalculator()
{
  if(m_nFlagLength>0) delete[] m_pFlag;
  ClearStack();
}

void CPrefixCalculator::initiate(int nLength=-1)
{
   if(nLength<=m_nFlagLength) 
   {
	   m_nCur=-1;
	   ClearStack();
	   if(nLength>0)
	      m_nExpressionLength=nLength;
	   return;
   }
   delete[] m_pFlag;
   m_pFlag=new bool[nLength];
   m_nFlagLength=nLength;
   m_nCur=-1;
   m_nExpressionLength=nLength;
   ClearStack();
}

void CPrefixCalculator::push(char cOperator)
{
  CExpression *pEx=new CExpression(cOperator);
  CStack<CExpression*>::push(pEx);
  m_nCur++;
  m_pFlag[m_nCur]=true;
}

void CPrefixCalculator::push(double dblOperand)
{
  CExpression *pEx=new CExpression(dblOperand); 
  CStack<CExpression*>::push(pEx);
  m_nCur++;
  m_pFlag[m_nCur]=false;
  Calculate();
}

void CPrefixCalculator::ClearStack()
{
  CExpression *pEx;
  while(!empty())
  {
	  pEx=pop();
	  delete pEx;
  }
}

bool CPrefixCalculator::CanCalculate()
{
  bool bCanCalculate;
  if(m_nCur<2) bCanCalculate=false;
  else if(m_pFlag[m_nCur]==false&&m_pFlag[m_nCur-1]==false&&
	  m_pFlag[m_nCur-2]==true) bCanCalculate=true;
  else bCanCalculate=false;
  return bCanCalculate;
}

void CPrefixCalculator::Calculate()
{
  CExpression*  pExp1,*pExp2,*pExp3;
  while(CanCalculate())
  {
	  pExp1=pop();
	  pExp2=pop();
	  pExp3=pop();
	  pExp2->Calculate(*pExp3,*pExp1);
	  delete pExp1;
	  delete pExp3;
	  push(pExp2);
  }
}

CExpression* CPrefixCalculator::pop()
{
  CExpression* pEx=NULL;
  if(!empty())
  {
	  CStack<CExpression*>::pop(pEx);
	  m_nCur--;
  }
  return pEx;
}

void CPrefixCalculator::push(CExpression *pEx)
{
	CStack<CExpression*>::push(pEx);
	m_nCur++;
	m_pFlag[m_nCur]=false;
}

double CPrefixCalculator::Calculate(int PreScheme[], char OperatorTable[], double OperandTable[],CString& strMidExpression)
{
	CExpression* pEx;
	double dblResult;
	if(m_nExpressionLength<=0) {m_nState=-1;return 0;}
	int nIndex=0;
	double d;
    char c;
	while(PreScheme[nIndex])
	{
		if(PreScheme[nIndex]>0)
		{
			d=OperandTable[PreScheme[nIndex]-1];
			push(OperandTable[PreScheme[nIndex]-1]);
		}
		else
		{
			c=OperatorTable[-PreScheme[nIndex]-1];
			push(OperatorTable[-PreScheme[nIndex]-1]);
		}
		nIndex++;
	}
	if(empty()){m_nState=-2;return 0;}
	pEx=pop();
	if(!empty()){ClearStack();m_nState=-3;return 0;}
	strMidExpression=pEx->m_strExpression;
    dblResult=pEx->m_dblValue;
    delete pEx;
	return dblResult;
}

⌨️ 快捷键说明

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