C语言高手帮帮忙!超急问题!
来源:百度知道 编辑:UC知道 时间:2024/05/22 07:26:24
用C语言编初一个二进制和十进制相互转换的程序,只用循环,不用到数据结构!非常感谢
只用到for循环,谢谢!
只用到for循环,谢谢!
不懂再问我,邮箱mangohappy@qq.com 24小时内回复
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
void binarytodecimal( char *b, int b_count )
{
int result = 0;
for ( int i = b_count; i >= 0; i-- )
if ( b[i] == '1' )
result = result + pow( 2.0, b_count - i );
printf("你输入的二进制数对应的十进制数为:%d\n",result);
}
void decimaltobinary( int d )
{
int d_count = 0;
char p[20];
while ( d > 0 )
{
p[d_count++] = d % 2 + 48;
d = ( d - d % 2 ) / 2;
}
for ( int i = d_count - 1; i >= 0; i-- )
printf("%c",p[i]);
printf("\n");
}
void main()
{
char c;
int count = 0; //计算输入二进制数的长度
char binary[20]; //定义字符数字存放二进制数,最长可输入20位二进制数
int decimal;
printf("二进制转十进制请按B,十进制转二进制请按D.\n");
scanf("%c&