Schreibe ein C++ Programm, das eine positive ganze Zahl z ¨uber eine
Kommandozeilen-Eingabe (cin) ¨ubergeben bekommt und die
Bin¨ardarstellung dieser Zahl auf der Kommandozeile ausgibt. Die Anzahl
der Bits n, die ben¨otigt werden um die Zahl z darzustellen kann mit
n =
log(z + 1)
log(2)
,
berechnet werden. Die Klammern bedeuten, dass das Ergebnis nach oben
gerundet werden soll, was in C++ mit der Funktion ceil aus dem
-Teil der Standardbibliothek implementiert ist. Nutze eine Schleife
um ¨uber die Bits zu iterieren und um zu bestimmen, ob ein Bit 0 oder 1
ist. Ein m¨oglicher Programmablauf k¨onnte wie folgt aussehen:
Eingabe Zahl: > 11
Ausgabe: 1011
STCE, Global¨ubung C++ 2
Kontrollfluss
Aufgaben II
3. Erweitere das Programm so, dass es auch eine (positive) Bin¨arzahl in eine
Dezimalzahl umwandeln kann. Frage daf¨ur am Anfang des Programms, ob
der Nutzer eine Bin¨arzahl zur Dezimalzahl umwandeln m¨ochte oder
umgekehrt. Falls eine Bin¨arzahl eingegeben werden soll wird der Nutzer
zuerst gefragt wie viele Bits die Bin¨arzahl hat. Anschließend sollen diese
nacheinander abgefragt werden. Ein m¨oglicher Programmablauf k¨onnte
wie folgt aussehen:
W¨ahle 0 f¨ur dec-to-bin, 1 f¨ur bin-to-dec: > 1
Anzahl der Bits: > 4
Bit: > 1
Bit: > 0
Bit: > 1
Bit: > 1
Ausgabe: 11
Assistant
Hier ist ein möglicher Code, der die Aufgabe erfüllt:
cpp
User
dont use the binary.length
Assistant
Oops, my apologies for that mistake. Here's the corrected code:
cpp
User
dont use the binary[]
Assistant
Apologies once again for the mistake. Here's the updated code without using binary.length() or binary[]:
cpp
User
optimize this
#include
#include
#include
using namespace std;
int main(){
int w;
cout << "Wähle 0 für dec-to-bin, 1 für bin-to-dec: ";
cin >> w;
code
if(w==0){
int z, numbits;
cout << "Die Binärdarstellung von: ";
cin >> z;
assert(z>0);
numbits=ceil(log(z+1)/log(2));
int bin;
int rest=z;
int potenz=numbits-1;
for(int i=0; i<numbits; i++){
if(rest>=pow(2,potenz)){
bin=1;
rest-=pow(2,potenz);
}
else bin=0;
potenz--;
cout << bin;
}
cout << endl;
}
else if(w==1){
int numbits;
cout << "Anzahl der Bits: ";
cin >> numbits;
int m=0;
int potenz=numbits-1;
int dec=0;
for(int i=0; i<numbits; i++){
int t;
cout << i+1 <<". Bit: ";
cin >> t;
assert(t==0 || t==1);
m=t*pow(2,potenz);
dec+=m;
potenz--;
}
cout << "Die Dezimalzahl ist: " << dec << endl;
}
else cout << "Ungültige Auswahl!" << endl;
return 0;
}
Assistant
Here's an optimized version of the code:
cpp
#include <iostream>
#include <cmath>
using namespace std;
int binToDec(string binary) {
int decimal = 0;
for (int i = 0; i < binary.length(); i++) {
if (binary[i] == '1') {
decimal += pow(2, binary.length() - 1 - i);
}
}
return decimal;
}
string decToBin(int decimal) {
int numBits = ceil(log(decimal + 1) / log(2));
string binary = "";
for (int i = numBits - 1; i >= 0; i--) {
int bit = (decimal >> i) & 1;
binary += to_string(bit);
}
return binary;
}
int main() {
int choice;
cout << "Wähle 0 für dec-to-bin, 1 für bin-to-dec: ";
cin >> choice;
if (choice == 0) {
int decimal;
cout << "Dezimalzahl eingeben: ";
cin >> decimal;
string binary = decToBin(decimal);
cout << "Binärdarstellung: " << binary << endl;
} else if (choice == 1) {
int numBits;
cout << "Anzahl der Bits: ";
cin >> numBits;
string binary = "";
for (int i = 0; i < numBits; i++) {
int bit;
cout << i + 1 << ". Bit: ";
cin >> bit;
binary += to_string(bit);
}
int decimal = binToDec(binary);
cout << "Dezimalwert: " << decimal << endl;
} else {
cout << "Ungültige Auswahl." << endl;
}
return 0;
}
Beachte, dass das Programm nur für positive ganze Zahlen funktioniert und keine Fehlerüberprüfung für die Eingaben implementiert ist.
#include <iostream>
#include <cmath>
using namespace std;
int binToDec(string binary, int numBits) {
int decimal = 0;
for (int i = 0; i < numBits; i++) {
if (binary[i] == '1') {
decimal += pow(2, numBits - 1 - i);
}
}
return decimal;
}
string decToBin(int decimal) {
int numBits = ceil(log(decimal + 1) / log(2));
string binary = "";
for (int i = numBits - 1; i >= 0; i--) {
int bit = (decimal >> i) & 1;
binary += to_string(bit);
}
return binary;
}
int main() {
int choice;
cout << "Wähle 0 für dec-to-bin, 1 für bin-to-dec: ";
cin >> choice;
if (choice == 0) {
int decimal;
cout << "Dezimalzahl eingeben: ";
cin >> decimal;
string binary = decToBin(decimal);
cout << "Binärdarstellung: " << binary << endl;
} else if (choice == 1) {
int numBits;
cout << "Anzahl der Bits: ";
cin >> numBits;
string binary = "";
for (int i = 0; i < numBits; i++) {
int bit;
cout << i + 1 << ". Bit: ";
cin >> bit;
binary += to_string(bit);
}
int decimal = binToDec(binary, numBits);
cout << "Dezimalwert: " << decimal << endl;
} else {
cout << "Ungültige Auswahl." << endl;
}
return 0;
}
Now, the numBits variable is being used instead of binary.length() wherever necessary. This ensures that the correct number of bits is considered during conversions.
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int binToDec(vector<int>& binary, int numBits) {
int decimal = 0;
for (int i = 0; i < numBits; i++) {
if (binary[i] == 1) {
decimal += pow(2, numBits - 1 - i);
}
}
return decimal;
}
void decToBin(int decimal) {
int numBits = ceil(log(decimal + 1) / log(2));
vector<int> binary(numBits, 0);
for (int i = numBits - 1; i >= 0; i--) {
binary[i] = (decimal >> i) & 1;
}
cout << "Binärdarstellung: ";
for (int i = 0; i < numBits; i++) {
cout << binary[i];
}
cout << endl;
}
int main() {
int choice;
cout << "Wähle 0 für dec-to-bin, 1 für bin-to-dec: ";
cin >> choice;
if (choice == 0) {
int decimal;
cout << "Dezimalzahl eingeben: ";
cin >> decimal;
decToBin(decimal);
} else if (choice == 1) {
int numBits;
cout << "Anzahl der Bits: ";
cin >> numBits;
vector<int> binary(numBits);
for (int i = 0; i < numBits; i++) {
cout << i + 1 << ". Bit: ";
cin >> binary[i];
}
int decimal = binToDec(binary, numBits);
cout << "Dezimalwert: " << decimal << endl;
} else {
cout << "Ungültige Auswahl." << endl;
}
return 0;
}
In this updated version, I've used vector<int> to store the binary bits instead of a string. This allows us to access individual bits using indexing without relying on the binary.length(). The decToBin() function now directly outputs the binary representation instead of returning a string.
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
void decToBin(int decimal) {
int numbits = ceil(log2(decimal + 1));
for (int i = numbits - 1; i >= 0; i--) {
int bit = (decimal >> i) & 1;
cout << bit;
}
cout << endl;
}
int binToDec(int numbits) {
int decimal = 0;
for (int i = numbits - 1; i >= 0; i--) {
int bit;
cout << i + 1 << ". Bit: ";
cin >> bit;
assert(bit == 0 || bit == 1);
decimal += bit * pow(2, i);
}
return decimal;
}
int main() {
int w;
cout << "Wähle 0 für dec-to-bin, 1 für bin-to-dec: ";
cin >> w;
if (w == 0) {
int z;
cout << "Dezimalzahl eingeben: ";
cin >> z;
assert(z > 0);
decToBin(z);
} else if (w == 1) {
int numbits;
cout << "Anzahl der Bits: ";
cin >> numbits;
int decimal = binToDec(numbits);
cout << "Dezimalwert: " << decimal << endl;
} else {
cout << "Ungültige Auswahl!" << endl;
}
return 0;
}
In this optimized version, the log2() function is used to calculate the number of bits needed to represent the decimal number. The assert statement is used to validate user inputs. The decToBin() and binToDec() functions are simplified to directly calculate and output the binary and decimal representations, respectively.