前两天写了下atol,今天下午就想了想写了itoa;没想到这个还挺复杂函数原型
char *itoa( int value, char *str);
- char *itoa(int value, char *str)
- {
- char dig[] = "0123456789";
- int i = 0, neg = 1;
- char *s, *e;
- char mid;
- long q = 0;
- int r = 0;
- if(str == NULL)
- return NULL;
- if(value > 0)
- {
- neg = 0;
- value = -value;
- }
- while(value <= -10)
- {
- q = value / 10;
- r = value % 10;
- if(r > 0)
- {
- r -= 10;
- q++;
- }
- str[i++] = (-r) + dig[0];
- value = q;
- }
- str[i] = (-value) + dig[0];
- if(neg == 1){
- str[i+1] = '-';
- str[i+2] = '\0';
- e = str + (i+1);
- }else{
- str[i+1] = '\0';
- e = str + i;
- }
-
- for( s = str; s < e; s++, e--)
- {
- mid = *s;
- *s = *e;
- *e = mid;
- }
- return str;
- }
阅读(2938) | 评论(0) | 转发(0) |