سلام
یه برنامه برای تبدیل اعداد بین 0 تا 1 از مبنای 10 به مبنای 2 توسط c یا c++ هرچی ساده تر بهتر
لطفا یه نفر compile نشده رو اینجا بزاره
سلام
یه برنامه برای تبدیل اعداد بین 0 تا 1 از مبنای 10 به مبنای 2 توسط c یا c++ هرچی ساده تر بهتر
لطفا یه نفر compile نشده رو اینجا بزاره
*->کتاب برنامه هاي آموزشي سي و سي++
انتشارات ديباگران
یه نفر اصل برنامه c این تمرین رو اینجا بزاره هرچی ساده تر بهتر.
روش كار اينه :
عدد را هي در 2 ضرب ميكينيم و قسمت صحيش را هربار به عنوان يكي از ازقام تبديل مبنا شده نگه ميداريم
و قسمت اعشاري عدد حاصل رو دوباره در دو ضرب ميكنيم و ال آخر
همين ! تا كي ؟ تا وقتي كه قسمت صحيح به صفر برسه ... اگه نرسيد چي ؟ تا جايي ادامه ميدهيم كه به تقريب دلخواه رسيده باشيم مثلا بگيم تا پنج رقم اعشار
مثال :
0.75 را ميخوايم تبديل كنيم به مبناي دو
خب اول در دو ضرب كنيم ميشه 1.5 حالا 1 را نگه ميداريم بعنوان اولين رقم تبديل يافته و 0.5 ئش رو دوباره در 2 ضرب ميكنيم .. ميشه 1.0 .. حالا دوباره 1 را نگه ميداريم بعنوان دومين عدد تبديل يافته . حالا چي رو بايد اين دفعه در دو ضرب كنيم ؟ صفر را ...پس تموم شد
يعني 0.11 ميشه تبديل يافته در مبناي 2
[LEFT]// 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
}[/LEFT]
#include <iostream>
using namespace std;
/*------------------------------------------
/ This program converts numbers into base-2 representation
/ could be optimized to handle better.
/ mainly illustrates the algorithm that is used for conversion.
/ Ervin J. Obando - 2005 - for personal use only.
/ please feel free to use code as you'd like, mail me for anything at [email protected]
/--------------------------------------------*/
// function prototype
void decBin(unsigned int srcData, unsigned int *destData);
// start code
int main()
{
// vars - holds user values
char number[12] = {0};
double fNumber = 0;
// integer array to hold final data
unsigned int arrNum[32];
// loop - prog start
while(true)
{
cout << "Enter number (0 to exit or just enter): ";
cin.getline(number, 11);
// check empty, break if empty - basic behavior - change to allow retry
if(strlen(number) == 0)
{
break;
}
// convert to usable data
fNumber = atof(number);
// allow user to end by entering 0 - since binary value is same anyway
if(fNumber == 0)
{
break;
}
// use function - cast it to integer
decBin(static_cast<int>(fNumber), arrNum);
// out result - looping through array - some formatting for readability
for(int i = 0; i < sizeof(arrNum)/sizeof(arrNum[0]); i++)
{
cout << arrNum[i];
if(i == 7 || i == 15 || i == 23 || i == 31)
{
cout << " ";
}
}
cout << endl << endl;
}
cout << endl;
// norm prog term
return 0;
}
// function definition
void decBin(unsigned int srcData, unsigned int *destData)
{
// declare a few vars - holders of data
const unsigned int numOfElements = 32;
int evenOddCheck = 0;
unsigned int binaryNumber[numOfElements];
// index must be set to -1 to get to array element 0
int index = numOfElements - 1;
// convert number by checking even/odd-ness, if even out 0, if odd out 1
while(srcData != 0)
{
evenOddCheck = srcData % 2;
if(evenOddCheck == 0)
{
binaryNumber[index] = 0;
}
else
{
binaryNumber[index] = 1;
}
srcData /= 2;
index--;
}
// pad binary string with 0's - if necessary
while(index < numOfElements)
{
binaryNumber[index--] = 0;
}
// write result to destination array
while(index + 1 <= numOfElements)
{
int i = index;
destData[index++] = binaryNumber[i++];
}
}