c·c++/c++ 프로그래밍

2진수에서 10진수로 바꾸기

바로이순간 2012. 3. 21. 21:58

#include <iostream>

using namespace std;

 

int main() {

  char buf[100]={0,};

  int i, x=0;

  double h,y;


  i=0;

  cin>>buf;

  while(buf[i]&&buf[i]!='.') {

    x=x+x+buf[i]-48;

    i=i+1;

  }

  if(buf[i]=='.') i=i+1;

  h=.5;

  while(buf[i]) {

    y=y+h*(buf[i]-48);

    h=.5*h;

    i=i+1;

  }

  cout<<x+y;


  return 0;

}