利用栈进行二进制到八进制、十进制、十六进制的转换

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、运行效果截图如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值