現(xiàn)在,大家看看題目:
輸入一個(gè)八位二進(jìn)制 將其轉(zhuǎn)化為十進(jìn)制數(shù)
先看看大致的規(guī)律:
大家試驗(yàn)下:0000 1101的結(jié)果是不是13
大家看看源代碼:
#include<iostream>
using namespace std;
double power(double x,int n);
int main(){
char ch;//每次讀取的字符
int value=0;//接收每次相鄰兩個(gè)二進(jìn)制位相加的結(jié)果
cout<<"輸入八位二進(jìn)制數(shù):"<<endl;
for(int i=7;i>=0;i--){
cin>>ch;
while(ch='1'){
value+=(int)(ch*power(2,i));
}
}
cout<<"十進(jìn)制:"<<value<<endl;
}
double power(double x,int n){
double value=1.0;
while(n--){
value*=x;
}
return value;
}
暫時(shí)找不出源代碼有problem!
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。