#include<string>
#include<iostream>
#include<cmath>
#define MAXLEN 50
using namespace std;
struct StackType{
char data[MAXLEN];
int top;
};
StackType*STInit()
{
StackType*s;
if(s=new StackType){
s->top=0;
return s;
}
return NULL;
}
StackType*STPush(StackType*s,char e){
if(s->top==MAXLEN)
cout<<"栈溢出!"<<endl;
s->data[++s->top]=e;
return s;
}
char STPop(StackType*s){
if(s->top==0){
cout<<"栈为空!"<<endl;
exit(0);
}
return s->data[s->top--];
}
int main()
{
StackType*s;
int i,len,sum=0;
char e,h;
cout<<"请输入二进制数:"<<endl;
s=STInit();
cin>>e;
while(e!='#'){
STPush(s,e);
cin>>e;
}
len=s->top;
for(i=0;i<len;i++){
sum=sum+(STPop(s)-48)*pow(2,double(i));
}
cout<<sum<<endl;
system("pause");
return sum;
}
利用栈结构二进制数转十进制数代码实现c++
原文作者:进制转换
原文地址: https://blog.csdn.net/cherishwangq/article/details/53168981
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/cherishwangq/article/details/53168981
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。