c++字符串(可能有字符)转换为int,有什么好的方法?

tianshimeng724 2010-06-26 05:00:21
我的需求
字符串 int结果
"1000" 1000
"+800" 800
"-100" -100
其他如:"0090"、"d000"、"-r789"、"+90u4"都要返回错误,请问
高手们代码怎么写?
atoi不能实现
请帮我写下代码,单写如sscanf我也不会使用,谢谢
...全文
304 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
achellies 2010-07-09
  • 打赏
  • 举报
回复

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;
};
we_sky2008 2010-06-28
  • 打赏
  • 举报
回复

#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;
}
barech 2010-06-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 haosuai 的回复:]

刚好有个现成的

C/C++ code

/*************************************************************************
函数参数: isp_str 要转换的字符串
oi_res 正常转换后的值,否则为0
函数返回: 正常返回0,不能正常转换返回小于0
函数……
[/Quote]

顶楼上的,好代码啊
liutengfeigo 2010-06-26
  • 打赏
  • 举报
回复
自己多进行判断吧。。
liutengfeigo 2010-06-26
  • 打赏
  • 举报
回复

#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;
}
haosuai 2010-06-26
  • 打赏
  • 举报
回复
刚好有个现成的


/*************************************************************************
函数参数: 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;
}
cao_julians 2010-06-26
  • 打赏
  • 举报
回复
strtok

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧