实现string到double的转换
分析:此题虽然类似于atoi函数,但毕竟double为64位, 而且支持小数,因而边界条件更加严格,写代码时需要更加注意。
#include <errno.h> #include <float.h> #include <stdio.h> #include <stdlib.h> #include "math.h" enum status { invaild = 0, vaild }; int g_status = vaild; int isspace(const char* str) { return *str == ' '; } int isDigit(char num) { if (num >= '0' && num <= '9') { return 1; } else { return 0; } } //对atof函数进行扩充,使其可以处理科学计数法形式的字符如 123.45e-6 double strToDouble(const char* nptr) { double val = 0, power = 1; int exp = 0; if (nptr == NULL) { return 0; } g_status = invaild; //跳过空格 while (*nptr == ' ') { nptr++; } int flag = *nptr == '-' ? -1 : 1; if (*nptr == '-' || *nptr == '+') { nptr++; } while (isDigit(*nptr)) { val = val * 10 + *(nptr++) - '0'; } if (*nptr == '.') { nptr++; } while (isDigit(*nptr)) { val = val * 10 + *(nptr++) - '0'; power *= 10; } val = val / power; if (*nptr == 'e' || *nptr == 'E') { nptr++; int eflag = *nptr == '-' ? -1 : 1; if (*nptr == '-' || *nptr == '+') { nptr++; } while (isDigit(*nptr)) { exp = exp * 10 + *(nptr++) - '0'; } //是否越界 // else if (eflag == 1) { if (val > DBL_MAX * pow(10.0, (double) -exp)) { return DBL_MAX; } while (exp-- > 0) { val *= 10; } } else if (eflag == -1) { if (val < DBL_MIN * pow(10.0, (double) exp)) { return DBL_MIN; } while (exp-- > 0) { val /= 10; } } } if (*nptr == '\0') { g_status = vaild; } return flag * val; } int main(void) { // DBL_MIN: 2.2250738585072014e-308 // DBL_MAX: 1.7976931348623157e+308 setvbuf(stdout, NULL, _IONBF, 0); char str[100] = "1.79e308"; double num; num = strToDouble(str); if (g_status) { printf("%.16e\n", num); } else { printf("%s", "invaild input!!!"); } return EXIT_SUCCESS; }
实现string到double的转换的更多相关文章
- double型转换成string型
double型转换成string型 题目描写叙述: 如有一个函数.其可接受一个long double參数,并将參数转换为字符串.结果字符串应保留两位小数,比如,浮点值123.45678应该生成&quo ...
- string与double的互相转换
#include <iostream> #include <string> #include <sstream> string DoubleToString(dou ...
- string和double之间的相互转换(C++)
很多人都写过这个标题的文章,但本文要解决的是确保负数的string和double也可以进行转换. 代码如下: string转double double stringToDouble(string nu ...
- 关于==和equals()方法&Java中string与char如何转换&String,StringBuffer
1.对于基本数据类型,可以直接使用==和!=进行内容比较 如:int x=30; int y=30; x==y; //true 基本数据类型 简单类型(基本类型) bo ...
- String转double失去精度问题
最近遇到一个坑,微信小程序中退款 19.9的字符串转double变成19.89,导致退不成功 . 坑死我了.现在把更改后的代码贴出来 public static void main(String[] ...
- 与String有关的强制转换
String --> int int i = Integer.parseInteger("123"); String --> double double d = Dou ...
- go中string和slice no-copy转换
在go里面,string和slice的互换是需要进行内存拷贝的,虽然在底层,它们都只是用 pointer + len来表示的一段内存. 通常,我们不会在意string和slice的转换带来的内存拷贝性 ...
- string 到 wstring的转换
string 到 wstring的转换_一景_新浪博客 string 到 wstring的转换 (2009-08-10 20:52:34) 转载▼ 标签: 杂谈 ...
- 利用Gson进行String和对象的转换
利用Gson进行String和对象的转换 /** * 从JsonStr中解析BUserBase * @param jsonStr * @return */ public static BUserBas ...
随机推荐
- P2515 [HAOI2010]软件安装
树形背包 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> ...
- ●BZOJ 3640 JC的小苹果
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3640题解: 期望dp,高斯消元 设dp[i][h]在i位置且血量为h这个状态的期望经过次数. ...
- ●BZOJ 2693 jzptab
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2693 题解: 莫比乌斯反演 先看看这个题,BZOJ 2154 Crash的数字表格,本题的升 ...
- bzoj 4025: 二分图
Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于是他想考考你. 解题报告: ...
- hdu 4533 线段树(问题转化+)
威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- oracle循环插入1万条数据
declare maxnumber constant number:=10000; i number :=1; begin for i in 1..maxnumber loop insert into ...
- OpenCV3.1.0中调用MHI(Motion History Images, 运动历史图像)
写在前边: OpenCV3.0+要想使用MHI,就要现安装扩展模块opencv_contrib.安装方法见:ubuntu 14.04 64位 安装Opencv3.1.0 (包含opencv_contr ...
- SpringSecurity 进行自定义Token校验
背景 Spring Security默认使用「用户名/密码」的方式进行登陆校验,并通过cookie的方式存留登陆信息.在一些定制化场景,比如希望单独使用token串进行部分页面的访问权限控制时,默认方 ...
- CentOS, Fedora, or Red Hat一行命令安装apache + mysql + php 及各种依赖库
sudo sh -c "yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-comm ...