atoi的实现

#include <iostream>
using namespace std;
int atoi(char* s)
{
	int retval=0;
	int n=1;
	if (*s=='-')
	{
		n=-1;
		s++;
	}
	while (*s!='')
	{
		retval=retval*10+(*s-'0');
		s++;
	}
	return(n*retval);
}
void main()
{
	char s[6];
	long n;
	printf("Enter a string:
") ;
	gets(s);
	n = atoi(s);
	printf("%ld
",n);
	while(1);
}

  

原文地址:https://www.cnblogs.com/wuyuankun/p/3687123.html