C++ C++;:二进制到十进制w/转换过程的外观

C++ C++;:二进制到十进制w/转换过程的外观,c++,binary,C++,Binary,我正在尝试制作一个将二进制转换为十进制的程序,但需要显示转换过程 输入二进制数:10110 1*(2^4)+1*(2^2)+1*(2^1) 10110的十进制等价物是:22 但是,在循环中间的计数器的值没有减少,导致这个 这是我当前的代码 #include <iostream> #include <math.h> #include <string> using namespace std; int main() { int bin, dec =

我正在尝试制作一个将二进制转换为十进制的程序,但需要显示转换过程

输入二进制数:10110

1*(2^4)+1*(2^2)+1*(2^1)

10110的十进制等价物是:22

但是,在循环中间的计数器的值没有减少,导致这个

这是我当前的代码

#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;

}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int-bin,dec=0,余数,num,base=1,counter=0,counter=0,constnum;
cout>num;
bin=num;
constnum=num;
而(bin>0)
{
bin=bin/10;
计数器2++;
}
while(num>0)
{
如果(数值%10==1){
不能只使用一个位集:

#include <bitset>
#include <iostream>

int main()
{
    std::bitset<32> val;
    std::cin >> val;
    std::cout << val.to_ulong() << "\n";
}
#包括
#包括
int main()
{
std::位集val;
标准:cin>>val;

std::cout你应该按照上升顺序而不是下降顺序来完成这个过程,这是为了匹配你的算法,这是上升顺序

所以,在你最后的一段时间里,不是这样:

if (num % 10 == 1) {
        cout << " 1*(2^" << counter2 << ") +";
        counter2--; 
        }
else if(num % 10 == 0) {
        counter2--;
     }

因为不再需要

正如前面所指出的,您正在以不同的顺序计算和显示结果。我建议您存储结果,稍后再显示。类似于:

#include <sstream>
....

int pos = 0;
string res;
while (num > 0) {
    if (num % 10 == 1) {
        stringstream out;
        out << "1*(2^" << pos << ") + ";

        res = out.str() + res;
    }

    remainder = num % 10; //get the last digit of the input
    dec = dec + remainder * base;
    base = base * 2;
    num = num / 10;
    pos++;
}

int len = res.length();
// to remove the last '+'
if (len >= 3) {
    res = res.substr(0, len - 3);
}

cout << res;
#包括
....
int pos=0;
字符串res;
while(num>0){
如果(数值%10==1){
流出来;
但是输出应该是1*(2^4)+1*(2^2)+1*(2^1)。根据您的建议,它会打印出1*(2^1)+1*(2^2)+1*(2^4)
while(bin > 0)
 {
    bin=bin/10;
    counter2++;
 }
#include <sstream>
....

int pos = 0;
string res;
while (num > 0) {
    if (num % 10 == 1) {
        stringstream out;
        out << "1*(2^" << pos << ") + ";

        res = out.str() + res;
    }

    remainder = num % 10; //get the last digit of the input
    dec = dec + remainder * base;
    base = base * 2;
    num = num / 10;
    pos++;
}

int len = res.length();
// to remove the last '+'
if (len >= 3) {
    res = res.substr(0, len - 3);
}

cout << res;