3

关于用栈将二进制转为八进制的问题 总是提示位置冲突

#include "stdafx.h"
#include
#include
const int STACK_INIT_SIZE=20;
const int STACKINCREMENT = 10;
typedef struct
{
char *base;
char *top;
int stacksize;
}sqstack;
void initStack(sqstack *s)
{
s->base = (char *)malloc(STACK_INIT_SIZE*sizeof(char ));
if (!s->base) exit(0);
s->top=s->base;
s->stacksize = STACK_INIT_SIZE;;
}
void pushStack(sqstack *s,char e)
{
if (s->top - s->base >= s->stacksize)
{
s->base = (char *)realloc(s->base, (STACKINCREMENT+s->stacksize)*sizeof(char ));
if (!s->base) exit(0);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
void popStack(sqstack *s,char *e)
{
if (s->base == s->top) return;
s->top--;
*e = *s->top;
}
int stackLen(sqstack s)
{
return s.top - s.base;
}
int main()
{
char c,e;
int sum=0;
sqstack *s=NULL,s2;
initStack(s);
initStack(&s2);
cin >> e;
while (e != '#')
{
if (e=='0'||e=='1')
pushStack(s, e);
cin >> e;
}
int a = stackLen(*s);
for (int i = 0; i < a; i += 3)
{
for (int j = 0; j < 3; j++)
{
popStack(s, &c);
sum = sum + (c - 48)*pow(2, j);
if (s->base == s->top) break;
}
pushStack(&s2, sum);
sum = 0;
while (s2.base != s2.top)
{
popStack(&s2, &c);
cout << c;
}
system("pause");
return 0;
}

}

查看全部
sinat_25885723
sinat_25885723
2015/09/22 10:38
  • 二进制
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复