函数+进制转换器v1.0beta
1.程序的运行截图
2.pass
3.介绍函数及其功能
(1)将二进制数转换为八进制和十进制数的函数
void twochange(int x) { //二转八 //整数部分 int x1, x2 = x, x3, cnt = 0, mount, s1 = 0, s2 = 0; while (x2 != 0) { x2 /= 10; cnt++; } x2 = x; if (cnt % 3 == 0) { mount = cnt; x2 = x; } else if(cnt%3==1){ mount += 2; x2 *= pow(10, 2); } else if (cnt % 3 == 2) { mount += 1; x2 *= pow(10, 1); } mount /= 3; while (x2 != 0) { x1 = x2 % 1000; s1 += x1 * pow(1000, mount - 1); x2 /= 1000; mount--; } printf("八进制:"); while (s1 != 0) { x1 = s1 % 1000;//划分 s1 /= 1000; int n = 0; while (x1 != 0) {//三个二进制数 x3 = x1 % 10; s2 += x3 * pow(2, n); x1 /= 10; n++; } printf("%d", s2); s2 = 0; } printf("\n"); //小数部分 //二转十 //整数部分 int x11, x21 = x, x31, cnt1 = 0, s11 = 0, i = 0; while (x21 != 0) { x11 = x21 % 10; s11 += x11 * pow(2, i); i++; x21 /= 10; } printf("十进制:%d", s11); //小数部分 }
(2)将八进制数转为二进制和十进制数的函数
void eightchange(int x) { //八转二 //整数部分 int x1, x2 = x, x3, s1 = 0, s2 = 0, i, cnt = 0; printf("二进制:"); while (x2 != 0) { x2 /= 10; cnt++; } x2 = x; while (x2 != 0) { x1 = x2 % 10; s1 += x1 * pow(10, cnt - 1); x2 /= 10; cnt--; } x2 = s1; while (x2 != 0) { x1 = x2 % 10; i = 0; while (x1 != 0) { x3 = x1 % 2; s2 += x3 * pow(10, i); i++; x1 /= 2; } printf("%03d", s2); s2 = 0; x2 /= 10; } printf("\n"); //小数部分 //八转十 //整数部分 int x11, x21 = x, x31, s11=0; cnt = 0; i = 0; while (x21 != 0) { x11 = x21 % 10; s11 += x11 * pow(8, i); i++; x21 /= 10; } printf("十进制:%d", s11); //小数部分 }
(3)将十进制数转为二进制和八进制数的函数
void tenchange(int x) { //十转二 //整数部分 int x1, x2 = x,s1=0,i=0; while (x2 != 0) { x1 = x2 % 2; s1 += x1 * pow(10, i); i++; x2 /= 2; } printf("二进制:%d\n", s1); //小数部分 //十转八 //整数部分 int s2; i = 0; s1 = 0; x2 = x; while (x2 != 0) { x1 = x2 % 8; s1 += x1 * pow(10, i); i++; x2 /= 8; } printf("八进制:%d", s1); //小数部分 }
4.main函数
int main()
{
int number, x;
//输入
printf("(输入1表示二进制,输入2表示八进制,输入3表示十进制)\n请选择要输入的数的进制:");
scanf("%d", &number);
printf("请输入一个十位以内正整数:");
scanf("%d", &x);
int i,x1,x2 = x,cnt=0,p;
while (x2 != 0) {
x2 /= 10;
cnt++;
}
x2 = x;
x1 = x2 % 10;
x2 /= 10;
if (x1 >= 0 && x1 <= 1) {
x1 = x2 % 10;
for (i = 2; x2 != 0;i++) {
if (x1 >= 0 && x1 <= 1) {
x2 /= 10;
}
else {
break;
}
}
}
else if (x1 >= 0 && x1 <= 7) {
x1 = x2 % 10;
for (i = 2; x2 != 0; i++) {
if (x1 >= 0 && x1 <= 1) {
x2 /= 10;
}
else {
break;
}
}
}
else if (x1 >= 0 && x1 <= 9) {
x1 = x2 % 10;
for (i = 2; x2 != 0; i++) {
if (x1 >= 0 && x1 <= 1) {
x2 /= 10;
}
else {
break;
}
}
}
if (number == 1&&i>cnt) {
printf("二进制转成\n");
twochange(x);
}
else if (number == 2 && i > cnt) {
printf("八进制转成\n");
eightchange(x);
}
else if (number == 3 && i > cnt) {
printf("十进制转成\n");
tenchange(x);
}
return 0;
}
5.思维导图
6.碰到的问题:
怎么判断二进制,八进制,十进制;
解决方案:
通过输入控制进制,避免了复杂的长段代码拖延运行效率
怎么保证小数不会数据丢失0.010000实际显示0.009999
解决方案:
没弄懂。。。待完善
7.代码互评
杨涛(拒绝平庸*)的代码
void Numchange(int m, int b)//将10进制数转化为任意进制数 { int n = m; int count = 0; if (m == 0) printf("0"); while (n != 0) { n = n / b; count++; } int number; for (int i = count; i >= 1; i--) { number = m / (int)pow(b, i - 1); if (number < 10) { printf("%d", number); } else { printf("%c", number + 55); } m = m % (int)pow(b, i - 1); } }
我的代码只能将10进制特定转换另外两个进制,人家的能随机转成任意进制,不得不说很值得学习,他的代码输入了两个值,而我的只传入一个值,在这点上造成了我的代码只能单纯的进行特定转换的缺陷。
8.
对函数的理解:简化计算,提高运行效率