您的位置:首页 > 理论基础 > 数据结构算法

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

2014-04-07 22:44 756 查看
这是一个利用栈实现的实例,自己写了一下,本想编写成各个进制之间都可以相互转换的“转换器”,可是在最开始的地方就已经卡住了,主要卡住的地方就是如何让用户自己输入一串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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: