编写C++代码实现二进制浮点数输入,显示其十进制数值结果,所有5类(可以是0,非正规化数,正规化数,无穷,NaN)浮点结果都要进行测试和结果显示。输入为一个32位的数据表示一个32位的二进制浮点数
编写C++代码实现二进制浮点数输入,显示其十进制数值结果,所有5类(可 以是0,非正规化数,正规化数,无穷,NaN)浮点结果都要进行测试和结果显示输入为一个32位的数据表示一个32位的二进制浮点数
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 赵4老师 2022-05-11 09:20关注
仅供参考:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { float f; double d; char bs[65]; char b[65]; char s[80]; unsigned char *p; char e[12]; char *t; int ex; int flag; flag=0; while (1) { printf("Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:");fflush(stdout); rewind(stdin); fgets(s,80,stdin); if ('\n'==s[0]) return 1; if (1==sscanf(s,"0x%16I64X",(__int64 *)&d) && strlen(s)>11) {flag=2;break;} if (1==sscanf(s,"0x%8X" ,( int *)&f)) {flag=1;break;} if (1==sscanf(s,"%f" , &f) && 1==sscanf(s,"%lf" , &d)) {flag=3;break;} } if (flag&1) { printf("f=%g\n",f); p=(unsigned char *)&f; printf("hex=%02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]); ltoa(*(long *)&f,b,2); sprintf(bs,"%032s",b); printf("bin=%s\n",bs); printf("bin=%.1s %.8s %s\n",bs,bs+1,bs+9); strncpy(e,bs+1,8);e[8]=0; ex=strtol(e,&t,2); printf(" %c %-4d-127 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9); ex-=127; printf(" %c %-8d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9); } if (flag&2) { printf("\nd=%lg\n",d); p=(unsigned char *)&d; printf("hex=%02X %02X %02X %02X %02X %02X %02X %02X\n",p[7],p[6],p[5],p[4],p[3],p[2],p[1],p[0]); _i64toa(*(__int64 *)&d,b,2); sprintf(bs,"%064s",b); printf("bin=%s\n",bs); printf("bin=%.1s %.11s %s\n",bs,bs+1,bs+12); strncpy(e,bs+1,11);e[11]=0; ex=strtol(e,&t,2); printf(" %c %-6d-1023 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12); ex-=1023; printf(" %c %-11d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12); } return 0; } //Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:0x3FC0000000000000 // //d=0.125 //hex=3F C0 00 00 00 00 00 00 //bin=0011111111000000000000000000000000000000000000000000000000000000 //bin=0 01111111100 0000000000000000000000000000000000000000000000000000 // + 1020 -1023 1.0000000000000000000000000000000000000000000000000000 // + -3 1.0000000000000000000000000000000000000000000000000000 // //Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:0x3E000000 // //f=0.125 //hex=3E 00 00 00 //bin=00111110000000000000000000000000 //bin=0 01111100 00000000000000000000000 // + 124 -127 1.00000000000000000000000 // + -3 1.00000000000000000000000 // //Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:0.125 //f=0.125 //hex=3E 00 00 00 //bin=00111110000000000000000000000000 //bin=0 01111100 00000000000000000000000 // + 124 -127 1.00000000000000000000000 // + -3 1.00000000000000000000000 // //d=0.125 //hex=3F C0 00 00 00 00 00 00 //bin=0011111111000000000000000000000000000000000000000000000000000000 //bin=0 01111111100 0000000000000000000000000000000000000000000000000000 // + 1020 -1023 1.0000000000000000000000000000000000000000000000000000 // + -3 1.0000000000000000000000000000000000000000000000000000 // //Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX: //
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=vs-2019 // https://docs.microsoft.com/zh-cn/cpp/porting/visual-cpp-change-history-2003-2015?view=vs-2019 // // Starting in Visual Studio 2015, if the argument that corresponds to a floating-point conversion specifier (a, A, e, E, f, F, g, G) is infinite, // indefinite, or NaN, the formatted output conforms to the C99 standard. This table lists the formatted output: // Value Output // -------------- --------- // infinity inf // Quiet NaN nan // Signaling NaN nan(snan) // Indefinite NaN nan(ind) // Any of these values may be prefixed by a sign. If a floating-point type conversion specifier character is a capital letter, // then the output is also formatted in capital letters. For example, if the format specifier is %F instead of %f, an infinity // is formatted as INF instead of inf. The scanf functions can also parse these strings, so these values can make a round trip // through printf and scanf functions. // // Before Visual Studio 2015, the CRT used a different, non-standard format for output of infinite, indefinite, and NaN values: // Value Output // ------------------------------ ------------------------- // + infinity 1.#INF random-digits // - infinity -1.#INF random-digits // Indefinite (same as quiet NaN) digit .#IND random-digits // NaN digit .#NAN random-digits // Any of these may have been prefixed by a sign, and may have been formatted slightly differently depending on field width and precision, // sometimes with unusual effects. For example, printf("%.2f\n", INFINITY) would print 1.#J because the #INF would be "rounded" to 2 digits of precision. #include <stdio.h> #include <float.h> int main() { int r; double d1=-1.0,d2=-2.0; printf("input two double (d1 d2):"); r=scanf("%lf%lf",&d1,&d2); printf("scanf return:%d d1:%lg d2:%lg\n",r,d1,d2); printf("_finite(d1):%d\n",_finite(d1)); printf("_fpclass(d1):0x%04X\n",_fpclass(d1)); return 0; } // input two double (d1 d2):nan 22 // scanf return:2 d1:nan d2:22 // _finite(d1):0 // _fpclass(d1):0x0002 // // input two double (d1 d2):nan(snan) 22 // scanf return:2 d1:nan(snan) d2:22 // _finite(d1):0 // _fpclass(d1):0x0002 // // input two double (d1 d2):nan(ind) 22 // scanf return:2 d1:-nan(ind) d2:22 // _finite(d1):0 // _fpclass(d1):0x0002 // // input two double (d1 d2):-inf 22 // scanf return:2 d1:-inf d2:22 // _finite(d1):0 // _fpclass(d1):0x0004
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
- ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
- ¥15 yolov9的训练时间
- ¥15 二叉树遍历没有报错但无法正常运行
- ¥15 在linux系统下vscode运行robocup3d上场球员报错
- ¥15 Python语言实验
- ¥15 SAP HANA SQL 增加合计行
- ¥20 用C#语言解决一个英文打字练习器,有偿
- ¥15 srs-sip外部服务 webrtc支持H265格式
- ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动