Έγραψα ένα 'απλό' (μου πήρε 30 λεπτά) πρόγραμμα που μετατρέπει δεκαδικό αριθμό σε δυαδικό. Είμαι ΣΙΓΟΥΡΟΣ ότι υπάρχει πολύ πιο απλός τρόπος, μπορείτε να μου τον δείξετε; Ορίστε ο κώδικας:
#include <iostream>
#include <stdlib.h>
using namespace std;
int a1, a2, remainder;
int tab = 0;
int maxtab = 0;
int table[0];
int main()
{
system("clear");
cout << "Enter a decimal number: ";
cin >> a1;
a2 = a1; //we need our number for later on so we save it in another variable
while (a1!=0) //dividing by two until we hit 0
{
remainder = a1%2; //getting a remainder - decimal number(1 or 0)
a1 = a1/2; //dividing our number by two
maxtab++; //+1 to max elements of the table
}
maxtab--; //-1 to max elements of the table (when dividing finishes it adds 1 additional elemnt that we don't want and it's equal to 0)
a1 = a2; //we must do calculations one more time so we're gatting back our original number
table[0] = table[maxtab]; //we set the number of elements in our table to maxtab (we don't get 10's of 0's)
while (a1!=0) //same calculations 2nd time but adding every 1 or 0 (remainder) to separate element in table
{
remainder = a1%2; //getting a remainder
a1 = a1/2; //dividing by 2
table[tab] = remainder; //adding 0 or 1 to an element
tab++; //tab (element count) increases by 1 so next remainder is saved in another element
}
tab--; //same as with maxtab--
cout << "Your binary number: ";
while (tab>=0) //until we get to the 0 (1st) element of the table
{
cout << table[tab] << " "; //write the value of an element (0 or 1)
tab--; //decreasing by 1 so we show 0's and 1's FROM THE BACK (correct way)
}
cout << endl;
return 0;
}
Παρεμπιπτόντως, είναι πολύπλοκος, αλλά προσπάθησα να κάνω ό,τι καλύτερο μπορούσα.
edit - Εδώ είναι η λύση που κατέληξα να χρησιμοποιήσω:
std::string toBinary(int n)
{
std::string r;
while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;}
return r;
}
Το std::bitset
έχει μια μέθοδο .to_string()
που επιστρέφει ένα std::string
που περιέχει μια αναπαράσταση κειμένου σε δυαδικό σύστημα, με συμπλήρωση με μηδενικά.
Επιλέξτε το πλάτος του bitset όπως απαιτείται για τα δεδομένα σας, π.χ. std::bitset<32>
για να λάβετε συμβολοσειρές 32 χαρακτήρων από ακέραιους αριθμούς 32 bit.
#include <iostream>
#include <bitset>
int main()
{
std::string binary = std::bitset<8>(128).to_string(); //to binary
std::cout<<binary<<"\n";
unsigned long decimal = std::bitset<8>(binary).to_ulong();
std::cout<<decimal<<"\n";
return 0;
}
EDIT: Παρακαλώ μην επεξεργαστείτε την απάντησή μου για το Οκταδικό και το Εξαδικό. Το OP ζήτησε συγκεκριμένα για δεκαδικό σε δυαδικό.
Μια μεταβλητή int
δεν είναι δεκαδική, είναι δυαδική. Αυτό που ψάχνετε είναι μια δυαδική συμβολοσειρά αναπαράστασης του αριθμού, την οποία μπορείτε να πάρετε εφαρμόζοντας μια μάσκα που φιλτράρει τα μεμονωμένα bit και στη συνέχεια να τα εκτυπώσετε:
for( int i = sizeof(value)*CHAR_BIT-1; i>=0; --i)
cout << value & (1 << i) ? '1' : '0';
Αυτή'είναι η λύση αν η ερώτησή σας είναι αλγοριθμική. Αν όχι, θα πρέπει να χρησιμοποιήσετε την κλάση std::bitset για να το χειριστείτε αυτό για εσάς:
bitset< sizeof(value)*CHAR_BIT > bits( value );
cout << bits.to_string();
Θέλετε να κάνετε κάτι τέτοιο:
cout << "Enter a decimal number: ";
cin >> a1;
cout << setbase(2);
cout << a1