/*************************************************************************
> File Name: test.cpp
> Author: Damon
> Mail: thydamon@gmail.com
> Created Time: Fri 05 Jun 2015 02:28:03 AM PDT
************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
void rever(char *pIn, char *pOut)
{
char *pTin = pIn;
char *pTout = pOut;
int i = 0, j = 0;
while (pTin[i] != '*')
{
i++;
}
while (i--)
{
pOut[j] = pIn[i];
j++;
}
pOut[j] = '*';
}
void putArr(char *nIn)
{
int i = 0;
while (nIn[i] != '*')
{
printf("%c ", nIn[i]);
i++;
}
printf("\n");
}
void TentoTw(const int nIn, char *nOut)
{
int nTmp = nIn;
char *pTmp = nOut;
int i = 0;
while (nTmp)
{
// printf("%d ", nTmp/2);
pTmp[i] = nTmp%2 + 48;
// printf("%d\n", *pTmp);
// pTmp++;
i++;
nTmp = nTmp/2;
}
pTmp[i] = '*';
}
void TwtoTen(char *pIn)
{
char *pTin = pIn;
int i = 0, num = 0, j = 0;
while (pTin[j] != '*')
j++;
//printf("%d\n",j);
while (pTin[i] != '*')
{
printf("%c ",pTin[i]);
num = num+(pTin[i] - 48)*pow(2,j-i-1);
i++;
}
printf("\n");
printf("%d\n",num);
}
int main(int argc, char* argv[])
{
int in = 0;
char str[32] = {0};
char fstr[32] = {0};
printf("please input a integer:");
scanf("%d", &in);
TentoTw(in, str);
putArr(str);
rever(str,fstr);
putArr(fstr);
TwtoTen(fstr);
return 0;
}
g++ test.cpp -o test -lm 编译即可。
本文介绍了一个简单的C++程序,该程序能够将十进制整数转换为二进制表示,并对其进行反转操作。此外,还展示了如何将反转后的二进制字符串再转换回十进制数。
1万+

被折叠的 条评论
为什么被折叠?



