atoi
来源:互联网 发布:郑州做seo的工资 编辑:程序博客网 时间:2024/06/05 08:22
一、atoi
将字符型转换成整型,跳过前面的空白字符,遇上字符或正负号开始转换,遇到非数字或"\0"结束转换,并将结果返回。如果不能转为整形或者为空字符串,返回0
二、代码实现
#include <stdio.h>#include <stdbool.h>#include <assert.h>bool Isspace(const char c){if (c == ' ' || c == "\t")return 1;elsereturn 0;}bool Isdigit(const char c){if (c <= '9' && c >= '0'){return 1;}elsereturn 0;}int my_atoi(const char* ptr){assert(ptr);int total = 0; //总数int sign = 0; //符号while(Isspace(*ptr)) {ptr++;}if (*ptr=='-'||*ptr=='+') //遇到符号开始转换,并保存符号{sign = *ptr;ptr++;}while (Isdigit(*ptr)){total = total * 10 + *ptr - '0'; //字符减字符零为数字ptr++;}if (sign == '-'){return -total;}else{return total;}}int main(){char* p = "-12345";int ret = 0;ret = my_atoi(p);printf("%d\n", ret);getchar();return 0;}三、运行结果图
阅读全文
0 0
- atoi
- atoi
- atoi
- atoi()
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- atoi
- 【Scikit-Learn 中文文档】朴素贝叶斯
- 【Scikit-Learn 中文文档】朴素贝叶斯
- KMP算法详解
- ProbabilityGenerator
- 蓝桥杯题目 输入一个正整数n,输出n!的值。
- atoi
- OpenJudge 1975 Median Weight Bead
- 欢迎使用CSDN-markdown编辑器
- 软件工程导论第十二章
- asp.net如何定时执行任务
- 初学者---Android 沉浸式(透明)状态栏适配
- 【Scikit-Learn 中文文档】决策树
- 设计模式之原型模式
- 算法作业系列10——Unique Substrings in Wraparound String