|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIDE 20
typedef char Elemtype;
typedef struct{
Elemtype *top;
Elemtype *base;
}Stack,*Pstack;
Pstack inin_stack(Pstack s)
{
s->base=(Elemtype*)malloc(MAXSIDE*sizeof(Elemtype));
s->top=s->base;
return s;
}
void push_stack(Pstack s,Elemtype e)
{
if(s->top-s->base>=MAXSIDE)//判断是否到达栈的上限,是的话要开辟空间;
{
s->base=(Elemtype*)realloc(s->base,(MAXSIDE+10)*sizeof(Elemtype));
s->top=s->base;
}
*s->top=e;
s->top++;
}
void pop_stack(Pstack s,Elemtype *e)
{
if(s->base=s->top)
return;
*e=*s->top;
s->top--;
}
int length(Pstack s)
{
return(s->top-s->base);
}
int main(void)
{
int i,sum=0,len=0;
Elemtype two,e;
Pstack n_stack;
n_stack=inin_stack(n_stack);
printf("请输入二进制数:");
scanf("%c",&two);
while(two!='\n')
{
push_stack(n_stack,two);
scanf("%c",&two);
}
printf("\n");
len=length(n_stack);
printf("len=%d\n",len);
for(i=0;i<len;i++)
{
pop_stack(n_stack,&e);
sum+=(e-48)*pow(2,i);
printf("转换成十进制输出为:%d\n",sum);
}
printf("转换成十进制输出为:%d\n",sum);
return 0;
}
怎么最终输出变成这个样子了?????????//
|
-
|