1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/*HEllo everybody. i would like to see your recommendations and your quotes.
how can i optimize this code?
what is wrong into the code?
what can it be improved?*/
/*4.28 (Printing the Decimal Equivalent of a Binary Number)
Input an integer containing only 0s and 1s (i.e., a “binary” integer)
and print its decimal equivalent. Use the remainder and division
operators to pick off the “binary” number’s digits one at a time from
right to left. Much as in the decimal number system, where the
rightmost digit has a positional value of 1, the next digit left has a
positional value of 10, then 100, then 1000, and so on, in the
binary number system the rightmost digit has a positional value of
1, the next digit left has a positional value of 2, then 4, then 8, and
so on. Thus the decimal number 234 can be interpreted as 2 * 100
+ 3 * 10 + 4 * 1. The decimal equivalent of binary 1101 is 1 * 1 + 0
* 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8, or 13.*/
//my answer
//from a binary to a decimal type.
//luis Fernando Pinzon.
//02/08/2018.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
//Initial variables.
int number;
int counter = 0;
int holodeckResidue = 0;
int holodeckQuotient;
int baseTen = 10;
//variables to determine the potential or the number to multiplying in accordance with the position of the digit.
int holodeckPotentialOne = 0;
int holodeckPotentialTwo = 1;
int potential = 0;
//store and join the value of the digits
int holodeckNumber = 0;
//amount variables
int combiner = 0;
int totalAmount = 0;
cout << "Conversion of binary to decimal" << endl;
cout << "Introduce a binary number: ";
cin >> number;
holodeckQuotient = number;
while(holodeckQuotient > counter)
{
//Separates the number in individual digits.
holodeckResidue = holodeckQuotient % baseTen;
holodeckQuotient = holodeckQuotient / baseTen;
cout << holodeckResidue << " "; //inverse input
//finds the potential or number for each location
potential = holodeckPotentialOne + holodeckPotentialTwo;
holodeckPotentialOne = potential;
holodeckPotentialTwo = potential;
cout << potential << " ";
//store the multiplication of the potential or number location.
holodeckNumber = holodeckResidue * potential;
cout << holodeckNumber << " ";
cout << "\n";
//add multiplitions up
combiner += holodeckNumber;
//cout << combiner ;
counter++;
}
//last location or position
//to add another position, therefore the last.
potential = holodeckPotentialOne + holodeckPotentialTwo;
//initialaze again
holodeckNumber = 0;
//last calculate
holodeckNumber = holodeckQuotient * potential;
cout << holodeckQuotient << " ";//first number...last location
cout << potential << " "; // last potential for last location
cout << holodeckNumber << endl; //last multiplication
totalAmount = combiner + holodeckNumber; //total amount, finally from binary to decimal type.
cout << "\n" << "you get in decimal system the next value: " << totalAmount << " back" << endl;
return 0;
}
|