3-2 输入一个8位二进制数,将其转换为十进制数输出。

设计思路:c++语言的循环结构以及函数的定义及使用相结合可实现程序的运行。

 

代码:

#include<iostream>

using namespace std;

double power(double x,int n);

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,1));

  }

  cout<<"Decimal value is "<<value<<endl;

  return 0;

}

double power(double x,int n){

  double val=1.0;

  while(n--)

    val*=x;

}

总结:c++语言函数的定义及使用更熟练

posted @ 2023-04-19 23:04  cvjj  阅读(74)  评论(0)    收藏  举报