1、二进制转八进制:
/*二进制转八进制*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 20
#define ERROR 0
#define OK 1
typedef int Status;
typedef char Elemtype;
typedef struct{ //栈的静态分配存储结构
Elemtype elem[MAXSIZE];
int top;
}SqStack;
void InitStack(SqStack *S)
{
S->top=-1; //top为整数且指向栈顶元素
}
/*压栈操作*/
Status PushStack(SqStack *S,Elemtype e)
{
if(S->top >= MAXSIZE-1)
{
return ERROR;
}
S->elem[++S->top]=e;
return OK;
}
/*弹栈操作*/
Status PopStack(SqStack *S,Elemtype *e)
{
if(S->top == -1)
{
return ERROR;
}
*e = S->elem[S->top--];
return OK;
}
/*求栈长度*/
Status StackLength(SqStack *S)
{
return S->top+1;
}
int main()
{
int sum=0,value,i,j;
Elemtype c;
SqStack S,S1;
InitStack(&S);
printf("请输入二进制数,以#结束输入:\n");
scanf("%c",&c);
while( c != '#')
{
if( c == '1'|| c == '0')
{
value=PushStack(&S,c);
if(!value)
{
printf("输入二进制数超过存储范围!\n");
return ERROR;
}
scanf("%c",&c);
}
else
{
printf("非法输入!\n");
return ERROR;
}
}
getchar(); //把'\n'从缓冲区去掉
InitStack(&S1);
int len=StackLength(&S);
for(i=0 ; i<len ; i+=3)
{
for(j=0;j<3;j++) //三位二进制数对应一位八进制数
{
PopStack(&S,&c);
sum += ( c - 48 ) * pow( 2 , j );
if(S.top == -1)
{
break;
}
}
PushStack(&S1,sum+48);//八进制数转字符进行压栈
sum=0; //从新进行下一个字符转换
}
printf("转化成八进制是:");
while( S1.top != -1) //把转换好的八进制数以字符形式进行弹栈
{
PopStack(&S1,&c);
printf("%c",c);
}
printf("(O)\n");
return 0;
}
2、二进制转十进制:
/*二进制转十进制*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 20
#define ERROR 0
#define OK 1
typedef int Status;
typedef char Elemtype;
typedef struct{
Elemtype elem[MAXSIZE];
int top;
}SqStack;
void InitStack(SqStack *S)
{
S->top=-1;
}
Status PushStack(SqStack *S,Elemtype e)
{
if(S->top >= MAXSIZE-1)
{
return ERROR;
}
S->elem[++S->top]=e;
return OK;
}
Status PopStack(SqStack *S,Elemtype *e)
{
if(S->top == -1)
{
return ERROR;
}
*e = S->elem[S->top--];
return OK;
}
/*求栈长度*/
Status StackLength(SqStack *S)
{
return S->top+1;
}
int main()
{
int sum=0,value;
Elemtype c;
SqStack S;
InitStack(&S);
printf("请输入二进制数,以#结束输入:\n");
scanf("%c",&c);
while( c != '#')
{
if( c == '1'|| c == '0')
{
value=PushStack(&S,c);
if(!value)
{
printf("输入二进制数超过存储范围!\n");
return ERROR;
}
scanf("%c",&c);
}
else
{
printf("非法输入!\n");
return ERROR;
}
}
getchar(); //把'\n'从缓冲区去掉
int len=StackLength(&S);
for(int i=0;i<len;i++)
{
PopStack(&S,&c);
sum += ( c - 48 ) * pow( 2 , i );
}
printf("转换的十进制数为:");
printf("%d(D)\n",sum);
return 0;
}
3、二进制转十六进制
/*二进制转十六进制*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 20
#define ERROR 0
#define OK 1
typedef int Status;
typedef char Elemtype;
typedef struct{ //栈的静态分配存储结构
Elemtype elem[MAXSIZE];
int top;
}SqStack;
void InitStack(SqStack *S)
{
S->top=-1; //top为整数且指向栈顶元素
}
/*压栈操作*/
Status PushStack(SqStack *S,Elemtype e)
{
if(S->top >= MAXSIZE-1)
{
return ERROR;
}
S->elem[++S->top]=e;
return OK;
}
/*弹栈操作*/
Status PopStack(SqStack *S,Elemtype *e)
{
if(S->top == -1)
{
return ERROR;
}
*e = S->elem[S->top--];
return OK;
}
/*求栈长度*/
Status StackLength(SqStack *S)
{
return S->top+1;
}
int main()
{
int sum=0,value,i,j;
Elemtype c;
SqStack S,S1;
InitStack(&S);
printf("请输入二进制数,以#结束输入:\n");
scanf("%c",&c);
while( c != '#')
{
if( c == '1'|| c == '0')
{
value=PushStack(&S,c);
if(!value)
{
printf("输入二进制数超过存储范围!\n");
return ERROR;
}
scanf("%c",&c);
}
else
{
printf("非法输入!\n");
return ERROR;
}
}
getchar(); //把'\n'从缓冲区去掉
InitStack(&S1);
int len=StackLength(&S);
for(i=0 ; i<len ; i+=4)
{
for(j=0;j<4;j++) //四位二进制数对应一位十六进制数
{
PopStack(&S,&c);
sum += ( c - 48 ) * pow( 2 , j );
if(S.top == -1)
{
break;
}
}
switch(sum)
{
case 10:
case 11:
case 12:
case 13:
case 14:
case 15: sum+=55;break;//大于十转换成字母字符
default :sum+=48; //小于十转换成数字字符
}
PushStack(&S1,sum); //把转换的字符进行压栈
sum=0; //从新进行下一个字符转换
}
printf("转化成十六进制是:");
while( S1.top != -1) //把转换好的十六进制数以字符形式进行弹栈
{
PopStack(&S1,&c);
printf("%c",c);
}
printf("(H)\n");
return 0;
}
4、运行效果截图如下: