码迷,mamicode.com
首页 > 其他好文 > 详细

my_atoi

时间:2015-07-21 12:02:12      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<iostream>
 2 using namespace std;
 3 int my_atoi(char *s)
 4 {
 5     if(s==NULL) 
 6         return -1;
 7     char *p=s;
 8     int n=0,sum=0;
 9     while(*p++)
10         n++;
11     bool isNegative=false;
12     int j=0;
13     if(s[0]==-)
14     {
15         isNegative=true;
16         j=1;
17     }
18     for(;j<n;j++)
19         sum=sum*10+s[j]-0;
20     if(isNegative)
21         sum*=-1;
22     return sum;
23 }
24 int main()
25 {
26     char *s="-123";
27     cout<<my_atoi(s)<<endl;
28     system("pause");
29     return 0;
30 }

 

my_atoi

标签:

原文地址:http://www.cnblogs.com/riden/p/4663785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!