3,881
社区成员
发帖
与我相关
我的任务
分享
template<typename _T>
class CNotNumeric
{
public:
typedef _T* Iter;
CNotNumeric(const _T* ptr,int num, _T sentinel)
{
m_iNum = num;
m_sentinel = sentinel;
m_pNumeric = ptr;
}
~CNotNumeric()
{
m_pNumeric = NULL;
}
BOOL operator()(_T ch)
{
BOOL bExisted = FALSE;
const _T* pch = NULL;
pch = find(m_pNumeric,m_pNumeric + m_iNum -1,ch);
if (*pch == m_sentinel)
{
bExisted = TRUE;
}
return bExisted;
}
static BOOL NotNumeric(const basic_string<_T> &s)
{
_T g_szNumeric[] = {_T('0'),_T('1'),_T('2'),_T('3'),_T('4'),_T('5'),_T('6'),_T('7'),_T('8'),_T('9'),_T('z')};
basic_string<_T>::const_iterator iterCharacter = find_if(s.begin(),s.end(),CNotNumeric<_T>(g_szNumeric, sizeof(g_szNumeric)/sizeof(_T),g_szNumeric[sizeof(g_szNumeric)/sizeof(g_szNumeric[0]) - 1]));
return (iterCharacter != s.end())?TRUE:FALSE;
}
static BOOL RemoveNoNumericChar(basic_string<_T> &s)
{
_T g_szNumeric[] = {_T('0'),_T('1'),_T('2'),_T('3'),_T('4'),_T('5'),_T('6'),_T('7'),_T('8'),_T('9'),_T('z')};
basic_string<_T>::iterator iterCharacter = find_if(s.begin(),s.end(),CNotNumeric<_T>(g_szNumeric, sizeof(g_szNumeric)/sizeof(_T),g_szNumeric[sizeof(g_szNumeric)/sizeof(g_szNumeric[0]) - 1]));
while (iterCharacter != s.end())
{
s.erase(iterCharacter);
iterCharacter = find_if(s.begin(),s.end(),CNotNumeric<_T>(g_szNumeric, sizeof(g_szNumeric)/sizeof(_T),g_szNumeric[sizeof(g_szNumeric)/sizeof(g_szNumeric[0]) - 1]));
}
return TRUE;
}
private:
_T m_sentinel;
int m_iNum;
const _T* m_pNumeric;
};
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
long myatol(const char *src)
{
char sign = 0;
long result = 0;
char ch;
while(*src == ' ')
src++;
if (*src == '-')
{
sign = 1;
src++;
}
else if (*src == '+')
src++;
while(*src == ' ')
src++;
while(ch = *src)
{
assert(0x2f < ch && ch < 0x3a);
result *= 10;
result += (ch - 0x30);
src++;
}
return (sign ? -1 * result : result);
}
int main()
{
printf("%d\n", myatol(" - 123456"));
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int fun(char *s)
{ int i = strlen(s);
int j = 0;
int qiu = 0;
for(int g= 1;g <i;++g)
if( s[g]>58 && s[g]<48)
return 2;//;判断要求的是否非法。
for(int k = 0;k < i; ++k)
{ if(s[k] == '-')
{ k++; j=1;}
qiu = (qiu*10)+(s[k]-48);//0的ASC2值为48.
}
if(j == 0) return qiu;
if(j == 1) return -qiu;
}
int main(void)
{ char a[] = "-001240";//请按正常的数放在这。要不求会出错。
int ss = fun(a);
cout << ss << endl;
system("pause");
return 0;
}
/*************************************************************************
函数参数: isp_str 要转换的字符串
oi_res 正常转换后的值,否则为0
函数返回: 正常返回0,不能正常转换返回小于0
函数功能: 转换字符串到整形
*************************************************************************/
int ChangeToInt(char * isp_str, int &oi_res)
{
if(*isp_str == NULL)
{
oi_res = 0;
return -1;
}
int iNum;
char * pStr;
pStr = isp_str;
iNum = 0;
/*做字符的检查*/
while(*pStr != NULL)
{
if((*pStr > '0' && *pStr < '9' )|| (iNum == 0 && *pStr == '-')
|| (iNum == 0 && *pStr == '+'))
{
pStr++;
iNum++;
}
else
{
oi_res = 0;
return -2;
}
}
if(iNum == 1 && (isp_str[0] == '-' || isp_str[0] == '+'))
{
oi_res = 0;
return -2;
}
oi_res = atoi(isp_str);
return 0;
}