栈的操作。。。把二进制转换为十进制
#include<iostream>using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//#define DataType int
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
/*初始化一个栈*/
void InitStack(SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base) exit(0);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
/*压栈操作,把元素e压入栈中*/
void Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base) exit(0);
S->top=S->base+S->stacksize;
S->stacksize=S->stacksize+STACKINCREMENT;
}
*(S->top++)=e;
}
/*出栈操作,把栈顶元素弹出栈*/
void Pop(SqStack *S,int e)
{
if(S->base==S->top)
exit(0);
e=*(--S->top);
cout<<"栈中e的值"<<e<<endl;
// (S->top)--;//?????????????????????????????????????????????????????????????
}
/*取得栈中元素的个数*/
int GetNumber(SqStack S)
{
return (S.top)-(S.base);
}
void main()
{
int bitNumber,tenNumber=0,n,count=0,s=0;
int i,j;
cout<<"二进制:"<<endl;
SqStack stack;//为甚么*stack??????????????????????????????????????????
InitStack(&stack);
cout<<"二进制数位数为几位:"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"从低到高输入数(0或1):"<<endl;
cin>>bitNumber;
/*if(bitNumber!=0||bitNumber!=1)
exit(0);*/
Push(&stack,bitNumber);
}
count=GetNumber(stack);
cout<<"开始栈的容量为:"<<count<<endl;
/*感觉下面这一段有问题,怎么改啊*/
for(i=0;i<n;i++)
{
Pop(&stack,bitNumber);
cout<<"输出来的值"<<bitNumber<<endl;
count=GetNumber(stack);
cout<<"栈的容量为:"<<count<<endl;
for(j=0;j<count;j++)
{
bitNumber=bitNumber*2;
cout<<".....bitNumb的值"<<bitNumber<<endl;
}
tenNumber=tenNumber+bitNumber;
//bitNumber=0;
}
cout<<"出结果了"<<tenNumber<<endl;
}
结果与正确答案不符合。。。。是后面的计算那错了貌似。。。怎么改啊