#include<iostream>
#include<assert.h>
using namespace std;
int my_atoi(const char* s)
{
int sign = 1;
int res = 0;
assert(s);
if (*s == (const char)("\0"))
{
return 0;
}
int len = strlen(s);
for (int i = 1; i < len; i++)
{
if (s[i] < '0' || s[i] > '9')
{
return 0;
}
}
for (int i = 0; i < len; i++)
{
if (s[0] == '-')
{
sign = -1;
}
if (s[0] == '+')
{
sign = 1;
}
if (s[i] >= '0' && s[i] <= '9')
{
res = res * 10 + (s[i] - '0');
}
}
return sign*res;
}
int main()
{
const char* p = "-1234";
int res = my_atoi(p);
cout << res << endl; //输出-1234
return 0;
}
文章评论