Binary to Decimal W/ the presentation of equation

Im trying to make a program that converts binary to decimal but having the needs to show the process of the convertion (Sample image below)

https://ibb.co/jyptyxw

here is my current code
i have no idea how to show the process of the conversion like in the given image.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
     int bin, dec = 0, remainder, num, base = 1;
     cout << "Enter the binary number: ";
     cin >> num;
     bin = num;
     while (num > 0)
     {
         rememainder = num % 10; //get the last digit of the input
         dec = dec + remainder * base;
         base = base * 2;
         num = num / 10;
     }
     cout << "Decimal Value" << bin << " : " << dec << endl;
     return 0;

}
Last edited on
Make a blank string for the output.

In your code, the first time you get the last digit, that's 2^0.
In your code, the second time you get the last digit, that's 2^1.
In your code, the third time you get the last digit, that's 2^2.
In your code, the fourth time you get the last digit, that's 2^3.
In your code, the fifth time you get the last digit, that's 2^4.
And so on.

So each time you get the last digit, if it's a 1, put:
1*(2^ whichever) +
on the front of the output string.

When you're done, show the output string.
Last edited on
 
1*(2^ whichever) +


in this block what variable is 'whichever'
should i use a forloop to counter the exponential increase while checking if its a 0 or a 1 with if condition?
whichever is the variable you are using to keep track of how many times you've fetched the last digit.
Im actually near to getting it but im stuck with the counter showing up ascending rather than descending.

im having this:
https://ibb.co/XsXvnMp
while it should be like this:
https://ibb.co/jyptyxw

this is my current code :
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
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
     int bin, dec = 0, remainder, num, base = 1,counter = 0;
     cout << "Enter the binary number: ";
     cin >> num;
     bin = num;
     while (num > 0)
     {
         remainder = num % 10; //get the last digit of the input
         dec = dec + remainder * base;
         base = base * 2;
         num = num / 10;
         counter++;
         if (num % 10 == 1)
         {
         	cout << "1*(2^" << counter << ") ";
		 }
     }
     
     cout << "\nThe decimal equivalent of " << bin << " : " << dec << endl;
     return 0;

}
Make a blank string for the output.

In your code, the first time you get the last digit, that's 2^0.
In your code, the second time you get the last digit, that's 2^1.
In your code, the third time you get the last digit, that's 2^2.
In your code, the fourth time you get the last digit, that's 2^3.
In your code, the fifth time you get the last digit, that's 2^4.
And so on.

So each time you get the last digit, if it's a 1, put:
1*(2^ whichever) +
on the front of the output string.


When you're done, show the output string.
Im sorry but i dont get it,
i cant store a counter which is an integer inside a string variable for the output
what do you mean in front of the output string?
Last edited on
i cant store a counter which is an integer inside a string variable for the output

http://www.cplusplus.com/reference/string/to_string/



1
2
3
4
5
6
string output; // Create a new string. Blank

// Each time round the loop, put new letters on the front 
output = string("+ 1*(2^") + to_string(counter) + ") " + output;

// when all done, remove leading '+' from string and output the string 
Last edited on
is there another way to do this without using the to_string function?
you can do a lookup table of all 255 byte combinations without using to-string. Just be endian aware. You can do it off nibbles if you prefer, its just a bit more weird, might use a bit field struct over a byte to do it..
Last edited on
I got it but having trouble fixing the value in the middle part
expected output:
1*(2^4) + 1*(2^2) + 1*(2^1)
what im having:
https://ibb.co/Z8hK34M

this is my current code
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
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

int main()
{
     int bin, dec = 0, remainder, num, base = 1,counter=0,counter2=0, constnum;
     cout << "Enter the binary number: ";
     cin >> num;
     bin = num;
     constnum = num;
     while(bin > 0)
     {
     	bin=bin/10;
		counter2++;
	 }
	 
     while (num > 0)
     {
     	if (num % 10 == 1) {
		    cout << " 1*(2^" << counter2 << ") +";
		    counter2--;	
			}
		 else if(num % 10 == 0) {
		 	counter2--;
		 }
         remainder = num % 10; //get the last digit of the input
         dec = dec + remainder * base;
         base = base * 2;
         num = num / 10;
         
	}

     cout << "\nThe decimal equivalent of " << constnum << " : " << dec << endl;
     return 0;

}
Last edited on
Don't output the final '+' on the last time round the loop
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

string mathSum( const string &bin )
{
   int n = bin.size();   if ( !n ) return "";
   string rem = bin.substr( 1 );
   return ( bin[0]=='1' ? "2^" + to_string(n-1) + ( rem.find('1') != string::npos ? " + " : "" ) : "" ) + mathSum( rem );
}


int main()
{
   string s;
   s = "1011010";            cout << s << " = " << mathSum( s ) << '\n';
   s = "101101101101101";    cout << s << " = " << mathSum( s ) << '\n';
}


or non-recursive version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

string mathSum( const string &bin )
{
   string result;
   int n = bin.size();
   for ( int i = 0; i < n; i++ )
   {
      if ( bin[n-1-i] == '1' ) result = "2^" + to_string( i ) + ( result != "" ? " + " : "" ) + result;
   }
   return result;
}


int main()
{
   string s;
   s = "1011010";            cout << s << " = " << mathSum( s ) << '\n';
   s = "101101101101101";    cout << s << " = " << mathSum( s ) << '\n';
}


1011010 = 2^6 + 2^4 + 2^3 + 2^1
101101101101101 = 2^14 + 2^12 + 2^11 + 2^9 + 2^8 + 2^6 + 2^5 + 2^3 + 2^2 + 2^0
Last edited on
Topic archived. No new replies allowed.