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

📄 calcformule.cpp

📁 我的简易编译器终于在花了近20个工作日后完成了。按照设计是做成一个FormulaEx.dll
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	case FUNC_IF:
		{// 108
		//	return CString("error of if");
			int nS = slOperand.GetCount();
			if (nS < 2) RETURNEMPTY;
			p = slOperand.GetHeadPosition();
			CString str1(slOperand.GetNext(p));
			str = slOperand.GetNext(p);
			if(nS>2) tempstr = slOperand.GetNext(p);
			else tempstr="";
			if(str1.CompareNoCase("true")==0) return str;
			if(CRegularOpt::IsNumber(str1) && (atoi(str1) != 0))
				 return str;
			return tempstr;
/*			*/
		}
/*	case FUNC_PRECISION:{
			int nS = slOperand.GetCount();
			if (nS < 1) RETURNEMPTY;
			int nP = -1;
			p = slOperand.GetHeadPosition();
			CString str1(slOperand.GetNext(p));
			if (nS > 1){
				nP = atoi(slOperand.GetNext(p));
			}
			return CRegularOpt::PrecisionNum(str1,nP);
						}
*/
	case FUNC_CASE:// 116
		{
			p = slOperand.GetHeadPosition();
			if(p==NULL) RETURNEMPTY;

			tempstr = slOperand.GetNext(p);
			int MatchType = 0;
			if(tempstr.CompareNoCase("true")==0)
				MatchType = 1;
			else
				if(CRegularOpt::IsNumber(tempstr)){
					tempstr = CRegularOpt::PrecisionNum(tempstr);
					MatchType = 2;
				}
			str = tempstr;
			CString str2;
			while(p != NULL){
				str2 = slOperand.GetNext(p);

				if(p == NULL)
					str = str2;
				else{
					bool bMatch=false;
					if(MatchType == 0)
						bMatch = tempstr == str2;
					else if(MatchType == 1)
						bMatch = CRegularOpt::IsTrue(str2);
					else //  MatchType == 2
						if(CRegularOpt::IsNumber(str2))
							bMatch = tempstr == CRegularOpt::PrecisionNum(str2);	

					str = slOperand.GetNext(p);
					if(bMatch)
						break;
				}
		
			}
			return str;
		}
	case FUNC_TODAY:
		{
			if(slOperand.IsEmpty())
				return CTime::GetCurrentTime().Format("%Y-%m-%d");
			else{
				CString & str1 = slOperand.GetHead();
				if(str1.IsEmpty()){
					return CTime::GetCurrentTime().Format("%Y-%m-%d");
				}
				if(str1 == "C"){
					return CTime::GetCurrentTime().Format("%Y年%m月%d日");
				}else{
					return CTime::GetCurrentTime().Format(str1);
				}
			}
		}	
	case FUNC_DAY:
		{
			if(slOperand.IsEmpty()){
				return CTime::GetCurrentTime().Format("%d");
			}else{
				CTime t;
				POSITION pPos = slOperand.GetHeadPosition();
				CString & str1 = slOperand.GetNext(pPos);
				if(str1.IsEmpty()){
					return CTime::GetCurrentTime().Format("%d");
				}
				CString str2 = CRegularOpt::TrimDateString(str1);
				if( CRegularOpt::IsDate(str2,t)){
					str = t.Format("%d");
					if(pPos != NULL){
						str1 = slOperand.GetNext(pPos);
						if(! str1.IsEmpty()){
							str = t.Format(str1);
						}
					}
				} else {
					if( CRegularOpt::IsNumber(str1) ){
						int nD =  atoi(str1) ;
						t = CTime(2005,12,4,0,0,0,-1) + CTimeSpan(nD,0,0,0);
						str = t.Format("%A");
					}else {
						str =  CTime::GetCurrentTime().Format(str1);
					}
				}
			}
			return str;
		}
	case FUNC_MONTH:
		{
			if(slOperand.IsEmpty()){
				return CTime::GetCurrentTime().Format("%m");
			}else{
				CTime t;
				POSITION pPos = slOperand.GetHeadPosition();
				CString & str1 = slOperand.GetNext(pPos);
				if(str1.IsEmpty()){
					return CTime::GetCurrentTime().Format("%m");
				}
				CString str2 = CRegularOpt::TrimDateString(str1);
				if( CRegularOpt::IsDate(str2,t)){
					str = t.Format("%m");
					if(pPos != NULL){
						str1 = slOperand.GetNext(pPos);
						if(! str1.IsEmpty()){
							str = t.Format(str1);
						}
					}
				} else {
					if( CRegularOpt::IsNumber(str1) ){
						int nM =  atoi(str1) ;
						t = CTime(2005,12,15,0,0,0,-1) + CTimeSpan(nM*30,0,0,0);
						str = t.Format("%B");
					}else {
						str =  CTime::GetCurrentTime().Format(str1);
					}
				}
			}
			return str;
		}		
	case FUNC_YEAR:
		{
			int nY; 
			if(slOperand.IsEmpty())
				nY = CTime::GetCurrentTime().GetYear();
			else{
				CTime t;
				CString & str1 = slOperand.GetHead();
				str1 = CRegularOpt::TrimDateString(str1);
				if( CRegularOpt::IsDate(str1,t))
					nY = t.GetYear();
				else
					nY = CTime::GetCurrentTime().GetYear();
			}
			str.Format("%d",nY);
			return str;
		}
	case FUNC_DAY_SPAN:
		{
			int nS = slOperand.GetCount();
			p = slOperand.GetHeadPosition();
			CString str1(slOperand.GetNext(p));
			CTime t;
		    str1 = CRegularOpt::TrimDateString(str1);
			if(! CRegularOpt::IsDate(str1,t))
				RETURNEMPTY;

			CTime t2 = CTime::GetCurrentTime();
			if( nS > 1){
				str1 = slOperand.GetNext(p);
			    str1 = CRegularOpt::TrimDateString(str1);
				if(! CRegularOpt::IsDate(str1,t2))
					t2 =  CTime::GetCurrentTime(); 
			}
			float fDays = (t-t2).GetDays() + (t.GetHour() - t2.GetHour()) / 24.f ;
			str.Format("%f",fDays);
			return str;
		}
	case FUNC_MONTH_SPAN:
		{
			int nS = slOperand.GetCount();
			p = slOperand.GetHeadPosition();
			CString str1(slOperand.GetNext(p));
			CTime t;
		    str1 = CRegularOpt::TrimDateString(str1);
			if(! CRegularOpt::IsDate(str1,t))
				RETURNEMPTY;

			CTime t2 = CTime::GetCurrentTime();
			if( nS > 1){
				str1 = slOperand.GetNext(p);
			    str1 = CRegularOpt::TrimDateString(str1);
				if(! CRegularOpt::IsDate(str1,t2))
					t2 =  CTime::GetCurrentTime(); 
			}
			float fMonths = (t.GetYear() - t2.GetYear()) * 12 + t.GetMonth() - t2.GetMonth()+
							(t.GetDay() - t2.GetDay()) / 30.f ;
			str.Format("%f",fMonths);
			return str;
		}
	case FUNC_YEAR_SPAN:
		{
			int nS = slOperand.GetCount();
			p = slOperand.GetHeadPosition();
			CString str1(slOperand.GetNext(p));
			CTime t;
		    str1 = CRegularOpt::TrimDateString(str1);
			if(! CRegularOpt::IsDate(str1,t))
				RETURNEMPTY;

			CTime t2 = CTime::GetCurrentTime();
			if( nS > 1){
				str1 = slOperand.GetNext(p);
			    str1 = CRegularOpt::TrimDateString(str1);
				if(! CRegularOpt::IsDate(str1,t2))
					t2 =  CTime::GetCurrentTime(); 
			}
			float fYears = t.GetYear() - t2.GetYear() + (t.GetMonth() - t2.GetMonth()) / 12.f +
							(t.GetDay() - t2.GetDay()) / 365.f ;
			str.Format("%f",fYears);
			return str;
		}
	case FUNC_GET_STR:
		{
			CString str1(slOperand.GetHead());
			str1 = CRegularOpt::TrimString(str1);
			for(int i=0; i<m_uiSysStrSum;i++)
				if(str1.Compare(m_sSysStrList[i].sName)==0)
					return CString(m_sSysStrList[i].sValue);
			RETURNEMPTY;
		}
	case FUNC_GET_PY:
		{
			CString str1(slOperand.GetHead());
			str1 = CRegularOpt::TrimString(str1);
			return 	 CCharCode::GetPYABIndex(str1);
		}
	}// end of case

	RETURNEMPTY;
}

CString CCalcFormula::Formula()
{
	CStringList  slOperand;
	COptStack    optStack;
	while(true){
		CString str = Item();
		slOperand.AddHead(str);
		if( str.IsEmpty()) 
			break;
		
		str = m_lpGetWord->getAWord();
		int optID = GetOptID(str);
		if( optID == -1){
			m_lpGetWord->SetPreword(str);
			break;
		}
//------------------------------------------
		if(optID == OP_IN){ // Specail Opt For In multi operand
			CString sInRes("0"); 
			CString str1Operand = CRegularOpt::TrimString(slOperand.RemoveHead());
			str = m_lpGetWord->getAWord();
			if( str.IsEmpty() || str[0] != '(') RETURNEMPTY;
			CStringList  slInnerOperand;
			while(1)
			{
				str = Formula();
				slInnerOperand.AddTail(str);
				str = m_lpGetWord->getAWord();
				if( str.IsEmpty() || ( str[0] != ',' && str[0] != ')') ) RETURNEMPTY;
				if( str[0] == ')'){
					//m_lpGetWord->SetPreword(str);
					break;
				}
			}
			POSITION p = slInnerOperand.GetHeadPosition();
			while(p != NULL){
				str = CRegularOpt::TrimString(slOperand.GetNext(p));
				if (str1Operand == str){
					sInRes ="1";
					break;
				}
			}

			slOperand.AddHead(sInRes);
			str = m_lpGetWord->getAWord();
			optID = GetOptID(str);
			if( optID == -1){
				m_lpGetWord->SetPreword(str);
				break;
			}
		}
//------------------------------------------

		for(int op = optStack.PushOpt(optID); op != 0; op = optStack.PushOpt(optID)){
			CString operand2 = slOperand.RemoveHead();
			CString operand = slOperand.RemoveHead();
			str = Operate(operand,operand2,op);
			slOperand.AddHead(str);
		}
	}
	for(int op = optStack.PopOpt(); op != 0; op = optStack.PopOpt()){
		CString operand2 = slOperand.RemoveHead();
		CString operand = slOperand.RemoveHead();
		CString str = Operate(operand,operand2,op);
		slOperand.AddHead(str);
	}
	return  slOperand.GetHead();
}

CString CCalcFormula::Calculate(LPCTSTR szExpress)
{
	CSZGetWord * lpGW  = new CSZGetWord(szExpress);
	m_lpGetWord = lpGW;
	CString sRes = Formula();
	delete lpGW;
	if(CRegularOpt::IsNumber(sRes))
		sRes = CRegularOpt::PrecisionNum(sRes);
	else
	{
		int sl = sRes.GetLength();
		if(sl>=2 && (( sRes[0]=='\"' && sRes[sl-1]=='\"')||( sRes[0]=='\'' && sRes[sl-1]=='\''))){
			if(sl==2) 
				sRes = "";
			else
				sRes = sRes.Mid(1,sl-2);
		}
	}
	if(sRes.IsEmpty()) return CString("0");
	return sRes;
}

CString CCalcFormula::Calculate(LPCTSTR szExpress,LPCTSTR szParams)
{
 	CSZGetWordWithPrm * lpGW  = new CSZGetWordWithPrm(szExpress,szParams);
	m_lpGetWord = lpGW;
	CString sRes = Formula();
	delete lpGW;
	if(CRegularOpt::IsNumber(sRes))
		sRes = CRegularOpt::PrecisionNum(sRes);
	else
	{
		int sl = sRes.GetLength();
		if(sl>=2 && (( sRes[0]=='\"' && sRes[sl-1]=='\"')||( sRes[0]=='\'' && sRes[sl-1]=='\''))){
			if(sl==2) 
				sRes = "";
			else
				sRes = sRes.Mid(1,sl-2);
		}
	}
	if(sRes.IsEmpty()) return CString("0");
	return sRes;
}


CString CCalcFormula::Calculate(CFormulaGetWord * lpGW)
{
	m_lpGetWord = lpGW;
	CString sRes =  Formula();
	if(CRegularOpt::IsNumber(sRes))
		sRes = CRegularOpt::PrecisionNum(sRes);
	else
	{
		int sl = sRes.GetLength();
		if(sl>=2 && (( sRes[0]=='\"' && sRes[sl-1]=='\"')||( sRes[0]=='\'' && sRes[sl-1]=='\''))){
			if(sl==2) 
				sRes = "";
			else
				sRes = sRes.Mid(1,sl-2);
		}
	}
	if(sRes.IsEmpty()) return CString("0");
	return sRes;
}

int CCalcFormula::CheckFormulaEx(LPCTSTR szExpress)
{
	int nPos;
	CSZGetWord * lpGW  = new CSZGetWord(szExpress);
	m_lpGetWord = lpGW;
	Formula();
	if(lpGW->IsEnd())
		nPos = 0;
	else 
		nPos = lpGW->GetCurPos();
	delete lpGW;
	return nPos;
}


BOOL  CCalcFormula::IsKeyWord(LPCTSTR sWord)
{
	CString strWord(sWord);
	if ((sWord[0] == ',') || (sWord[0] == '(') || (sWord[0] == ')'))
			return TRUE;
	if ((sWord[0] =='-') && (strlen(sWord)>1)) return FALSE;
	return GetOptID(sWord)>0;
}

int CCalcFormula::CheckFormula(LPCTSTR szExpress)
{
	BOOL bRes = TRUE;
	int nNextType = 1;
	int nBrackets = 0;
	int nErrorPos;
	CSZGetWord * lpGW  = new CSZGetWord(szExpress);
	m_lpGetWord = lpGW;
	CString sWord;
	sWord =	m_lpGetWord->getAWord();
	while(sWord!=""){
		BOOL bKW = IsKeyWord(sWord);
		if(nNextType == 1){
			if(bKW){
				if (sWord == "(")
					nBrackets ++;
				//else if (sWord == ")")
				//	nBrackets --;
				else if ((sWord.CompareNoCase("NOT")!=0) && (sWord!="!"))
					goto errorexit;
			}else nNextType = 0;
		}else{
			if(bKW){
				if (sWord == ")")
					nBrackets --;
				else if (sWord == "("){
					nBrackets ++;
					nNextType = 1;
				}
				else nNextType = 1;
			}else goto errorexit;
		}
		if(nBrackets<0)
			goto errorexit;
		sWord =	m_lpGetWord->getAWord();
	}	
	if(nBrackets ==0)
		return 0;
errorexit:
	nErrorPos = lpGW->GetCurPos()-sWord.GetLength();
	if (nErrorPos==0) nErrorPos = 1;
	delete lpGW;
	return nErrorPos;
}

⌨️ 快捷键说明

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