数据结构5:利用栈实现二进制到十进制的转换

         这是一个利用栈实现的实例,自己写了一下,本想编写成各个进制之间都可以相互转换的“转换器”,可是在最开始的地方就已经卡住了,主要卡住的地方就是如何让用户自己输入一串10101代码(不需要限制),从而实现转换!

         我的想法呢,利用回车作为0101的结束,但是问题在于,如果说你作为int型输入时候,1010会被当做一个数,而且“回车”也是一个字符型的东东,总结一下问题在于:

如何使得用户的输入如何压入栈中

            这里提供一个思路吧:将输入全部当做字符进行,最后计算的时候减去48就可以了!

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define INCREACEMENT 10

typedef char ElemType;
typedef int Status;
typedef struct{
	ElemType *base;
	ElemType *top;
	int Length;
}SqStack;

void InitStack(SqStack *S)
{
	S->base = (ElemType *)malloc(MAXSIZE*sizeof(ElemType));
	if (!S->base)
	{
		printf("Game Over");
		exit(1);
	}
	S->top = S->base;
	S->Length = MAXSIZE;//该参数应该是整个数组的长度,而不是数据元素的长度,因为长度可以S->top-S->base得出!
}

void Push(SqStack *S,ElemType e)
{
	if (S->top - S->base >= S->Length)
	{
		S->base = (ElemType *)realloc(S->base,(INCREACEMENT+S->Length)*sizeof(ElemType));
		if (!S->base)
		{
			printf("Game Over");
			exit(1);
		}
		S->top = S->base +S->Length;
		S->Length += INCREACEMENT;
	}
	*(S->top) = e;
	S->top++;
}

Status Pop(SqStack *S,ElemType *e)
{
	if (S->top == S->base)
	{
		printf("There is no element");
		exit(1);
	}
	*e = *--(S->top);
	return OK;
}

int BinToDex(SqStack *S)
{
	int sum=0,i=0;
	ElemType temp;
	while (S->top != S->base)
	{
		Pop(S,&temp);
		sum += (temp-48)*pow(2,i);
		i++;
	}
	return sum;
}
int main()
{
	int j,len,sum=0;
	ElemType temp;
	SqStack S;
	InitStack(&S);
	//如何让用户输入自定义数组,并
	printf("Please input some numbers:");
	scanf("%c",&temp);
	while (temp != '\n')
	{
		Push(&S,temp);
		scanf("%c",&temp);
	}
	//getchar();
	len = S.top -S.base;
	sum = BinToDex(&S);
	printf("容量为:%d\n",len);
	printf("转换后的十进制数为:%d",sum);
	while (1)
	{
		;
	}
}
1、这个程序,最大的收获在于,如何获取用户输入!

2、这个程序在输入了一次之后就停滞了,需要改善;

3、添加了一点点代码,现在可以从,2,8,16进制转换到十进制了,但是对于进制输入的边界问题没有做检查!写的像shi一样!

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define INCREACEMENT 10

typedef char ElemType;
typedef int Status;
typedef struct{
	ElemType *base;
	ElemType *top;
	int Length;
}SqStack;

void InitStack(SqStack *S)
{
	S->base = (ElemType *)malloc(MAXSIZE*sizeof(ElemType));
	if (!S->base)
	{
		printf("Game Over");
		exit(1);
	}
	S->top = S->base;
	S->Length = MAXSIZE;//该参数应该是整个数组的长度,而不是数据元素的长度,因为长度可以S->top-S->base得出!
}

void Push(SqStack *S,ElemType e)
{
	if (S->top - S->base >= S->Length)
	{
		S->base = (ElemType *)realloc(S->base,(INCREACEMENT+S->Length)*sizeof(ElemType));
		if (!S->base)
		{
			printf("Game Over");
			exit(1);
		}
		S->top = S->base +S->Length;
		S->Length += INCREACEMENT;
	}
	*(S->top) = e;
	S->top++;
}

Status Pop(SqStack *S,ElemType *e)
{
	if (S->top == S->base)
	{
		printf("There is no element");
		exit(1);
	}
	*e = *--(S->top);
	return OK;
}

int BinToDex(SqStack *S,int e)
{
	int sum=0,i=0;
	ElemType temp;
	while (S->top != S->base)
	{
		Pop(S,&temp);
		if ('A'<=temp && temp <= 'F')      //这个判断用来处理16进制,16进制由于ASCII码应该-55(A代表10)
		{
			temp -= 7;
		}
		sum += (temp-48)*pow(e,i);
		i++;
	}
	return sum;
}
int main()
{
	int j,len,sum=0,e;
	ElemType temp;
	SqStack S;
	InitStack(&S);
	//如何让用户输入自定义数组,并
	printf("请输入转换的进制,2,8,或者16:");
	scanf("%d",&e);
	getchar();
	printf("Please input some numbers:");
	scanf("%c",&temp);
	while (temp != '\n')
	{
		Push(&S,temp);
		scanf("%c",&temp);
	}
	//getchar();
	len = S.top -S.base;
	sum = BinToDex(&S,e);
	printf("转换后的十进制数为:%d",sum);
	system("pause");
}


  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值