//编写函数实现库函数atof
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
double calculate(const char *src, int flag)
{
double num = 0.0;
int n = 0;
int count = 0;
while (*src)
{
if ((*src >= '0') && (*src <= '9'))//推断输⼊入是否为⾮非数组字符
{
num = num * 10 + (*src - '0');
src++;
if (n)
{
count++;
}
}
else if (*src == '.')
{
n = 1;
src++;
}
else
{
num = 0;
break;
}
}
if (n)
{
num = num / powl(10,count);
}
return flag * num;
}
double str_to_int(const char *str)
{
double num = 0.0;
int flag = 1;
if (str != NULL && *str != '\0')
{
while (isspace(*str))
{
str++;
}
if (*str == '+')
str++;
else if (*str == '-')
{
str++;
flag = -1;
}
if (*str != '\0')
{
num = calculate(str, flag);
}
}
return num;
}
int main()
{
printf("%f\n", str_to_int(" +23.45"));
printf("%f\n", str_to_int(" -2.345"));
printf("%f\n", str_to_int("+234.5"));
printf("%f\n", str_to_int("-2345.44"));
printf("%f\n", str_to_int("2345"));
printf("%f\n", str_to_int("-1111.11"));
printf("%f\n", str_to_int(""));
printf("%f\n", str_to_int("123ab"));
return 0;
}

【C语言】编写函数实现库函数atof的更多相关文章

  1. 【C语言】编写函数实现库函数atoi,把字符串转换成整形

    //编写函数实现库函数atoi.把字符串转换成整形 #include <stdio.h> #include <string.h> int my_atoi(const char ...

  2. 用c++语言编写函数 int index(char *s,char * t),返回字符串t在字符串s中出现的最左边的位置,如果s中没有与t匹配的子串,则返回-1。类似于索引的功能。

    首先,分析一下程序的思路: 1:从s的第i个元素开始,与t中的第1个元素匹配,如果相等,则将s的第i+1元素与t中的第2个元素匹配,以此类推,如果t所有元素都匹配,则返回位置i;否则,执行2; 2: ...

  3. 如何写好 C语言 main 函数!你准备好编写 C 程序了吗?

    学习如何构造一个 C 文件并编写一个 C main 函数来成功地处理命令行参数.   我知道,现在孩子们用 Python 和 JavaScript 编写他们的疯狂"应用程序".但是 ...

  4. C语言pow函数编写

    C语言pow函数编写 #include<stdio.h> double chaoba(double f,double q); //声明自定义函数 void main(void) { dou ...

  5. Atiitt 使用java语言编写sql函数或存储过程

    Atiitt 使用java语言编写sql函数或存储过程 1.1. java编写sql函数或存储过程的机制1 1.2. Java编写sp的优点1 1.3. 支持java源码,class文件,blog f ...

  6. 【C语言】编写函数,将一个数的指定位置置0或置1

    //编写函数,将一个数的指定位置置0或置1 #include <stdio.h> unsigned int set_bit(unsigned int num, int pos, int f ...

  7. 【C语言】编写函数实现字符串旋转

    //编写函数实现字符串旋转 #include <stdio.h> #include <assert.h> #include <string.h> void reve ...

  8. 如何用C语言编写病毒‘

    怎样用C语言编写病毒在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现.[Abstract] This paper introduce the charateristic ...

  9. 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...

随机推荐

  1. SQL server 上机练习题

    首先创建一个数据库,里面有 登录表 学生表   课程表   选课表 成绩表 1. 查询Student表中的所有记录的Sname.Ssex和Class列.2. 查询教师所有的单位即不重复的Depart列 ...

  2. 诊断:expdp导出时遇到错误ORA-31693和ORA-00922

    11.2.0.1使用数据泵expdp导出时,如果使用parallel,可能会遇到 ORA-: Table data object "OWNER"."TABLE" ...

  3. (C/C++学习)23.C++中指针的长度

    引言:先看下面一个程序会打印出什么? #include<iostream> using namespace std; int main() { int a = 2; int *p = &a ...

  4. (C/C++学习)15.C语言字符串和字符数组

    说明:在C语言中字符串和字符数组有很多相似之处,却又有着一些不同.本文将针对其区别与联系,进行分析总结. 一.字符串 1.在C语言中,字符串是由双引号括起来的任意字符序列,如:"china& ...

  5. POJ3616 Milking Time【dp】

    Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...

  6. UVA - 208 Firetruck(并查集+dfs)

    题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...

  7. TestNG安装及配置

    1. 在idea中新建一个maven项目 2. 在pom.xml中添加testng和reportng依赖 <dependencies> <!-- 添加testNG依赖 --> ...

  8. Python-函数和代码复用

    函数的定义与使用 >函数的理解与定义 函数是一段代码的表示 -函数是一段具有特定功能的.可重用的语句组 -函数是一种功能的抽象,一般函数表达特定功能 -两个作用:降低编程难度 和 代码复用 de ...

  9. 洛谷 2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver

    [题解] 每个格子可以到达的区域是一个菱形,但是我们并不能快速的求和,所以我们可以把原来的草地旋转45度,用二维前缀和快速处理菱形的区域的和. #include<cstdio> #incl ...

  10. Webstorm如何配置自动补全前缀--autoprefixer

    我们在写样式代码时,对不同平台会有不同的兼容性写法,会在代码前加前缀,但是手动加前缀很费时间而且很容易弄错.Webstorm编辑器是有自带补全前缀功能的,那为什么还要写这篇配置博客,因为Webstor ...