二进制转八进制
#include<stdio.h>#include<stdlib.h>
#include<math.h>
#define STACKSIZE1 20
#define STACKSIZE2 10
#define STACKINSREMATH 10
typedef char ElemType;
typedef struct
{
ElemType *top;
ElemType *base;
int StackSize;
}sqtack;
void INITSTACK1(sqtack *s1)
{
s1->base = (ElemType *)malloc(STACKSIZE1 * sizeof(ElemType));
if(!s1->base)
{
printf("栈一初始化失败!!!");
exit(0);
}
s1->top = s1->base;
s1->StackSize = STACKSIZE1;
}
void Push1(sqtack *s1,ElemType c1)
{
if(s1->top - s1->base >= s1->StackSize)
{
s1->base = (ElemType *)realloc(s1->base,(s1->StackSize +STACKINSREMATH) * sizeof(ElemType));
}
if(!s1->base)
{
printf("栈一压入失败!!!") ;
exit(0);
}
*(s1->top) = c1;
s1->top++;
}
void Pop1(sqtack *s1,ElemType *c)
{
if(s1->top == s1->base)
{
return ;
}
*c = *(--s1->top);
}
intStackLen1(sqtack s1)
{
return (s1.top - s1.base);
}
void INITSTACK2(sqtack *s2)
{
s2->base = (ElemType *)malloc(STACKSIZE2 * sizeof(ElemType));
if(!s2->base)
{
printf("栈二初始化失败!!!");
exit(0);
}
s2->top = s2->base;
s2->StackSize = STACKSIZE2;
}
void Push2(sqtack *s2,ElemType c2)
{
if(s2->top - s2->base >= s2->StackSize)
{
s2->base = (ElemType *)realloc(s2->base,(s2 ->StackSize + STACKINSREMATH));
}
if(!s2->base)
{
printf("栈二压入失败!!!");
exit(0);
}
*(s2->top) = c2;
s2->top++;
}
void Pop2(sqtack *s2,ElemType *c2)
{
if(s2->top == s2->base)
{
return ;
}
*c2 = *(--s2->top);
}
int StackLen2(sqtack s2)
{
return (s2.top - s2.base);
}
int main()
{
sqtack s1,s2;
int i,result = 0,sum = 0,yu;
int len1,len2;
char c1,c2;
INITSTACK1(&s1);
INITSTACK2(&s2);
printf("请输入二进制数,输入“#”表示结束:\n");
scanf("%c",&c1);
while(c1 != '#')
{
Push1(&s1,c1);
scanf("%c",&c1);
}
getchar();
len1 = StackLen1(s1);
for(i = 0;i < len1;i++)
{
Pop1(&s1,&c1);
yu = i % 3;
sum += (c1 - 48) * pow(2,yu);
if((i % 3) == 2)
{
Push2(&s2,sum);
sum = 0;
}
}
Push2(&s2,sum);
len2 = StackLen2(s2);
for(i = 0;i < len2;i++)
{
Pop2(&s2,&c2);
result += c2 * pow(10,len2 - (i + 1));
}
printf("输出的八进制数:%d\n",result)
return 0; 本帖最后由 jackz007 于 2020-3-14 23:00 编辑
#include <stdio.h>
char * bin2oct(char * oct , const char * bin)
{
char c ;
unsigned int d , k , m ;
for(d = 0 , k = 0 ; bin ; d = d * 2 + bin - '0' , k ++) ;
for(m = 0 ; d ; oct = '0' + (d % 8) , oct = '\0' , d /= 8) ;
for(k = 0 ; k < m / 2 ; c = oct , oct = oct , oct = c , k ++) ;
return oct ;
}
char * oct2bin(char * bin , const char * oct)
{
char c ;
unsigned int d , k , m ;
for(d = 0 , k = 0 ; oct ; d = d * 8 + oct - '0' , k ++) ;
for(m = 0 ; d ; bin = '0' + (d % 2) , bin = '\0' , d /= 2) ;
for(; m < 32 ; bin = '0' , bin = '\0') ;
for(k = 0 ; k < m / 2 ; c = bin , bin = bin , bin = c , k ++) ;
return bin ;
}
main(void)
{
char c , bin , oct ;
unsigned int k ;
printf("请输入二进制数,输入“#”表示结束:\n") ;
for(k = 0 ; (c = getchar()) != '#' && c != '\n' && k < 32 ; bin = c , bin = '\0') {
if(c != '0' && c != '1') {
fprintf(stderr , "输入内容无效!\n") ;
k = 0 ;
break ;
}
}
if(k > 0) {
printf("输入的二进制数:%s\n" , bin) ;
printf("输出的八进制数:%s\n" , bin2oct(oct , bin)) ;
printf("输出的二进制数:%s\n" , oct2bin(bin , oct)) ;
}
}
编译、运行实况:
C:\Bin>g++ -o x x.c
C:\Bin>x
请输入二进制数,输入“#”表示结束:
00100000000010100000000010110000#
输入的二进制数:00100000000010100000000010110000
输出的八进制数:4002400260
输出的二进制数:00100000000010100000000010110000
C:\Bin>
页:
[1]