图中这道编程有问题,没考虑到小数部分,求证去
1条回答 默认 最新
- SeaTalks 2016-11-30 11:41关注
你自己调调格式吧
#include<stdio.h> //转化为2进制 void showbitZ(int n) { if (n > 1) { showbitZ(n / 2); } printf("%d", n % 2); } void showbitL(float f,int c) { if (c == 0)return; float twice = f * 2; int tmp = (int)twice; printf("%d", tmp); showbitL(twice - tmp, --c); } void showbit(int n, float f) { showbitZ(n); printf("."); showbitL(f,8); } //转化为8进制 void showOctZ(int n) { if (n > 7) { showOctZ(n / 8); } if (n == 0)return; printf("%d", n % 8); } void showOctL(float f, int c) { if (c == 0)return; float twice = f * 8; int tmp = (int)twice; printf("%d", tmp); showOctL(twice - tmp, --c); } void showOct(int n, float f) { showOctZ(n); printf("."); showOctL(f, 8); } //转化为16进制 char c_16[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; void showHexZ(int n) { if (n > 15) { showHexZ(n / 15); } if (n == 0)return; printf("%c", c_16[n % 15]); } void showHexL(float f, int c) { if (c == 0)return; float twice = f * 16; int tmp = (int)twice; printf("%c", c_16[tmp]); showOctL(twice - tmp, --c); } void showHex(int n, float f) { showHexZ(n); printf("."); showHexL(f, 8); } void main() { float shu = 0; int zhengshu = 0; printf("请输入一个十进制数:"); scanf("%f", &shu); zhengshu = (int)shu; shu -= zhengshu; showbit(zhengshu,shu); printf("\n"); showOct(zhengshu, shu); printf("\n"); showHex(zhengshu, shu); printf("\n"); }
解决 无用评论 打赏 举报