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

📄 regularopt.cpp

📁 我的简易编译器终于在花了近20个工作日后完成了。按照设计是做成一个FormulaEx.dll
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	int sl = strlen(szDateStr);
 
	CString sTmp2("");
	bool bDot = false;
	for( int j=0; j< sl; j++){
		if (szDateStr[j] >= '0' && szDateStr[j] <= '9'){
			if (bDot){
				sTmp2 = sTmp2 + '-';
				bDot = false;
			}
			sTmp2 = sTmp2 + szDateStr[j];
		}else if(sTmp2!="")
			bDot = true;
	}
	return sTmp2;
}

bool CRegularOpt::IsDate(LPCTSTR szTime ,CTime & t_time)
{
	int sl = strlen(szTime);
	int sp=0;
	int s=0;
	char c[5];
	int y,m,d;
	while(sp<sl && (szTime[sp]==' ' ||  szTime[sp]== 9)) sp++;
	while(sp<sl ){
		if(szTime[sp]>='0' &&  szTime[sp]<= '9'){
			if(s<4) c[s] = szTime[sp];
			sp++;s++;
			continue;
		}
		//if(szTime[sp] == '-') 
			sp++; 
		break;
	}//check year
	c[4]=0;
	y = atoi(c);
	if(y < 1970 || y>2038) return false;
	s=0;
	while(sp<sl ){
		if(szTime[sp]>='0' &&  szTime[sp]<= '9'){
			if(s<2) c[s] = szTime[sp];
			sp++;s++;
			continue;
		}
		//if(szTime[sp] == '-') 
		sp++; 
		break;
	}//check month
	if(s > 2) return false;
	c[s]=0;
	m = atoi(c);
	if(m<1 || m >12) return false;
	s=0;
	while(sp<sl ){
		if(szTime[sp]>='0' &&  szTime[sp]<= '9'){
			if(s<2) c[s] = szTime[sp];
			sp++;s++;
			continue;
		}
		break;
	}//check day
	if(s > 2) return false;
	c[s]=0;
	d = atoi(c);
	if(d<1 || d >31) return false;
	t_time = CTime(y,m,d,0,0,0,-1);	
	return true;
}


bool CRegularOpt::IsTime(LPCTSTR szTime ,CTime & t_time)
{
	int sl = strlen(szTime);
	int sp=0;
	int s;
	char c[5];
	int h,min,sec;
	while(sp<sl && (szTime[sp]==' ' ||  szTime[sp]== 9)) sp++;

	s=0;
	while(sp<sl ){
		if(szTime[sp]>='0' &&  szTime[sp]<= '9'){
			if(s<2) c[s] = szTime[sp];
			sp++;s++;
			continue;
		}
		//if(szTime[sp] == ':') 
			sp++; 
		break;
	}//check hour
	if(s > 2) return false;
	c[s]=0;
	h = atoi(c);
	if(h<0 || h >24) return false;
	s=0;
	while(sp<sl ){
		if(szTime[sp]>='0' &&  szTime[sp]<= '9'){
			if(s<2) c[s] = szTime[sp];
			sp++;s++;
			continue;
		}
		//if(szTime[sp] == ':') 
			sp++; 
		break;
	}//check minute
	if(s > 2) return false;
	c[s]=0;
	min = atoi(c);
	if(min<0 || min >60) return false;
	s=0;
	while(sp<sl ){
		if(szTime[sp]>='0' &&  szTime[sp]<= '9'){
			if(s<2) c[s] = szTime[sp];
			sp++;s++;
			continue;
		}
		break;
	}//check second
	if(s > 2) return false;
	c[s]=0;
	sec = atoi(c);
	if(sec<0 || sec >60) return false;
	t_time = CTime(2002,7,24,h,min,sec,-1);	
	return true;
}

int	 CRegularOpt::SplitFloatList(LPCTSTR szStr , CArray<float,float> & arfData, char splitChar)
{
	int nSL = strlen(szStr);
	if (nSL<1) return 0;
	int nRes = 0;

	CString sWord;
	int nPos = 0;
	if(splitChar == ' '){
		while(nPos < nSL){
			while((nPos < nSL) && ((szStr[nPos]==' ') || (szStr[nPos]==9) || (szStr[nPos]==13) || (szStr[nPos]==10) )) nPos++;
			sWord="";
			while((nPos < nSL) && (szStr[nPos]!=' ') && (szStr[nPos]!=9) && (szStr[nPos]!=13) && (szStr[nPos]!=10) ) {
				sWord += szStr[nPos];
				nPos ++;
			}
			if(sWord != ""){
				nRes ++;
				arfData.Add(float(atof(sWord)));
			}
		}
	}else{
		while(nPos < nSL){
			sWord="";
			while((nPos < nSL) && (szStr[nPos]!=splitChar)) {
				if((szStr[nPos]!=' ') && (szStr[nPos]!=9) && (szStr[nPos]!=13) && (szStr[nPos]!=10) )
					sWord += szStr[nPos];
				nPos ++;
			}
			if(sWord != ""){
				nRes ++;
				arfData.Add(float(atof(sWord)));
			}
			nPos++;
		}
	}
	return nRes;
}

int	 CRegularOpt::SplitString(LPCTSTR szStr , CStringList & ssRes,char splitChar)
{
	int nSL = strlen(szStr);
	if (nSL<1) return 0;
	int nRes = 0;

	CString sWord;
	int nPos = 0;
	if(splitChar == ' '){
		while(nPos < nSL){
			while((nPos < nSL) && ((szStr[nPos]==' ') || (szStr[nPos]==9))) nPos++;
			sWord="";
			while((nPos < nSL) && (szStr[nPos]!=' ') && (szStr[nPos]!=9)) {
				sWord += szStr[nPos];
				nPos ++;
			}
			if(sWord != ""){
				nRes ++;
				ssRes.AddTail(sWord);
			}
		}
	}else{
		while(nPos < nSL){
			sWord="";
			while((nPos < nSL) && (szStr[nPos]!=splitChar)) {
				sWord += szStr[nPos];
				nPos ++;
			}
			if(sWord != ""){
				nRes ++;
				ssRes.AddTail(sWord);
			}
			nPos++;
		}
	}
	return nRes;
}

CString	CRegularOpt::SplitNum(LPCTSTR szNum ,char splittertype)
{
	CString resNum(szNum),tempstr(szNum);
	tempstr.TrimLeft();
	int sl = tempstr.GetLength();
	if(sl<3) return resNum;
	int dp = tempstr.Find('.');
	if( dp == -1) dp = sl;
	int sp,step=0;
	switch (splittertype){
	case '3':step=3;	break;
	case '4':step=4;  break;
	}
	if(step !=0 ){
		if(dp>0){
			if(tempstr[0]=='-'){
				sp = (dp-1)%step;
				if(dp>1 && sp==0) sp=step;
				sp++;
				resNum = tempstr.Mid(0,sp);
			}else{
				sp = dp%step;
				if(sp==0) sp=step;
				resNum = tempstr.Mid(0,sp);
			}
			for(;sp<dp;sp+=step){
				resNum += ","+tempstr.Mid(sp,step);
			}
			sp++;
		}else{
			sp = 1;
		}
		if(sp<sl)
			resNum += '.';
		for(;sp<sl;sp+=3){
			resNum += tempstr.Mid(sp,step);
			if(sp+3 < sl) resNum += ',';
		}
	}
	return resNum;
}
//大写 负 零壹贰叁肆伍陆柒捌玖
const	CString CNum[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
const	CString CNum2[]={"〇","一","二","三","四","五","六","七","八","九"};
const	CString CBit[]={"","拾","佰","仟"};
      //拾佰仟万拾佰仟亿拾佰仟萬
CString	CRegularOpt::Capitalization(LPCTSTR szNum,const bool isSimple)
{
	CString resstr("");
	CString tmpstr(szNum);
	tmpstr.TrimLeft();
	int sl = tmpstr.GetLength();
	int sp=0;
	if (isSimple){
		if(sl<1) return CNum2[0];
		for(;sp<sl;sp++)
			if(tmpstr[sp]>='0' && tmpstr[sp]<='9')
				resstr += CNum2[tmpstr[sp]-'0'];
			else
				resstr += tmpstr[sp];
		return resstr;
	}
	int dotpos = tmpstr.Find('.');
	if(dotpos != -1){
		while(sl>1 && tmpstr[sl-1] == '0') sl--;
		if(tmpstr[sl-1]=='.') sl--;
		if(sl != tmpstr.GetLength()){
			tmpstr = tmpstr.Mid(0,sl);
		}
	}else dotpos = sl;
	if(sl<1) return CString("零");
	if(tmpstr[0] == '-'){
		resstr="负";
		sp = 1;
	}
	CString integerNum = tmpstr.Mid(sp,dotpos-sp);
	CString decimalNum("");
	if(dotpos+1<sl) decimalNum = tmpstr.Mid(dotpos+1);
	sl = integerNum.GetLength();
	sp=0; while(sp<sl && integerNum[sp]=='0') sp++;
	if(sp > 0) integerNum = integerNum.Mid(sp);
	int inl = integerNum.GetLength();
	if(inl>0){
		int h = (inl-1) % 4 ;
		int j = (inl-1) / 4 + 1;
		sp=0;
		bool allzero = false;
		bool preallzero = false;
		for(;j>0;j--){
			int k=h;
			h = 3;
			bool preiszero = allzero;
			allzero = true;
			for(;k>=0;k--,sp++){
				if(integerNum[sp] == '0')
					preiszero = true;
				else{
					allzero = false;
					if(preiszero)
						resstr+="零";
					preiszero = false;
					resstr+=CNum[int(integerNum[sp])-48] + CBit[k];
				}
			}// end for k
			if(j!=0 && j % 2 == 0 ){
				if(!allzero) 
					resstr+="万";
			}
			else
			{
				if(!allzero || !preallzero){ 
					int repyi = j/2;
					for(int i=0; i<repyi; i++)
						resstr+="亿";
				}
			}
			preallzero = allzero;
		}//end for j
	}else
		resstr+="零";
	
	int dnl = decimalNum.GetLength();
	if(dnl>0){
		resstr+="点";
		for(int i=0; i<dnl; i++){
			resstr+=CNum[int(decimalNum[i])-48];
		}
	}
	return resstr;
}
CString	CRegularOpt::RightMoveDot(LPCTSTR szNum,int bits)
{
	CString str(szNum);
	int sp = str.Find('.');
	int sl = str.GetLength();
	if(sp == -1){
		if(sl == 0) return CString("");
		for(int i=0;i<bits; i++)
			str +='0';
		return str;
	}
	CString tempstr("");
	if(sp>0) tempstr = str.Mid(0,sp);
	str = str.Mid(sp+1);
	sl = str.GetLength();
	if(sl > bits){
		tempstr += str.Mid(0,bits);
		tempstr += '.';
		tempstr +=  str.Mid(bits);
	}else{
		tempstr +=  str;
		for(int i=0; i<bits-sl;i++)
			tempstr+='0';
	}
	return tempstr;
}

CString	CRegularOpt::CheckNumber(LPCTSTR szNum)
{
	int sl = strlen(szNum);
	char  * c = new char[sl+1];
	int nPos=0; bool bNeedPot = true;
	for(int i=0;i<sl;i++){
		if( szNum[i]>='0' && szNum[i]<='9' )
			c[nPos++] = szNum[i];
		if( bNeedPot && szNum[i]=='.'){
			c[nPos++] = '.';
			bNeedPot = false;
		}
	}
	c[nPos] = '\0';
	for(i=0;i<nPos;i++)
		if(c[i] != '0') break;
	
	CString sRes(c+i);
	if(c[i]=='.') 
		sRes ='0'+sRes;
	if(nPos>0 && c[nPos-1]=='.')
		sRes+='0';
	if(sRes.IsEmpty()) sRes="0";

	delete [] c;
	return sRes;
}

CString	CRegularOpt::PrecisionNum(LPCTSTR szNum,int precision)
{
	CString tmpstr(szNum);
	tmpstr.TrimLeft();
	int sl = tmpstr.GetLength();
	int dotpos = tmpstr.Find('.');
	int sp=0;
	int sign = 1;
	if(dotpos == -1) dotpos = sl;
	if(sl<1) return CString("0");
	if(tmpstr[0] == '-'){
		sp = 1;
		sign = -1;
	}
	CString integerNum = tmpstr.Mid(sp,dotpos-sp);
	CString decimalNum("");
	if(dotpos+1<sl) 
		decimalNum = tmpstr.Mid(dotpos+1);
	dotpos = integerNum.GetLength();
	sp=0; 
	while(sp<dotpos && integerNum[sp]=='0') 
		sp++;
	if(sp > 0) 
		integerNum = integerNum.Mid(sp);
	sl =  decimalNum.GetLength();
	int osl = sl;
	while(sl>0 && decimalNum[sl-1] == '0') sl--;
	if(sl == 0) decimalNum = "";
	else if(sl < osl) decimalNum =  decimalNum.Mid(0,sl);
	CString resstr;
	if(precision > -1){//if(precision != -1){
		if(sl <= precision){
			for(;sl<precision;sl++)
				decimalNum+='0';
		}else{
			sl = precision;
			int isIn = (decimalNum[sl]>='5')?1:0;
			if(sl>0)
				decimalNum = decimalNum.Mid(0,sl);
			else 
				decimalNum = "";
			sp = sl;
			while(isIn && sp>0) {
				if(decimalNum[sp-1] == '9') decimalNum.SetAt(sp-1,'0');
				else {
					 decimalNum.SetAt(sp-1,decimalNum[sp-1]  + 1);
					 isIn = 0;
				}
				sp--;
			}
			sp = integerNum.GetLength();
			while(isIn && sp>0){
				if(integerNum[sp-1] == '9') integerNum.SetAt(sp-1,'0');
				else {
					 integerNum.SetAt(sp-1,integerNum[sp-1]  + 1);
					 isIn = 0;
				}
				sp--;
			}
			if(isIn) integerNum = "1"+integerNum;
		}
	}
	if(integerNum.IsEmpty()) integerNum="0";
	if( sign == -1){
		if(sl > 0)
			resstr.Format("-%s.%s",integerNum,decimalNum);
		else
			resstr.Format("-%s",integerNum);
	}else{
		if(sl > 0)
			resstr.Format("%s.%s",integerNum,decimalNum);
		else
			resstr.Format("%s",integerNum);
	}
	if(resstr.IsEmpty()) return CString("0");
	return resstr;
}

CString CRegularOpt::GetAWord(LPCTSTR strSource,int & startPos ,char splitterChar)
{
	int sl = strlen(strSource);
	while((startPos < sl ) &&(strSource[startPos] == ' ' || strSource[startPos] == 9 || strSource[startPos] == 10|| strSource[startPos] == 13)) startPos++;
	int bp = startPos;
	while ( startPos < sl  && strSource[startPos] != splitterChar && strSource[startPos] != ' ' && strSource[startPos] != 9 ) startPos ++;
	int cl = startPos-bp+1 ;
	if(strSource[startPos] == splitterChar) startPos++;
	if(cl < 2) return  CString("");
	char * lpc = new char[cl];
	CopyMemory(lpc,strSource+bp,cl-1);
	lpc[cl-1]= 0;
	CString str(lpc);
	delete [] lpc;
	return str;	
}

CString CRegularOpt::GetARegularWord(LPCTSTR strSource,int & startPos,int sl,bool & bAcceptOpt)
{
	while((startPos < sl ) &&(strSource[startPos] == ' ' || strSource[startPos] == 9 || strSource[startPos] == 10|| strSource[startPos] == 13)) startPos++;
	int bp = startPos;
	if(startPos >= sl) return CString("");
//	int endex=1;
/*	if((strSource[startPos]>='0' && strSource[startPos]<='9') || strSource[startPos]== '.' 
		||( !bAcceptOpt && (strSource[startPos]== '-'||strSource[startPos]== '+' ) )){
		startPos++;
		while ( startPos < sl  && (
				( strSource[startPos]>='0' && strSource[startPos]<='9') ||
				( strSource[startPos]>='a' && strSource[startPos]<='z') ||
				( strSource[startPos]>='A' && strSource[startPos]<='Z') ||
				  strSource[startPos]=='.' )
				  ) startPos ++;
		bAcceptOpt = true;
	}else*/
	if( (strSource[startPos]>='0' && strSource[startPos]<='9') || 
		strSource[startPos]== '.' ||
		( !bAcceptOpt && (strSource[startPos]== '-' || strSource[startPos]== '+' ) ) ||
		( strSource[startPos]>='a' && strSource[startPos]<='z') ||
		( strSource[startPos]>='A' && strSource[startPos]<='Z') ||
		strSource[startPos]=='_' ||
		strSource[startPos]=='@'  ){
		startPos++;
		while ( startPos < sl  && (
				( strSource[startPos]>='0' && strSource[startPos]<='9') ||
				( strSource[startPos]>='a' && strSource[startPos]<='z') ||
				( strSource[startPos]>='A' && strSource[startPos]<='Z') ||
				  strSource[startPos]=='_' ||  strSource[startPos]=='.' ||
				  strSource[startPos]=='@' ) ) 
			startPos ++;
		bAcceptOpt = true;
	}else {
		bAcceptOpt = false;
		switch(strSource[startPos]){
		case '<':
			++startPos;
			if((startPos<sl) && ((strSource[startPos] == '=') || 
								 (strSource[startPos] == '>') ||
								 (strSource[startPos] == '<')   ) ) startPos ++;
			break;	
		case '>':
			++startPos;
			if((startPos<sl) && ((strSource[startPos] == '=') || 
								 (strSource[startPos] == '>')  ) ) startPos ++;
			break;
		case '=':
		case '!':
			++startPos;
			if((startPos<sl) && (strSource[startPos] == '=')) startPos ++;
			break;
		case '|':
			++startPos;
			if((startPos<sl) && (strSource[startPos] == '|')) startPos ++;
			break;
		case '&':
			++startPos;
			if((startPos<sl) && (strSource[startPos] == '&')) startPos ++;
			break;
		case '\"':
//			bp++;
			++startPos;
			while(startPos < sl && strSource[startPos] != '\"')startPos++;
			if(startPos < sl) startPos ++;
			bAcceptOpt = true;
			break;
		case '\'':
//			bp++;
			++startPos;
			while(startPos < sl && strSource[startPos] != '\'') startPos++;
			if(startPos < sl) startPos ++;
			bAcceptOpt = true;
			break;
		case ')':
			bAcceptOpt = true;
			startPos ++;
			break;
		default: //"+-*/"
			startPos ++;
			break;
		}
	}

	int cl = startPos-bp;
//	if(cl < 2) return  CString("");
	char * lpc = new char[cl+1];
	CopyMemory(lpc,strSource+bp,cl);
	lpc[cl]= 0;
	CString str(lpc);
	delete [] lpc;
	return str;	
//	std::auto_ptr
}

CString CRegularOpt::GetARegularWord(LPCTSTR strSource,int& startPos)
{
	bool b = false;
	return GetARegularWord(strSource, startPos,strlen(strSource),b);
}

CString  CRegularOpt::TrimString(LPCTSTR szWord)
{
	CString strWord(szWord);
	strWord.TrimLeft();
	strWord.TrimRight();
	int	sl = strWord.GetLength();
	if(sl >= 2 && (( strWord[0]=='\"' && strWord[sl-1]=='\"') || ( strWord[0]=='\'' && strWord[sl-1]=='\'')) ){
		if(sl>2)
			strWord = strWord.Mid(1,sl-2);
		else
			strWord = "";
	}
	return strWord;
}

⌨️ 快捷键说明

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