Write a
program to convert any number in the decimal system (base 10) to a number in the binary system (base 2)
or vice verse.
(Hint: in main function, you give a menu to let
user select either converting from base 10 to base 2 or base 2 to base 10.)
Note:
I. Use stack concepts to write a function to convert any number
in the decimal system (base 10)
to a number in the binary system (base 2)
1. Keep dividing the
number by 2 and replace the number by quotient until the number is zero.
2. Push the remainder to a stack each
time.
3. Pop all elements from the stack to
a vector.
II. Do the method of converting a
number in the binary system to a number in the decimal system in
recursive way.
Your function
header is followed:
int Bin2Dec(string bin);
The following is a hint:
Mbin('0') = 0
Mbin('1') = 1
Mbin(<bin_num>'0') = 2 * Mbin(<bin_num>)
Mbin(<bin_num>'1') = 2 * Mbin(<bin_num>) + 1example:
Mbin("10110")=2*Mbin("1011")
=2*[2*Mbin("101")+1]
=2*{2*[2*Mbin("10")+1]+1}
=2*{2*[2*(2*Mbin('1'))+1]+1}
=2*{2*[2*(2*1)+1]+1}
=2*{2*[2*2+1]+1}
=2*{2*5+1}
=2*11
=22