输入浮点数3.6printf输出浮点数3.600A

【C语言模拟实现】浮点数-转-定点数 - 推酷
【C语言模拟实现】浮点数-转-定点数
要想超神,就要什么都精!
知识准备:
1. 输出浮点数的十六进制形式?(利用指针输出)
将浮点数指针-转换成-整型指针,以十六进制的格式输出指针内容。
示例程序:
#include&stdio.h&
int main()
float *var;
scanf(&%f&,var);
printf(&%x&,*((int*)var));
输入(float)
输出(十六进制)
浮点数在计算机的存储格式?
符号位:0为正,1为负;
指数位:移码表示;
尾数位:隐式存储小数点前面的1,也就是只存储小数点后面的位
十进制:8.25
二进制:1000.01 = 1.00001 x 2
= 1.00001 x 2
符号位为:0
指数位为:3 + 127 = 130 = b
尾数位为:00001
最终,8.25在计算机中存储的形式为00 00 b
对比我们自己计算出的结果 与 通过计算机输出的结果,一致:程序正确。
可以正式进行程序设计了
程序设计:
我们int型作为定点数的一个容器,假设定点数32位,符号部分1位,整数部分15位,小数部分16位
分别得到浮点数的符号、整数部分与小数部分,对应到定点数的各部分。
#include&stdio.h&
#define SIGN_BIT 0x
#define EXP_BIT
0x7f800000
#define TAIL_BIT 0x007fffff
int main()
float *aF//浮点数
int aFix = 0;//定点数容器
int tmp = 0;//浮点数容器
int exp = 0;//指数大小
int tail = 0;//尾数位容器
scanf(&%f&,aFloat);
tmp = *((int*)aFloat);//置定点数的符号位
aFix = tmp & SIGN_BIT;
//置定点数的整数部分
exp = ((tmp & EXP_BIT) && 23) - 127;//指数值
tail = ((tmp & TAIL_BIT) | 0x);//尾数各位
aFix = aFix | ((tail && (23-exp)) && 16);
//置定点数的小数部分
aFix = aFix | ((tail & ~(0xffffffff && (23-exp))) && (7-exp));
printf(&%x\n&,aFix);
输出(十六进制)
输出(二进制)
按照我们前面指定的规则:定点数的符号位1位,整数位15位,小数位16位
将二进制换算出来,答案正确。
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
排版有问题
没有分页内容
视频无法显示
图片无法显示浮点数字符串转换成浮点数实现 - c语言程序开发技术文章 - 红黑联盟
浮点数字符串转换成浮点数实现
&& 之前面试的时候,常给面试者出的一个面试题目是,给定一个字符串,输出该字符串表示的浮点数的值,要求如下:
&&&&&&& 写一个转换函数,该函数的输入是一个表示浮点数的字符串,把该字符串转换成浮点数并输出。条件:请考虑各种情况,并且代码中的循环尽量少,不能调用API或者crt库中的函数。例如:输入字符串&345.7&,则输出浮点数345.7。接口可以为:float StrToFloatA(TCHAR* pstrfloat);
&&&&&&& 没想到完全做对这个题目的人居然不多(上机笔试,函数写好之后丢到测试机里面跑一下看是否完全正确),因此自己在空闲时间实现了一下,确实还是有一点难度的。代码如下:
/* -------------------------------------------------------------------------
//& 文件名&&&& :&& StringToFloat.h
//& 创建者&&&& :&& magictong
//& 创建时间&&& :&&
//& 功能描述&&& :&&&
//& $Id: $
// -----------------------------------------------------------------------*/&
#ifndef __STRINGTOFLOAT_H__&
#define __STRINGTOFLOAT_H__&
// -------------------------------------------------------------------------&
float StrToFloatW(wchar_t* pstrfloat);&
float StrToFloatA(char* pstrfloat);&
#if defined(UNICODE) || defined(_UNICODE)&
&&& #define StrToFloat& StrToFloatW&
&&& #define StrToFloat& StrToFloatA&
#endif // !UNICODE&
// -------------------------------------------------------------------------&
// $Log: $&
#endif /* __STRINGTOFLOAT_H__ */&
/* -------------------------------------------------------------------------
//& 文件名&&&& :&& StringToFloat.cpp
//& 创建者&&&& :&& magictong
//& 创建时间&&& :&&
//& 功能描述&&& :&&&
//& $Id: $
// -----------------------------------------------------------------------*/&
#include &stdafx.h&&
#include &StringToFloat.h&&
// -------------------------------------------------------------------------&
#pragma warning(disable:4244)&
// -------------------------------------------------------------------------&
// 函数&&&&&& : StrToFloatA&
// 功能&&&&&& : 将一个字符串转换为浮点数&
// 返回值& : float&&
// 参数&&&&&& : char* pstrfloat&
// 附注&&&&&& :&&
// -------------------------------------------------------------------------&
float StrToFloatA(char* pstrfloat)&
&&& // check&
&&& if (!pstrfloat)&
&&&&&&& return 0.0;&
&&& bool bNegative =&
&&& bool bDec =&
&&& char* pSor = 0;&
&&& char chByte = '0';&
&&& float fInteger = 0.0;&
&&& float fDecimal = 0.0;&
&&& float fDecPower = 0.1f;&
&&& // 进行首位判断,判断是否是负数&
&&& if (pstrfloat[0] == '-')&
&&&&&&& bNegative =&
&&&&&&& pSor = pstrfloat + 1;&
&&&&&&& bNegative =&
&&&&&&& pSor =&
&&& while (*pSor != '\0')&
&&&&&&& chByte = *pS&
&&&&&&& if (bDec)&
&&&&&&& {&
&&&&&&&&&&& // 小数&
&&&&&&&&&&& if (chByte &= '0' && chByte &= '9')&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& fDecimal += (chByte - '0') * fDecP&
&&&&&&&&&&&&&&& fDecPower = fDecPower * 0.1;&
&&&&&&&&&&& }&
&&&&&&&&&&& else&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& return (bNegative? -(fInteger +& fDecimal): fInteger + fDecimal);&
&&&&&&&&&&& }&
&&&&&&& }&
&&&&&&& else&
&&&&&&& {&
&&&&&&&&&&& // 整数&
&&&&&&&&&&& if (chByte &= '0' && chByte &= '9')&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& fInteger = fInteger * 10.0 + chByte - '0';&
&&&&&&&&&&& }&
&&&&&&&&&&& else if (chByte == '.')&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& bDec =&
&&&&&&&&&&& }&
&&&&&&&&&&& else&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& return (bNegative? -fInteger : fInteger);&
&&&&&&&&&&& }&
&&&&&&& }&
&&&&&&& pSor++;&
&&& return (bNegative? -(fInteger +& fDecimal): fInteger + fDecimal);&
// -------------------------------------------------------------------------&
// 函数&&&&&& : StrToFloatW&
// 功能&&&&&& : 将一个字符串转换为浮点数&
// 返回值& : float&&
// 参数&&&&&& : char* pstrfloat&
// 附注&&&&&& :&&
// -------------------------------------------------------------------------&
float StrToFloatW(wchar_t* pstrfloat)&
&&& // check&
&&& if (!pstrfloat)&
&&&&&&& return 0.0;&
&&& bool bNegative = &
&&& bool bDec =&
&&& wchar_t* pSor = 0;&
&&& wchar_t chByte = L'0';&
&&& float fInteger = 0.0;&
&&& float fDecimal = 0.0;&
&&& float fDecPower = 0.1f;&
&&& // 进行首位判断,判断是否是负数&
&&& if (pstrfloat[0] == L'-')&
&&&&&&& bNegative =&
&&&&&&& pSor = pstrfloat + 1;&
&&&&&&& bNegative =&
&&&&&&& pSor =&
&&& while (*pSor != L'\0')&
&&&&&&& chByte = *pS&
&&&&&&& if (bDec)&
&&&&&&& {&
&& &&&&&&&&&// 小数&
&&&&&&&&&&& if (chByte &= L'0' && chByte &= L'9')&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& fDecimal += (chByte - L'0') * fDecP&
&&&&&&&&&&&&&&& fDecPower = fDecPower * 0.1;&
&&&&&&&&&&& }&
&&&&&&&&&&& else&
&&&&&&&&&&& {&
&&&&&&&&&&&&& &&return (bNegative? -(fInteger +& fDecimal): fInteger + fDecimal);&
&&&&&&&&&&& }&
&&&&&&& }&
&&&&&&& else&
&&&&&&& {&
&&&&&&&&&&& // 整数&
&&&&&&&&&&& if (chByte &= L'0' && chByte &= L'9')&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& fInteger = fInteger * 10.0 + chByte - L'0';&
&&&&&&&&&&& }&
&&&&&&&&&&& else if (chByte == L'.')&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& bDec =&
&&&&&&&&&&& }&
&&&&&&&&&&& else&
&&&&&&&&&&& {&
&&&&&&&&&&&&&&& return (bNegative? -fInteger : fInteger);&
&&&&&&&&&&& }&
&&&&&&& }&
&&&&&&& pSor++;&
&&& return (bNegative? -(fInteger +& fDecimal): fInteger + fDecimal);&
// -------------------------------------------------------------------------&
// $Log: $&
&&&&&&& 测试用例:
// StringToFloatShell.cpp : Defines the entry point for the console application.&
#include &stdafx.h&&
#include &StringToFloat.h&&
int _tmain(int argc, _TCHAR* argv[])&
&&& float aaaa = 0;&
&&& aaaa = StrToFloat(L&12.34&);&
&&& aaaa = StrToFloat(L&a23&);&
&&& aaaa = StrToFloat(L&1234&);&
&&& aaaa = StrToFloat(L&12.34&);&
&&& aaaa = StrToFloat(L&12.34.56&);&
&&& aaaa = StrToFloat(L&.34&);&
&&& aaaa = StrToFloat(L&34a&);&
&&& aaaa = StrToFloat(L&34a.456&);&
&&& aaaa = StrToFloat(L&-34&);&
&&& aaaa = StrToFloat(L&-56.34&);&
&&& aaaa = StrToFloat(L&-3.45.67&);&
&&& aaaa = StrToFloat(L&-.45.6a&);&
&&& aaaa = StrToFloat(L&-.&);&
&&& aaaa = StrToFloat(L&-0&);&
&&& return 0;&
&&&& &&&大家可以再找找BUG。
&&&&&&& [END]
摘自 magictong的专栏二次元同好交流新大陆
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
然后只好加上DecimalFormat了
import&javax.swing.JOptionPimport&java.text.DecimalFpublic&class&CutNum{&&&&public&static&void&main(String&args[])&&&&{&&&&&&&&//预先设置所能输出的小数的位数最长10&&&&&&&&DecimalFormat&twoDigits=new&DecimalFormat("0.##########");&&&&&&&&String&&&&&&&&&input=JOptionPane.showInputDialog("请输入一个浮点数:");&&&&&&&&double&cutFloat=Double.parseDouble(input);&&&&&&&&int&cutInt=(int)cutF&&&&&&&&JOptionPane.showMessageDialog(null,input+"的整数部分是"+cutInt+&&&&&&&&&&&&"\n小数部分是"+twoDigits.format((cutFloat-cutInt)));&&&&}}
为什么不用float?
Float类型的精度为8位,格式化串为"0.#####"123.22f整数部分3位,加上“#####“5位,一共8位,在精度范围内,故被格式化为123.22 1234.22f整数部分4位,加上“#####“5位,一共9位,超出精度范围,故被格式化为
double类型double e =
格式化串为"0.################################################################################", 一共80个“#”却一直是正确的,对于double在此时不检查是否超出精度
在用浮点类型数据时以double型为主,尽量少用float,double精度高于float
阅读(5927)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'输入一个浮点数,把它的整数部分和小数部分分别输出',
blogAbstract:'
莫名其妙的小数
最规矩的做法,就是将读取的浮点数强制转换成整数,即整数部分,再将浮点数减去整数,就得到小数部分了
import&javax.swing.JOptionPpublic&class&CutNum{&&&&public&static&void&main(String&args[])&&&&{&&&&&&&&String&&&&&&&&&input=JOptionPane.showInputDialog(\"请输入一个浮点数:\");',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:1,
publishTime:8,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!用scanf输入浮点数有问题
[问题点数:20分,结帖人redgict]
用scanf输入浮点数有问题
[问题点数:20分,结帖人redgict]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2010年7月 Linux/Unix社区大版内专家分月排行榜第三
2013年6月 Linux/Unix社区大版内专家分月排行榜第二2013年5月 Linux/Unix社区大版内专家分月排行榜第二2013年3月 Linux/Unix社区大版内专家分月排行榜第二2013年1月 Linux/Unix社区大版内专家分月排行榜第二2012年12月 Linux/Unix社区大版内专家分月排行榜第二2012年8月 Linux/Unix社区大版内专家分月排行榜第二2011年12月 Linux/Unix社区大版内专家分月排行榜第二2011年10月 C/C++大版内专家分月排行榜第二2011年10月 Linux/Unix社区大版内专家分月排行榜第二
2012年6月 C/C++大版内专家分月排行榜第三2012年6月 PHP大版内专家分月排行榜第三2012年5月 C/C++大版内专家分月排行榜第三2012年3月 Linux/Unix社区大版内专家分月排行榜第三2012年2月 Linux/Unix社区大版内专家分月排行榜第三2011年11月 C/C++大版内专家分月排行榜第三
2010年12月 C/C++大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 浮点数输出格式 的文章

 

随机推荐