十进制转二进制-使用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);
}