二进制十进制互换,请大虾帮忙
假设输入一组包含32个的用0和1表示的二进制数的链
(a)写一段能把这个二进制数转化成对应的十进制表示
(b)给出一个链的一个以十进制为基础的整数的表示,并把这个整数转换成它的二进制表示。他的输出应该包含一个含有32个0和1的链来表示这个二进制数。
这得咋写啊?大虾们帮人帮到底,请给出代码及\\注释
问题点数:100、回复次数:11Top
1 楼BroncoSpeedCoursing(≡野马奔驰≡)回复于 2004-08-02 15:16:34 得分 0
//普通的转换,根据这个修改一下。
#include
#include
#include
using namespace std;
void main()
{
string binary; //string to hold the binary string
int bit; //translate a character bit to an int value
int decimal=0; //int to hold the decimal value
char read; //char to read through the binary string
/******** Input binary string *********/
cout << "Input a binary string: ";
cin >> binary;
/********************
* Go through the string, read a char, multiply with appropriate power of 2
* and keep increasing the decimal value.
**********************/
for(int i=0; i
{
read = binary[i];
if(read == '0')
bit = 0;
else
bit = 1;
decimal = decimal + ((pow(2,(binary.size() - i -1)))*bit);
}
cout << "The number is " << decimal << endl;
/******** Input decimal number *********/
cout << "\nEnter decimal number: ";
cin >> decimal;
binary=""; //initialize binary string
int max=0; //the max power of 2
i=0; //initiazlie i to 0;
int string_size=0; //keeping track of binary string value
/****** Find the max power of 2 **********/
while(max < decimal)
{
i++;
max = pow(2,i);
}
/************
* if the max power of 2 is exactly the value of decimal,
* then add a '1' to the binary string and i 0's.
************/
if(max == decimal)
{
binary = binary + '1';
for(int j=0;j
binary=binary+'0';
}
/************
* else add a '1' to the begining of the string,
* check if the next '1' would be still less than or equal
* to the decimal value. Keep doing that for i times.
************/
else
{
i=i-1;
binary=binary+'1';
string_size = string_size + (pow(2,i));
for(int j=i;j>0;j--)
{
i--;
if((string_size + pow(2,i)) <= decimal)
{
string_size = string_size + pow(2,i);
binary = binary + '1';
}
else
binary = binary + '0';
}
}
cout << "The binary string is: " << binary << endl;
cout << "\nEnd of demonstration!" << endl;
}
Top
2 楼keiy()回复于 2004-08-02 16:11:44 得分 0
如果是串<->值相互转的话:
ansi有标准函数:
二进制串->十进制值:
long strtol(const char *s, char **endptr, int radix);
ex:
#include <stdio.h>
int main(void)
{
char *string = "1010001", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 2);
printf("string = %s long = %ld\n", string, lnumber);
return 0;
}
反之,用:
int atoib ( const char *nptr,int base )
{ register const unsigned char *ptr = (const unsigned char *)nptr;
register unsigned num = 0;
register int c = *ptr;
register int neg = 0;
while ( isspace(c) ) c = *++ptr; /* skip over whitespace chars */
if ( c == '-' ) /* get an optional sign */
{ neg = 1;
c = *++ptr;
} else if ( c == '+' ) c = *++ptr;
while ( isdigit(c) )
{ num = ( base * num ) + ( c - '0' );
c = *++ptr;
}
if ( neg ) return ( -num );
return ( (int) num );
}
Top
3 楼keiy()回复于 2004-08-02 16:15:44 得分 0
不好意思,上面两个都是二进制串->十进制值:
反过来用下面的
char *_ltoa( long val,int base, int convup,char *buf) /* must be large enough to hold maximum number */
{
#define MAXBUF 15 /* max length of a converted value */
char *p, *str;
str = convup? "0123456789ABCDEF": "0123456789abcdef";
p = buf+MAXBUF-1;
*p = 0;
do { *--p = str[val % base]; } while( val /= base );
return p;
}
Top
4 楼talentyang(一枚硬币!)回复于 2004-08-02 16:27:46 得分 0
正确!Top
5 楼snow810211(阳光)回复于 2004-08-02 16:35:25 得分 0
我學習Top
6 楼findstone(我找石头)回复于 2004-08-02 16:43:58 得分 0
楼上的好强,学习!Top
7 楼lixunhua(李寻花)回复于 2004-08-03 01:07:09 得分 0
第一段程序有20余处错误,但是我不知如何修改Top
8 楼tuxw(醉书生)回复于 2004-08-03 01:42:27 得分 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 将 n 以二进制串存到 str
char* DtoB(char *str, unsigned long n)
{
int i;
unsigned long m = 0x80000000;
for(i = 0; i < 32; ++i)
{
str[i] = (m & n) ? '1' : '0';
m >>= 1;
}
str[32] = '\0';
return str;
}
// 从二进制串中读数
unsigned long BtoD(char *str)
{
int i;
unsigned long n = 0;
int len = strlen(str);
if (len > 32)
len = 32;
for(i = 0; i < len; ++i)
{
n += str[i] == '1' ? 1 : 0; // 非法字符认作 0
n <<= 1;
}
n >>= 1;
return n;
}
void main()
{
char str[33];
unsigned long n;
scanf("%d", &n);
puts(DtoB(str, n));
scanf("%s", str);
printf("%d\n", BtoD(str));
}
Top
9 楼tuxw(醉书生)回复于 2004-08-03 01:55:30 得分 0
不好意思,前面那个读二进制串的函数考虑不周,在输入32以上时如果最好位为 1,读出的结果最高位丢失,改了一下
// 从二进制串中读数
unsigned long BtoD(char *str)
{
int i;
unsigned long m;
unsigned long n = 0;
int len = strlen(str);
if (len > 32)
len = 32;
m = 0x80000000 >> (32 - len);
for(i = 0; i < len; ++i)
{
if (str[i] == '1') // 非法字符认作 0
n |= m;
m >>= 1;
}
return n;
}
Top
10 楼lixunhua(李寻花)回复于 2004-08-03 13:38:35 得分 0
醉书生,请问反过来呢?从十进制到二进制呢?Top
11 楼tuxw(醉书生)回复于 2004-08-03 19:51:43 得分 0
不是有两个函数吗,是十进制和二进制互转的Top




