模拟itoa和atoi
来源:互联网 发布:淘宝1688代销赚钱吗 编辑:程序博客网 时间:2024/06/05 08:42
模拟实现atoi
函数原型:
int atoi ( const char * str );
传入一个char*,返回值为int
int my_atoi(const char* str){ assert(str); int num = 0; int flag = 1; //跳过空白字符 while (*str == ' ' || *str == '\t' || *str == '\n') str++; //判断转换后数字的正负 flag = (*str == '-') ? -1 : 1; //如果有输入符号,则要将此符号跳过 if (*str == '-' || *str == '+') str++; //每一位数字转为数字 while (*str >= '0' && *str <= '9') { num = num * 10 + (*str - '0'); str++; } return num*flag;}
模拟实现itoa
函数原型:
char* itoa(int value,char* str,int radix)
int转为char*。
value为int的值。
str为最后字符串保存的地方。
radix为转为X进制的字符。
char* my_itoa(int value,char* str,int radix){ char* firstdig; char* p = str; unsigned int digval; if (value < 0) { *p++ = '-'; value = -value; } firstdig = p; //这时存到p中(也就是str)的数字为反的 do { //数字一定是无符号整型,并且数字为对进制位取得余数。 digval = value%radix; value /= radix; if (digval > 9) *p = (char)(digval - 10 + 'a'); else *p = (char)(digval + '0'); p++; } while (value>0); //这里需要翻转一下 //先把末尾置为\0结束符 *p-- = '\0'; while (firstdig < p) { char temp = *p; *p = *firstdig; *firstdig = temp; firstdig++; p--; } return str;}
阅读全文
0 0
- 模拟itoa和atoi
- 【字符串】模拟实现atoi和itoa
- 模拟c库函数atoi和itoa
- 模拟实现atoi和itoa函数
- 模拟实现atoi、itoa
- 模拟实现C库的atoi和itoa
- 模拟实现c库的atoi和itoa
- atoi和itoa函数
- 浅析atoi和itoa
- atoi 和itoa用法
- atoi 和itoa用法
- atoi 和itoa用法
- itoa和atoi函数
- atoi和itoa函数
- atoi和itoa
- atoi 和itoa用法
- itoa和atoi
- itoa 和 atoi 函数
- win10蓝屏问题,关于驱动kisSaasUrlRedirectKnl64.sys 的
- java循环拼字符串
- linkit one学习笔记-开发板简介
- Altium designer在PCB界面的高亮方法
- 使用__slots__:
- 模拟itoa和atoi
- 纯C#代码生成条码
- 奶牛身高
- 用java 自动登录一个网站的例子
- test
- libxls库的使用
- Android中EditTex焦点设置和弹不弹出输入法的问题
- 热修复技术 Sophix
- .babelrc文件在windows系统下的创建方式