首页 > 其他 > 详细

自己实现atoi函数

时间:2014-11-26 06:40:37      阅读:293      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <string>
using namespace std;
// 自己实现atoi函数的过程


int My_atoi(const char *  p);


int main()
{
string s;
cout << "请输入一个字符串:";
cin >> s;
cout<<"经过转换为整形为:"<<My_atoi(s.c_str())<<endl;
system("pause");
return 0;
}


int My_atoi(const char *  p)
{
int flag = 1 ,result = 0;
if (*p >= ‘0‘ && *p <= ‘9‘ || ‘-‘ == *p || ‘+‘ == *p)
if (‘-‘ == p[0])
{
flag = -1;
p++;
}
else if (‘+‘ == p[0])
p++;
while (*p <= ‘9‘ && *p >= ‘0‘)
result = 10 * result + (*p++ - ‘0‘);
return flag * result;


}

自己实现atoi函数

原文:http://blog.csdn.net/fantasydreams/article/details/41505853

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!