#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++
最新推荐文章于 2024-02-26 13:44:41 发布