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

📄 cstring_ext_num.shtml

📁 mfc资源大全包含MFC编程各个方面的源码
💻 SHTML
📖 第 1 页 / 共 2 页
字号:
<html><head><meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><meta NAME="Author" CONTENT="Guy Gascoigne - Piggford"><title>String - CString Extension for numerical IO</title></head><body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323"alink="#FF0000" bgproperties="fixed"><table WIDTH="100%">  <tr WIDTH="100%">    <td><!--#exec cgi="/cgi/ads.cgi"--></td>    <td></td>  </tr></table><h3 align="center"><font COLOR="#AOAO99">CString Extension for numerical IO </font></h3><hr align="center"><p>This code was contributed by <a HREF="mailto:mabtech@flash.net">Michael A. Barnhart</a><p>I think it is fairly self explaining. I have add calls to put and get numerical entriesinto the string. This with the parsing routines already include have proven quite usefullat least to me. </p><p>The following extentions add numerical reading and writting to the CStringEx Class. Thedefault field length of 8 is used to support my application for Finite Element Card Immageformat of 10 fields of 8. Change as you desire. The only item that I have foundinteresting is when usign the &quot;=&quot; sign to copy strings between standard CStingsand CStringEx may only be used as follows.</p><p>This is BAD </p><PRE><TT><FONT COLOR="#990000">	CString Stand;	CStingEX Ex1,Ex2;	Ex1.PutFloat(1.234);	Stand = Ex1; (this is not OK).	Ex2 = Stand; Now many functions will fail (ie. GetLenght is always zero)</pre></tt></font><p>End BAD</p><p>This is OK</p><pre><tt><FONT COLOR="#990000">	CString Stand;	CStingEX Ex1,Ex2;	Ex1.PutFloat(1.234);	Stand = (LPCSTR)Ex1;	Ex2 = Stand;</pre></tt></font><p>End OK</p><br><h3>CStringEx.h</h3><pre><tt><FONT COLOR="#990000">#if !defined(AFX_STRINGDEF_H__3A310B45_A6C3_11D1_B56D_00A024838B6B__INCLUDED_)#define AFX_STRINGDEF_H__3A310B45_A6C3_11D1_B56D_00A024838B6B__INCLUDED_// COPYRITE 1995 MAB TECH , 96,97inline double atodn(char alpha[],int n);inline float atofn(char *alpha,int n);inline int atoin(char *alpha,int n);inline long int atoln(char alpha[],int n);inline void dtoan(char *alpha,double val,int len);inline void ftoan(char *alpha,float val,int len);inline void itoan(char alpha[],int n,int l);inline void ltoan(char alpha[],long int n,int l);inline int sizestr(char string[],int max);inline void stradd(char stringto[],int maxto,char stringfrom[],int maxfrom);inline void strfill(char string[],int max,char fill);inline void strover(char stringto[],char stringfrom[],int maxfrom);class CStringEx : public CString{public:	int GetInt();	long GetLong();	float GetFloat();	double CStringEx::GetDouble();	void PutInt(int i, int len=8);	void PutLong(long i, int len=8);	void PutFloat(float f, int len=8);	BOOL IsInt();	BOOL IsFloat();	CStringEx() : CString( ){};	CStringEx( const CString&amp; stringSrc) : CString( stringSrc ){};	CStringEx( const CStringEx&amp; stringSrc) : CString( stringSrc ){};	CStringEx( TCHAR ch, int nRepeat = 1 ) : CString( ch, nRepeat ){};	CStringEx( LPCTSTR lpch, int nLength ) : CString( lpch, nLength ){};	CStringEx( const unsigned char* psz ) : CString( psz ){};	CStringEx( LPCWSTR lpsz ) : CString( lpsz ){};	CStringEx( LPCSTR lpsz ) : CString( lpsz ){};	CStringEx&amp; Insert(int pos, LPCTSTR s);	CStringEx&amp; Insert(int pos, TCHAR c);	CStringEx&amp; Delete(int pos, int len);	CStringEx&amp; Replace(int pos, int len, LPCTSTR s);	int Find( TCHAR ch, int startpos = 0 ) const;	int Find( LPCTSTR lpszSub, int startpos = 0 ) const;	int FindNoCase( TCHAR ch, int startpos = 0 ) const;	int FindNoCase( LPCTSTR lpszSub, int startpos = 0 ) const;	int FindReplace( LPCTSTR lpszSub, LPCTSTR lpszReplaceWith, BOOL bGlobal = TRUE );	int FindReplaceNoCase( LPCTSTR lpszSub, LPCTSTR lpszReplaceWith,				BOOL bGlobal = TRUE );	int ReverseFind( TCHAR ch ) const{ return CString::ReverseFind(ch);};	int ReverseFind( LPCTSTR lpszSub, int startpos = -1 ) const;	int ReverseFindNoCase( TCHAR ch, int startpos = -1  ) const;	int ReverseFindNoCase( LPCTSTR lpszSub, int startpos = -1 ) const;	CStringEx GetField( LPCTSTR delim, int fieldnum);	CStringEx GetField( TCHAR delim, int fieldnum);	int GetFieldCount( LPCTSTR delim );	int GetFieldCount( TCHAR delim );	CStringEx GetDelimitedField( LPCTSTR delimStart, LPCTSTR delimEnd,				int fieldnum = 0);};#endif // !defined(AFX_STRINGDEF_H__3A310B45_A6C3_11D1_B56D_00A024838B6B__INCLUDED_)</pre></tt></font><br><h3>CStringEx.cpp</h3><pre><tt><FONT COLOR="#990000">#include &lt;mablib.h&gt; 		// this is just for MFC classes [ may want to be afx.h or stdafx.h : Editor ]#include &lt;CStringEx.h&gt;inline double atodn(char alpha[],int n){	double val;	char format[16];	char string[32];	sprintf(format,"%c%df",'%',n);	if(n>32) n=32;	strncpy(string,alpha,n);	string[n]='\0';	val=atof(string);//0.0;	//sscanf(string,format,&amp;val);	return (val);}inline float atofn(char *alpha,int n){	float val;	char format[16];	char string[32];	sprintf(format,"%c%df",'%',n);	if(n>32) n=32;	strncpy(string,alpha,n);	string[n]='\0';	val=(float)0.;	sscanf(string,format,&amp;val);	return (val);}inline int atoin(char *alpha,int n){	int val;	char format[32];	char string[32];	sprintf(format,"%c%dd",'%',n);	val=0;	if(n>32)n=32;	strncpy(string,alpha,n);	string[n]='\0';	sscanf(string,format,&amp;val);	return (val);}inline long int atoln(char alpha[],int n){	long int val;	char string[32];	if(n>31) return(0);	strncpy(string,alpha,n);	string[n]='\0';	val=atol(string);	return (val);}inline void dtoan(char *alpha,double val,int len){	char format[16];	int exp,i;	unsigned int lenx;	char *temp;	/* do we need to use exponential */	if(val &lt; 0.0) sprintf(format,"%7.2E",val);	else          sprintf(format,"%8.2E",val);	exp=strlen(&amp;format[7]);	exp=atoin(&amp;format[7],exp);	if(exp+2 >len)	{ /* use E format */		lenx=len+7;		i=sizeof(char);		temp=(char near *)calloc(lenx,i);		if(val &lt; 0.0) sprintf(format,"%c%d.%dE",'%',len-1,len-5);		else          sprintf(format,"%c%d.%dE",'%',len,len-4);		sprintf(temp,format,val);		lenx=strlen(temp);		i=lenx-2;		do {			if(temp[i]=='0') strcpy(&amp;temp[i],&amp;temp[i+1]);			else i--;		} while(i>0 &amp;&amp; temp[i]!='E');		if(temp[i]=='E') strcpy(&amp;temp[i],&amp;temp[i+1]);		while (len &lt; (int) strlen(temp))		{			i--;			strcpy(&amp;temp[i],&amp;temp[i+1]);		}		strcpy(alpha,temp);		free(temp);	}	else	{ /* use f format */		if(val &lt; 0.0) sprintf(format,"%c%d.%df",'%',len-1,len-3-exp);		else          sprintf(format,"%c%d.%df",'%',len,len-2-exp);		sprintf(alpha,format,val);	}	return;}inline void ftoan(char *alpha,float val,int len){	char format[16];	int exp,i;	unsigned int lenx;	char *temp;	/* do we need to use exponential */	if(val &lt; 0.0) sprintf(format,"%7.2E",val);	else          sprintf(format,"%8.2E",val);	exp=strlen(&amp;format[7]);	exp=atoin(&amp;format[7],exp);	if(exp+2 >len)	{ /* use E format */		lenx=len+7;		i=sizeof(char);		temp=(char near *)calloc(lenx,i);		if(val &lt; 0.0) sprintf(format,"%c%d.%dE",'%',len-1,len-5);		else          sprintf(format,"%c%d.%dE",'%',len,len-4);		sprintf(temp,format,val);		lenx=strlen(temp);		i=lenx-2;		do {			if(temp[i]=='0') strcpy(&amp;temp[i],&amp;temp[i+1]);			else i--;		} while(i>0 &amp;&amp; temp[i]!='E');		if(temp[i]=='E') strcpy(&amp;temp[i],&amp;temp[i+1]);		while (len &lt; (int) strlen(temp))		{			i--;			strcpy(&amp;temp[i],&amp;temp[i+1]);		}		strcpy(alpha,temp);		free(temp);	}	else	{ /* use f format */		if(val &lt; 0.0) sprintf(format,"%c%d.%df",'%',len-1,len-3-exp);		else          sprintf(format,"%c%d.%df",'%',len,len-2-exp);		sprintf(alpha,format,val);	}	return;}inline void itoan(char alpha[],int n,int l){	int val;	int count;	char item;	for (count=1;count&lt;=l;count++)	{		val=n-(n/10)*10;		itoa(val,&amp;item,10);		alpha[l-count]=item;		n=n/10;	}	count=0;	while ((int)alpha[count]==48 &amp;&amp; count &lt;l-1)	{		alpha[count]=' ';		count++;	}		return;}inline void ltoan(char alpha[],long int n,int l){	long int val;	int count;	char item;	for (count=1;count &lt;=l;count++)	{		val=n-(n/10)*10;		ltoa(val,&amp;item,10);		alpha[l-count]=item;		n=n/10;	}	count=0;	while ((int)alpha[count]==48 &amp;&amp; count &lt;l-1)	{		alpha[count]=' ';		count++;	}	return;}inline int sizestr(char string[],int max){	int i;	for(i=0;i &lt; max;i++)	{		if(string[i]=='\0') return(i);	}	return(max);}inline void stradd(char stringto[],int maxto,char stringfrom[],int maxfrom){	int l1,l2,i;	l1=sizestr(stringto,maxto);	l2=sizestr(stringfrom,maxfrom);	for(i=0;i &lt;=l2;i++) stringto[l1+i]=stringfrom[i];	return;}inline void strfill(char string[],int max,char fill)/* max is length of string including null terminator */{	int l1,i;	string[max-1]='\0';	l1=sizestr(string,max);	/* l1 is length of the string    this matchs the position    of the null terminator */	for(i=l1;i &lt;=max-2;i++) string[i]=fill;	string[max-1]='\0';	return;}inline void strover(char stringto[],char stringfrom[],int maxfrom){	int l2,i;	l2=sizestr(stringfrom,maxfrom)-1;	for(i=0;i &lt;=l2;i++) stringto[i]=stringfrom[i];	return;}// Insert	- Inserts a sub string into the string// Returns	- Reference to the same string object// pos		- Position to insert at. Extends the string with spaces if needed// s		- Sub string to insertCStringEx&amp; CStringEx::Insert(int pos, LPCTSTR s){	int len = lstrlen(s);	if ( len == 0 )		return *this;		int oldlen = GetLength();	int newlen = oldlen + len;	LPTSTR str;	if ( pos >= oldlen )	{				// insert after end of string		newlen += pos - oldlen ;		str = GetBuffer( newlen );		_tcsnset( str+oldlen, _T(' '), pos-oldlen );		_tcsncpy( str+pos, s, len );	}	else	{		// normal insert		str = GetBuffer( newlen );		memmove( str+pos+len, str+pos, sizeof(_T(' ')) *(oldlen-pos) );		_tcsncpy( str+pos, s, len );	}	ReleaseBuffer( newlen );	return *this;}// Insert	- Inserts a character into the string// Returns	- Reference to the same string object// pos		- Position to insert at. Extends the string with spaces if needed// c		- Char to insertCStringEx&amp; CStringEx::Insert(int pos, TCHAR c){	TCHAR buf[2];	buf[0] = c;	buf[1] = _T('\0');	return Insert( pos, buf );}// Delete	- Deletes a segment of the string and resizes it// Returns	- Reference to the same string object// pos		- Position of the string segment to remove// len		- Number of characters to removeCStringEx&amp; CStringEx::Delete(int pos, int len){	int strLen = GetLength();		if( pos >= strLen)		return *this;	if(len &lt; 0 ||len > strLen - pos)		len = strLen - pos;

⌨️ 快捷键说明

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