关于二进制转八进制的代码
我昨天晚上看了小甲鱼的用栈实现二进制转十进制,然后留给我们的作业是二进制转八进制和十六进制,昨天编了我好久,都没编成功,昨天睡觉前整理了一下思路,今天早上编成功的,这次是自己编的啊,就是栈的部分是参考小甲鱼视频里的,由于那堂课的源代码要30鱼币,好贵的,只好自己苦命的编了。求大神指导。#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define STACK_INIT_SIZE 20
#define STACKICREAMENT10
typedef char ElemType;
typedef struct
{
ElemType *top;
ElemType *base;
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=realloc(s->base, (s->stacksize + STACKICREAMENT ) * 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 Stacksize(sqStack s)
{
return (s.top - s.base);
}
int main()
{
int i,j,k,a,len,len1,len2,sum1=0,sum2=0;
ElemType e,sum;
sqStack s1,s2;
Initstack(&s1);
Initstack(&s2);
printf("请输入二进制数,以#结束\n");
scanf("%c",&e);
while(e!='#')
{
Push(&s1,e);
scanf("%c",&e);
}
getchar();
len1=Stacksize(s1);
printf("该栈的容量为%d\n",len1);
len=len1;
while(len % 3 != 0)
{
len++;
}
for(i=0; i< len /3; i++)
{
a=3;
if(i == len /3 - 1 && len != len1)
{
a= len1 % 3;
}
for(j=0; j<a; j++)
{
Pop(&s1,&e);
sum1=sum1+(e-48)*pow(2,j);
}
sum=sum1+48;
sum1=0;
Push(&s2,sum);
}
len2=Stacksize(s2);
printf("转换为八进制为:");
for(k=0; k<len2 ;k++)
{
Pop(&s2,&sum);
printf("%c",sum);
}
return 0;
} 关于16进制,只要把代码稍稍改动就可以,就是八进制是3位,十六进制是4位,看过视频的应该懂。 看看哦! 谢谢分享! #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include<math.h>
#define INIT_STACK_SIZE 20
#define STACKINCREMENT 10
typedef char ElemType;
typedef struct {
ElemType *base;
ElemType *top;
int stackSize;
}seStack;
void initStack(seStack *s);
void push(seStack *s,ElemType e);
void pop(seStack *s,ElemType *e);
int stackLen(seStack s);
int main() {
seStack s2,s3;
initStack(&s2);
initStack(&s3);
printf("请输入你想要转化的二进制数:输入'#'结束");
ElemType c1;
scanf("%c",&c1);
while(c1 != '#')
{
push(&s2,c1);
scanf("%c",&c1);
}
getchar();
int i;
if(stackLen(s2) % 3 == 0)
{
i = stackLen(s2)/3;
} else
i = stackLen(s2)/3 + 1;
for(;i>0;i--)
{
ElemType mid = 0;
ElemType rval;
for(int j = 0;j<3;j++)
{
if(s2.base == s2.top)
{
rval = '0';
}
else
pop(&s2,&rval);
mid += (rval-48) * pow(2,j);
}
push(&s3,mid);
}
printf("转化后的八进制数是:");
ElemType val;
while (s3.top != s3.base)
{
pop(&s3,&val);
printf("%d",val);
}
return 0;
}
void initStack(seStack *s)
{
s->base = (ElemType *)malloc(sizeof(ElemType));
if(!s->base)
exit(0);
s->top = s->base;
s->stackSize = INIT_STACK_SIZE;
}
void push(seStack *s,ElemType e)
{
if(s->top - s->base >= s->stackSize)
{
s->base = (ElemType *)realloc(s->base,(s->stackSize + STACKINCREMENT) * sizeof(ElemType));
s->top = s->base + s->stackSize;
s->stackSize = s->stackSize + STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
void pop(seStack *s,ElemType *e)
{
if(s->top == s->base)
return;
*e = *--(s->top);
}
int stackLen(seStack s)
{
return (s.top - s.base);
}
这样不知道可不可以。算了几个是对的,就是不知道有没有啥逻辑错误 #include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define Stack_size 20
#define Stackinrement 10
typedef char Elemtype;
typedef struct{
Elemtype *bottom ;
Elemtype *top;
int Stacksize;
}sqstack;
void initstack(sqstack *s)
{
s->bottom=(Elemtype*)malloc((Stack_size)*sizeof(Elemtype));
if(!s->bottom)
{exit(0);}
s->top=s->bottom;
s->Stacksize= Stack_size;
}
void push(sqstack *s,Elemtype e)
{
if((s->top-s->bottom)>=s->Stacksize-1)
{s->bottom=(Elemtype*)realloc(s->bottom,(s->Stacksize+Stackinrement)*sizeof(Elemtype)) ;
if(!s->bottom)
{exit(0);}
s->top=s->bottom+Stackinrement;
s->Stacksize=s->Stacksize+Stackinrement;
}
*s->top= e;
s->top++;
}
void pop(sqstack *s,Elemtype *e)
{
if(s->top==s->bottom)
{exit(0);}
s->top--;
*e=*s->top;
}
int stacklen(sqstack s)
{return(s.top-s.bottom);
}
int main()
{ Elemtype c;
sqstack s,s1;
initstack(&s);
initstack(&s1);
printf ("输入二进制数,输入#代表结束");
scanf("%c",&c);
while(c!='#')
{
push(&s,c) ;
scanf("%c",&c);
}
getchar();
int len= stacklen(s);
char sum;
int len1=len/3;
int a=len%3;
int j=0;
printf("当前栈容量为%d\n",len);
for(int i=0;i<len1;i++)
{
for(j=0;j<3;j++)
{
pop(&s,&c);
sum=sum+pow(2,j)*(c-48);
}
j=0;
sum=sum+48;
push(&s1,sum);
sum=0;
}
if(a!=0)
{
for(int k=0;k<a;k++)
{
sum=sum+pow(2,k)*(c-48);
}
len1=len1+1;
push(&s1,sum);
}
printf("转化结果为");
for(int i=0;i<len1;i++)
{
pop(&s1,&sum);
printf("%c",sum);
}
printf("\n");
return 0;
大部分参考了视频教学,改的有点乱,能跑就懒得动了
大概的想法就是第一个s堆完了以后,先把不一定能凑成三个的顶部元素拿掉,再三个三个弹出来加完推到s1里,最后判断要不要推一次顶上的几个元素和
最后的sum跑出来才忘记加48了,{:10_258:}{:10_258:}{:10_258:}
页:
[1]