代码拉取完成,页面将自动刷新
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef int ElemType;
typedef struct
{
ElemType* base;//栈底
ElemType* top;//栈顶
int stackSize;
}sqStack;
void InitStack(sqStack* s)
{
s->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack* s, ElemType e)
{
if (s->top - s->base >= s->stackSize)
{
s->base = (ElemType*)malloc((s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if (!s->base)
{//检查
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack* s, ElemType *e)//删除数据
{
if (s->top == s->base)//没数据了
{
return;
}
*e = *--(s->top);
}
int StackLen(sqStack s)//因为不对栈进行修改,所以不用传指针
{
return (s.top - s.base);
}
int main()
{
ElemType c;
sqStack s;
int len, i, sum = 0;
InitStack(&s);
printf("请输入二进制数,输入#符号表示结束!\n");
scanf("%c", &c);
while (c!='#')
{
Push(&s, c);
scanf("%c", &c);
}
getchar();//清理键盘缓冲区,把'\n'从缓冲区去掉
len = StackLen(s);//获取当前容量
printf("栈的当前容量是:%d\n", len);
for (i = 0; i < len; i++)
{
Pop(&s, &c);//弹出一个
sum = sum + (c - 48) * pow(2, i);
}
printf("转化为十进制数是:%d\n", sum);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。