PDA

View Full Version : سوال: دریافت عدد وتبدیل اون به باینری



rroona
جمعه 08 آبان 1388, 21:39 عصر
با سلام
دنبال کدهای برنامه ای هستم که یه عدد از ورودی دریافت کنه و اون رو به باینری تبدیل کنه ودر خروجی نمایش بده.
ممنون

shask00l
جمعه 08 آبان 1388, 22:31 عصر
از روش تقسیم متوالی استفاده کن . به یک حلقه ی while نیاز داری . عدد مبنای 10 رو اینقدر به 2 تقسیم کن تا خارج قسمت صفر بشه . اونوقت باقیمانده های بدست اومده رو از آخر به اول لیست کن . جواب همینه .

__Genius__
شنبه 09 آبان 1388, 00:36 صبح
// Convert a decimal integer do a binary string
// added a test printf() you can remove later
// Turbo C modified for Pelles C vegaseat 19nov2004

#include <stdio.h>

void dec2bin(long decimal, char *binary);

int main()
{
long decimal;
char binary[80];

printf("\n\n Enter an integer value : ");
scanf("%ld",&decimal);
dec2bin(decimal,binary);
printf("\n The binary value of %ld is %s \n",decimal,binary);

getchar(); // trap enter
getchar(); // wait
return 0;
}

//
// accepts a decimal integer and returns a binary coded string
//
void dec2bin(long decimal, char *binary)
{
int k = 0, n = 0;
int neg_flag = 0;
int remain;
int old_decimal; // for test
char temp[80];

// take care of negative input
if (decimal < 0)
{
decimal = -decimal;
neg_flag = 1;
}
do
{
old_decimal = decimal; // for test
remain = decimal % 2;
// whittle down the decimal number
decimal = decimal / 2;
// this is a test to show the action
printf("%d/2 = %d remainder = %d\n", old_decimal, decimal, remain);
// converts digit 0 or 1 to character '0' or '1'
temp[k++] = remain + '0';
} while (decimal > 0);

if (neg_flag)
temp[k++] = '-'; // add - sign
else
temp[k++] = ' '; // space

// reverse the spelling
while (k >= 0)
binary[n++] = temp[--k];

binary[n-1] = 0; // end with NULL
}

__Genius__
شنبه 09 آبان 1388, 00:37 صبح
C++ Version :



int a = 17;
std::cout << std::bitset<CHAR_BIT>( a ) << std::endl;

rroona
شنبه 09 آبان 1388, 23:00 عصر
از روش تقسیم متوالی استفاده کن . به یک حلقه ی while نیاز داری . عدد مبنای 10 رو اینقدر به 2 تقسیم کن تا خارج قسمت صفر بشه . اونوقت باقیمانده های بدست اومده رو از آخر به اول لیست کن . جواب همینه .
ممنون دوست عزیز
اگه ممکنه در مورد لیست کردن باقیمانده ها هم توضیح بدین
من فکر میکنم این کار با استفاده از آرایه راحت باشه اما میخواستم بدونم که بدون استفاده از آرایه هم این کار ممکنه و چطور؟؟
بازم از همتون ممنونم