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

📄 getcode.cpp

📁 支持Windows 3.x、Windows 9x平台上的中文(GB、Big5)、日文(Shift JIS、EUC JIS)、韩文(KS C 5601)、HZ码的显示与输入,智能内码识别
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if( c2==0xa1 )	//c2已经到头
	{
		if( c1==0xa1 )	//c1也已经到头
			return	0 ;
		c2	=0xfe ;
		c1-- ;
	}
	else
		c2-- ;
	
	return	1 ;
}

//初始EUC-JIS的c1,c2
void	InitEucJisC1C2( LPCSTR lpcsInput , int nLenOfInput )
{
	int	n ;
	
	switch( nLenOfInput )
	{
		case	1:
			n	=CharToInt( lpcsInput[0] ) ;
			if( n==0xa )
				c1	=0xa1 ;
			else
				c1	=n*16 ;
			c2	=0xa1 ;
			break ;
		case	2:
			c1	=CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
			c2	=0xa1 ;
			break ;
		case	3:
			c1	=CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
			n	=CharToInt( lpcsInput[2] ) ;
			if( n==0xa )
				c2	=0xa1 ;
			else
				c2	=n*16 ;
			break ;
	}
}
//-----------------------------------------------------------------------------------------------//

//判断KSC5601内码是否合法
BOOL	JudgeKsc5601Code( LPCSTR lpcsInput , int nLenOfInput )
{
	int	n ;
	
	switch( nLenOfInput )
	{
		case	1:	//左边第一位
			n	=CharToInt( lpcsInput[0] ) ;
			if( n>=0xa && n<=0xf )
				return	1 ;
			return	0 ;
		case	2:	//左边第二位
			n	=CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
			if( n>=0xa1 && n<=0xfd)
				return	1 ;
			return	0 ;
		case	3:	//左边第三位
			//先判断第一字节
			n	=CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
			if( n<0xa1 || n>0xfd)
				return	0 ;

			n	=CharToInt( lpcsInput[2] ) ;
			if( n>=0xa && n<=0xf )
				return	1 ;
			return	0 ;
		case	4:	//左边第四位
			//先判断第一字节
			n	=CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
			if( n<0xa1 || n>0xfd)
				return	0 ;

			n	=CharToInt( lpcsInput[2] )*16+CharToInt( lpcsInput[3] ) ;
			if( n>=0xa1 && n<=0xfe )
				return	1 ;
			return	0 ;
	}
	
	return	0 ;
}

//根据c1,c2,得到KSC5601码的下一个代码,c1,c2必须正确
BOOL	GetNextKsc5601Code( void )
{
	if( c2==0xfe )	//c2已经到头
	{
		if( c1==0xfd )	//c1也已经到头
			return	0 ;
		c2	=0xa1 ;
		c1++ ;
	}
	else
		c2++ ;
	
	return	1 ;
}

//根据c1,c2,得到KSC5601码的上一个代码,c1,c2必须正确
BOOL	GetPriorKsc5601Code( void )
{
	if( c2==0xa1 )	//c2已经到头
	{
		if( c1==0xa1 )	//c1也已经到头
			return	0 ;
		c2	=0xfe ;
		c1-- ;
	}
	else
		c2-- ;
	
	return	1 ;
}

//初始KSC5601的c1,c2
void	InitKsc5601C1C2( LPCSTR lpcsInput , int nLenOfInput )
{
	int	n ;
	
	switch( nLenOfInput )
	{
		case	1:
			n	=CharToInt( lpcsInput[0] ) ;
			if( n==0xa )
				c1	=0xa1 ;
			else
				c1	=n*16 ;
			c2	=0xa1 ;
			break ;
		case	2:
			c1	=CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
			c2	=0xa1 ;
			break ;
		case	3:
			c1	=CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
			n	=CharToInt( lpcsInput[2] ) ;
			if( n==0xa )
				c2	=0xa1 ;
			else
				c2	=n*16 ;
			break ;
	}
}
//-----------------------------------------------------------------------------------------------//

//内码输入法向下翻页
//返回0:出错或者输入字符不合法
//返回1:只可以向下翻页
//返回2:只可以向上翻页
//返回3:可以向下也可以向上翻页
//返回4:不能进行翻页
int	PageNext(  LPCSTR lpcsInput , int nLenOfInput ,		//输入串和长度
				LPSTR lpsOutput , LPINT lpnLenOfOutput )	//输出串和长度
{
	int	n , i , n1 ;
		
	switch( nLenOfInput )
	{
		case	1:	//只输入了一个字符,除了该字符都可以改变
			n	=CharToInt( lpcsInput[0] ) ;	//得到已经输入的字符值
			for( i=0 ; i<10 ; i++ )
			{
				lpsOutput[i*4]	=i+'0' ;
				//返回这个编码
				lpsOutput[i*4+1]	=c1 ;
				lpsOutput[i*4+2]	=c2 ;
				if( i<9 )
					lpsOutput[i*4+3]	=' ' ;

				//得到下一个编码
				if( !GetNextCode() )	//已经没有下一个编码了
				{
					*lpnLenOfOutput	=(i+1)*4-1 ;
					nPages++ ;
					nPageStep	=i+1 ;
					return	2 ;	//只可以向前翻页  
				}
				//判断下一个编码是否合法
				if( (c1/16) != n )	//不合法
				{
					*lpnLenOfOutput	=(i+1)*4-1 ;
					nPages++ ;
					nPageStep	=i+1 ;
					return	2 ;	//只可以向前翻页  
				}
			}
			*lpnLenOfOutput	=i*4-1 ;
			nPages++ ;
			nPageStep	=i ;
			return	3 ;
		case	2:	//只输入了两个字符,除了这两个字符都可以改变
			n	=c1 ;	//不能改变第一个字节
			for( i=0 ; i<10 ; i++ )
			{
				lpsOutput[i*4]	=i+'0' ;
				//返回这个编码
				lpsOutput[i*4+1]	=c1 ;
				lpsOutput[i*4+2]	=c2 ;
				if( i<9 )
					lpsOutput[i*4+3]	=' ' ;

				//得到下一个编码
				if( !GetNextCode() )	//已经没有下一个编码了
				{
					*lpnLenOfOutput	=(i+1)*4-1 ;
					nPages++ ;
					nPageStep	=i+1 ;
					return	2 ;	//只可以向前翻页  
				}
				//判断下一个编码是否合法
				if( c1 != n )	//不合法
				{
					*lpnLenOfOutput	=(i+1)*4-1 ;
					nPages++ ;
					nPageStep	=i+1 ;
					return	2 ;	//只可以向前翻页  
				}
			}
			*lpnLenOfOutput	=i*4-1 ;
			nPages++ ;
			nPageStep	=i ;
			return	3 ;
		case	3:	//只输入了三个字符,除了这三个字符都可以改变
			n	=c1 ;	//不能改变第一个字节
			n1	=CharToInt( lpcsInput[2] ) ;	//不能改变第三位数
			for( i=0 ; i<10 ; i++ )
			{
				lpsOutput[i*4]	=i+'0' ;
				//返回这个编码
				lpsOutput[i*4+1]	=c1 ;
				lpsOutput[i*4+2]	=c2 ;
				if( i<9 )
					lpsOutput[i*4+3]	=' ' ;

				//得到下一个编码
				if( !GetNextCode() )	//已经没有下一个编码了
				{
					*lpnLenOfOutput	=(i+1)*4-1 ;
					nPages++ ;
					nPageStep	=i+1 ;
					return	2 ;	//只可以向前翻页  
				}
				//判断下一个编码是否合法
				if( c1 != n || (c2/16!=n1) )	//不合法
				{
					*lpnLenOfOutput	=(i+1)*4-1 ;
					nPages++ ;
					nPageStep	=i+1 ;
					return	2 ;	//只可以向前翻页  
				}
			}
			*lpnLenOfOutput	=i*4-1 ;
			nPages++ ;
			nPageStep	=i ;
			return	3 ;
	}
	
	return	0 ;
}

//判断内码是否合法
BOOL	JudgeCode( LPCSTR lpcsInput , int nLenOfInput )
{
	switch( GetInputCode() )	//判断当前的输入内码
	{
		case	0:	//GB
		case	2:	//HZ
			return	JudgeGBCode( lpcsInput , nLenOfInput ) ;
		case	1:	//BIG5
			return	JudgeBig5Code( lpcsInput , nLenOfInput ) ;
		case	3:	//SHIFT-JIS
			return	JudgeShiftJisCode( lpcsInput , nLenOfInput ) ;
		case	4:	//EUC-JIS
			return	JudgeEucJisCode( lpcsInput , nLenOfInput ) ;
		case	5:	//KSC5601
			return	JudgeKsc5601Code( lpcsInput , nLenOfInput ) ;
	}
	
	return	0 ;
}

//根据c1,c2,得到下一个代码,c1,c2必须正确
BOOL	GetNextCode( void )
{
	switch( GetInputCode() )	//判断当前的输入内码
	{
		case	0:	//GB
		case	2:	//HZ
			return	GetNextGBCode() ;
		case	1:	//BIG5
			return	GetNextBig5Code() ;
		case	3:	//SHIFT-JIS
			return	GetNextShiftJisCode() ;
		case	4:	//EUC-JIS
			return	GetNextEucJisCode() ;
		case	5:	//KSC5601
			return	GetNextKsc5601Code() ;
	}
	
	return	0 ;
}

//根据c1,c2,得到前一个代码
BOOL	GetPriorCode( void )
{
	switch( GetInputCode() )	//判断当前的输入内码
	{
		case	0:	//GB
		case	2:	//HZ              
			return	GetPriorGBCode() ;
		case	1:	//BIG5
			return	GetPriorBig5Code() ;
		case	3:	//SHIFT-JIS
			return	GetPriorShiftJisCode() ;
		case	4:	//EUC-JIS
			return	GetPriorEucJisCode() ;
		case	5:	//KSC5601
			return	GetPriorKsc5601Code() ;
	}
	
	return	0 ;
}

//初始c1,c2
void	InitC1C2( LPCSTR lpcsInput , int nLenOfInput )
{
	switch( GetInputCode() )	//判断当前的输入内码
	{
		case	0:	//GB
		case	2:	//HZ
			InitGBC1C2( lpcsInput , nLenOfInput ) ;
			return ;
		case	1:	//BIG5
			InitBig5C1C2( lpcsInput , nLenOfInput ) ;
			return ;
		case	3:	//SHIFT-JIS
			InitShiftJisC1C2( lpcsInput , nLenOfInput ) ;
			return ;
		case	4:	//EUC-JIS
			InitEucJisC1C2( lpcsInput , nLenOfInput ) ;
			return ;
		case	5:	//KSC5601
			InitKsc5601C1C2( lpcsInput , nLenOfInput ) ;
			return ;
	}
}

//--------------------------------------------------------------------------------------------------------//
#ifdef __cplusplus
extern "C" {
#endif
			
//根据输入的英文内码,得到汉字
//nFlag=0,从头开始查,设置翻页指针
//nFlag=3,向下翻页,改变翻页指针
//nFlag=4,向上翻页
//返回0:出错或者输入字符不合法
//返回1:只可以向下翻页
//返回2:只可以向上翻页
//返回3:可以向下也可以向上翻页
//返回4:不能进行翻页
int __export FAR PASCAL	GetHzOfCode(
					LPCSTR lpcsInput , int nLenOfInput ,		//输入串和长度
					LPSTR lpsOutput , LPINT lpnLenOfOutput ,	//输出串和长度
					int nFlag )
{
	if( nFlag==3 )	//向下翻页
		return	PageNext( lpcsInput , nLenOfInput , lpsOutput , lpnLenOfOutput ) ;
	if( nFlag==4 )	//向上翻页
	{
		for( int i=0 ; i<10+nPageStep ; i++ )	//往前翻两页
			GetPriorCode() ;
		nPages	-=2 ;	//减少两页
		//往下翻一页
		PageNext( lpcsInput , nLenOfInput , lpsOutput , lpnLenOfOutput ) ;
		if( !nPages )	//一页没翻
			return	1 ;	//只可以向下翻页
		return	3 ;	//可以向上也可以向下翻页
	}
        
        //剩余的就是从头开始查
	//判断输入内码是否合法
	if( !JudgeCode( lpcsInput , nLenOfInput ) )	//不合法
		return	0 ;

	if( nLenOfInput==4 )	//长度为4,直接返回
	{
		lpsOutput[0]	='1' ;
		lpsOutput[1]	=CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
		lpsOutput[2]	=CharToInt( lpcsInput[2] )*16 + CharToInt( lpcsInput[3] ) ;
		*lpnLenOfOutput	=3 ;
		return	1 ;
	}
	
	//确定初始的c1,c2
	InitC1C2( lpcsInput , nLenOfInput ) ;

	//赋值
	for( int i=0 ; i<10 ; i++ )
	{
		lpsOutput[i*4]	=i+'0' ;
		lpsOutput[i*4+1]	=c1 ;
		lpsOutput[i*4+2]	=c2++ ;
		if( i<9 )
			lpsOutput[i*4+3]	=' ' ;
	}
	*lpnLenOfOutput	=39 ;
	
	nPages	=0 ;	//初始翻过的页
        return	1 ;
}

#ifdef __cplusplus
}
#endif

⌨️ 快捷键说明

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