#include <stdio.h>
#include <iostream>
using std::cout;
using std::endl;
int Atoi(char *);
int main(){
int num0,num1,num2;
//Testing Positive String
char str[] = "12345";
num0 = Atoi(str);
//Testing Negative String
char N[] = "-12345";
num1= Atoi(N);
char M[] = "-1nw321";
num2= Atoi(M);
cout<<num0<<endl;
cout<<num1<<endl;
cout<<num2<<endl;
return 0;
}
int Atoi(char *str){
int number=0;
int symbol_flag=1;
int i=0;
//Make sure that the negative number is still working
if (str[i]=='-') {
i++;
symbol_flag=-1;
}
//Tranverse the string to get the number
for (; str[i]!='\0'; ++i) {
if (str[i]>='0'&&str[i]<='9') {
number=number*10+str[i]-'0';
}
else{
return 0;
}
}
return number*symbol_flag;
}
ATOI函数-代码实例
最新推荐文章于 2024-04-19 01:00:00 发布