#include <stdio.h>
int str2num(const char *s){
int i;
char c;
for (i = 0; '0' <= (c = *s) && c <= '9'; ++s)
i = i*10 + c - '0';
return i;
}
double str2double(const char *s){
int iValue = 0;
double dValue = 0.0;
//bool bDot = false;
char c;
for ( ; '0' <= (c = *s) && c <= '9' ; ++s){
iValue = iValue * 10 + c - '0';
}
if( c && '.' == c){
int base = 1;
const char *tmp = ++ s;
while( *tmp++ != '/0' ) base = base * 10;
for ( ; '0' <= (c = *s) && c <= '9' ; ++s){
dValue = dValue * 10 + c - '0';
}
dValue = dValue / base;
}
return iValue + dValue;
}
char * int2str(long n, int base, char *buf, int length)
{
char *p = buf ;
int minus;
p = &buf[length];
*--p = '/0';
if (n < 0) {
minus = 1;
n = -n;
}
else
minus = 0;
if (n == 0)
*--p = '0';
else
while (n > 0) {
*--p = "0123456789abcdef"[n % base];
n /= base;
}
if (minus)
*--p = '-';
return p;
}
int main(){
char *str = "75979";
char *dStr = "75979.534";
printf("%s, %d/n", str, str2num( str ) );
printf("%s, %.3f/n", dStr, str2double( dStr ) );
int iInt = -12345;
char cStr[10];
printf("%d, %s/n", iInt, int2str( iInt, 10, cStr, 10) );
}
atoi和itoa的实现
最新推荐文章于 2022-02-25 19:38:17 发布