|
发表于 2012-10-10 21:22:11
|
显示全部楼层
本帖最后由 histamine 于 2012-10-10 21:37 编辑
LZ代码吐槽点很多
建议LZ重新复习C语言数组与指针有关部分
无聊献丑写一个- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <limits.h>
- static char *long2str( long num )
- {
- char *p, *low, *high;
- char *str = (char *)malloc( (size_t)(log10(LONG_MAX) + 3.0) * sizeof(char) );
- p = str;
- if ( num < 0 )
- {
- *(p++) = '-';
- num = labs( num );
- }
- low = p;
- do
- {
- int digit = num % 10;
- *(p++) = digit + '0';
- num /= 10;
- } while ( num > 0 );
- *(p--) = '\0';
- high = p;
- while( low < high )
- {
- char temp = *low;
- *(low++) = *high;
- *(high--) = temp;
- }
- return str;
- }
- static void printlong( long num )
- {
- char *str = long2str( num );
- printf( "%s\n", str );
- free( str );
- }
- int main(void)
- {
- printlong( -12645L );
- printlong( 12645L );
- printlong( 0L );
- return 0;
- }
复制代码 |
|