您的位置:首页 > 其它

十进制转二进制-使用do while 、while 、for循环实现-C描述

2017-02-10 10:03 239 查看
十进制转二进制-使用do while 、while 、for循环实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//位转换
/*
6%2 = 0
3%2 = 1
1%2 = 1
*/
void 十进制转二进制() {
printf("输入十进制\n");
int  *pNum = (int*)malloc(1 * sizeof(int));
scanf("%d", pNum);
//数组
int * pResult = (int *)malloc(1000 * sizeof(int));
printf("二进制是:\n");
int count = 0;
/*do {			//使用do while方式
*(pResult + count) = *pNum % 2;
printf("count = %d\n", count);
count++;
*pNum = *pNum / 2;
} while (*pNum);*/

//while (*pNum) {	//使用 while方式
//	*(pResult + count) = *pNum % 2;
//	printf("count = %d\n", count);
//	count++;
//	*pNum = *pNum / 2;
//}
for (; *pNum; count++) { //使用For循环方式
*(pResult + count) = *pNum % 2;
printf("count = %d\n", count);
*pNum = *pNum / 2;
}
printf("count->%d\n", count);
for (int i = count - 1; i >= 0; i--) {
printf("%d", *(pResult + i));
}
printf("\n");
free(pNum);
free(pResult);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐