【c语言】模拟实现库函数的atof函数
2023-09-27 14:27:00 时间
// 模拟实现库函数的atof函数 #include <stdio.h> #include <string.h> #include <assert.h> #include <ctype.h> double my_atof(char const *p) { double ret = 0; int flag = 1; int count = 0; assert(p != NULL); while (isspace(*p)) { p++; } while (*p) { if (count) { count = count * 10; } if (*p == '+') p++; else if (*p == '-') { p++; flag = -1; } else if (*p == '.') { count++; p++; } else if (*p >= '0'&& *p <= '9') { ret = ret * 10 + (*p - '0'); p++; } else return 0; } return ret*flag / count; } int main() { printf("%f\n", my_atof(" +23.45")); printf("%f\n", my_atof(" -2.345")); printf("%f\n", my_atof("+234.5")); printf("%f\n", my_atof("-23.45")); printf("%f\n", my_atof("2.345")); printf("%f\n", my_atof("234.5")); printf("%f\n", my_atof(" .")); printf("%f\n", my_atof("12.3ab")); return 0; } <img src="//img-blog.csdn.net/20150704160911903?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
相关文章
- 如何让c语言使用结构体近似模拟c++中的类
- C#实现的三种方式实现模拟键盘按键
- javascript模拟类的最佳实践
- 【MATLAB教程案例88】通过matlab模拟光的干涉现象
- 1133 Splitting A Linked List (25 分)【难度: 一般 / 知识点: 链表模拟】
- 1042 Shuffling Machine (20 分)【难度: 简单 / 知识点: 模拟】
- 在Linux中模拟击键和鼠标移动
- UVaLive 6581 && Gym 100299B What does the fox say? (模拟+STL)
- 《Android游戏开发详解》一2.20 对象的更多实践——模拟一个色子
- Fiddler模拟post四种请求数据
- GO语言练习:第二个工程--模拟音乐播放器
- arcgis api 4.x for js 集成 Echarts4 实现模拟迁徙图效果(附源码下载)
- 模拟源码深入理解Vue数据驱动原理(1)
- 基于unity物体定点移动与模拟刹车的细节 GIF 图文详解——线性差值函数以及平滑阻尼的运用和实践(Lerp AND SmoothDamp)
- CodeForces 404 Marathon ( 浮点数取模 -- 模拟 )
- <模拟电子学习1>Multisim 12.0 结构和仿真51最小的单芯片系统