#include <stdio.h>
#include <stdlib.h>
//2009:写出一个字符串转换到整数的程序,例如输入:"-1234",输出:-1234
//思路:
#define MININT 0xffffffff
#define MAXSIZE 1024
int my_atoi(const char* str,int iRadius = 10)
{
//如果字符串为空,直接返回负的最大值
if(NULL==str)
{
return MININT;
}
int i = 0;
bool isNegative = false;
if('-' == str[i++])
{
isNegative = true;//设置负数标记
}
int sum = 0;
char ch;
for( ; (ch = str[i])!= '\0' ; i++)
{
sum = sum*iRadius + (str[i] - '0');
}
return isNegative ? -1*sum : sum;
}
int main(int argc,char* argv[])
{
printf("请输输入你想要转换的字符串:");
char str[MAXSIZE];
scanf("%s",str);
printf("\n转换后的数字是:%d",my_atoi(str));
system("pause");
getchar();
return 0;
}
08-26