【C语言】编写函数实现库函数atof
//编写函数实现库函数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的更多相关文章
- 【C语言】编写函数实现库函数atoi,把字符串转换成整形
//编写函数实现库函数atoi.把字符串转换成整形 #include <stdio.h> #include <string.h> int my_atoi(const char ...
- 用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: ...
- 如何写好 C语言 main 函数!你准备好编写 C 程序了吗?
学习如何构造一个 C 文件并编写一个 C main 函数来成功地处理命令行参数. 我知道,现在孩子们用 Python 和 JavaScript 编写他们的疯狂"应用程序".但是 ...
- C语言pow函数编写
C语言pow函数编写 #include<stdio.h> double chaoba(double f,double q); //声明自定义函数 void main(void) { dou ...
- Atiitt 使用java语言编写sql函数或存储过程
Atiitt 使用java语言编写sql函数或存储过程 1.1. java编写sql函数或存储过程的机制1 1.2. Java编写sp的优点1 1.3. 支持java源码,class文件,blog f ...
- 【C语言】编写函数,将一个数的指定位置置0或置1
//编写函数,将一个数的指定位置置0或置1 #include <stdio.h> unsigned int set_bit(unsigned int num, int pos, int f ...
- 【C语言】编写函数实现字符串旋转
//编写函数实现字符串旋转 #include <stdio.h> #include <assert.h> #include <string.h> void reve ...
- 如何用C语言编写病毒‘
怎样用C语言编写病毒在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现.[Abstract] This paper introduce the charateristic ...
- 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)
本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...
随机推荐
- MYSQL 使用事务
直接上代码,ID是唯一标识 CREATE PROCEDURE PRO2() BEGIN DECLARE t_error INTEGER; DECLARE CONTINUE HANDLER FOR SQ ...
- 使用python获得N个区分度较高的RGB颜色值
获得任意N个区分度最高的RGB颜色值是一个经典的问题,之前在做一些可视化的东西时需要解决这个问题.首先去网上找了一些方法,未果,于是想自己来搞,心里的想法是,先给出一个距离函数用来度量两个RGB颜色值 ...
- Alpha项目测试
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/3338 这个作业要求在哪里 htt ...
- 模拟title提示!
转载http://wouit.com/post/2013-08-15/40052169981html <a href="#" tishi="常见的网页新闻循环跳动显 ...
- ArrayList经典Demo
import java.util.ArrayList; import java.util.Iterator; public class ArrayListDemo { public static vo ...
- 总结struts2 iterator status的用法
1:#status.odd 是否奇数行 2:#status.count 当前行数 3:#status.index 当前行的序号,从0开始『#status.count=#status.index+1』 ...
- linux性能优化cpu-02平均负载
每次我们系统变慢时,我们通常做的第一件事就是top命令或者uptime命令,看一下系统的负载情况,比如下面: 我在命令行中输入uptime 22:15:51 表示当前系统时间 up 13 min ...
- 洛谷 1091 合唱队形(NOIp2004提高组)
[题解] 分别做一遍最长上升序列和最长下降序列,再枚举峰的位置计算答案即可. #include<cstdio> #include<algorithm> #include< ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- spring 学习(二)
public interface BeanPostProcessor { @Nullable default Object postProcessBeforeInitialization(Object ...