4.22打卡

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double power(double x,int n)
{
    double val=1.0;
    while(n--)
    val*=x;
    return val;
}
int main()
{
    int value=0;
    cout<<"Enter an 8 bit binary number: ";
    for(int i=7;i>=0;i--)
    {
        char ch;
        cin>>ch;
        if(ch=='1')
        value+=static_cast<int>(power(2,i));
     } 
     cout<<"Decimal value is "<<value<<endl;
    return 0;
}

 

posted @ 2023-04-22 13:30  记得关月亮  阅读(11)  评论(0)    收藏  举报