使用栈和队列的特性来实现进制转换
需要看普通C语言版的看这里
全部代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M 100
typedef long ElemType;
// 栈
typedef struct
{
ElemType data[M];
int top;
} Stack;
// 队列
typedef struct QNode
{
ElemType data; /*数据域*/
struct QNode * next; /*指针域*/
} QNode, *QueuePtr;
//定义队列结构
typedef struct
{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
//初始化栈;
void InitStack(Stack *s)
{
s->top=-1;
}
int Push(Stack *s,ElemType e)
{
if(s->top==M-1)
{
printf("栈满\n");
return 0;
}
s->top++;
s->data[s->top]=e;
return 1;
}
bool Pop(Stack *&s, ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
//判断是否为空
bool StackEmpty(Stack * s)
{
return(s->top==-1);
}
//栈的大小
int StackSize(Stack * s)
{
return(s->top + 1);
}
// 初始化队列
void initQueue(LinkQueue *Q)
{
Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
if(!Q->front) exit(OVERFLOW);
Q->front->next = NULL;
}
// 销毁队列
void destroyQueue(LinkQueue *Q)
{
while (Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
}
// 入队
void enQueue(LinkQueue *Q, ElemType e)
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
//插入数据
p->data = e;
p->next = NULL;
//Q.rear一直指向队尾
Q->rear->next = p;
Q->rear = p;
}
// 出队
void deQueue(LinkQueue *Q, ElemType &e)
{
if(Q->front == Q->rear) return;
QueuePtr p = Q->front->next;
e = p->data;
Q->front->next = p->next; //队头元素p出队
if(Q->rear == p) //如果队中只有一个元素p, 则p出队后成为空队
Q->rear = Q->front = NULL; //给队尾指针赋值
free(p); //释放存储空间
}
// 队列是否空
bool isQueueEmpty(LinkQueue *Q) {
return Q->front->next == NULL;
}
/**
* 整数部分十进制转二进制
*/
void convertIntegerToBinary(ElemType N)
{
ElemType e;
Stack *s=(Stack *)malloc(sizeof(Stack));
InitStack(s);
while(N)
{
Push(s,N%2);
N=N/2;
}
while(! StackEmpty(s))
{
Pop(s,e);
printf("%d",e);
}
}
/**
* 小数部分十进制转二进制
*/
void convertDecimalToBinary(double n)
{
char a[M];
int i=0;
ElemType e;
LinkQueue *Q = (LinkQueue *)malloc(sizeof(LinkQueue));
if(!Q) exit(OVERFLOW);
initQueue(Q);
if (n == 0) {
return;
}
printf(".");
for(i=0; i<18; i++)
{
if(n*2>1)
{
n=n*2-1;
enQueue(Q, 1);
// printf("1");
}
else
{
n=n*2;
enQueue(Q, 0);
// printf("0");
}
}
while (Q->front) {
deQueue(Q, e);
printf("%ld", e);
}
printf("\n\n");
}
/**
* 十进制转二进制
*/
void digitalToBinary(double input) {
long integer = input / 1;
double decimal = input - (double)integer;
convertIntegerToBinary(integer);
convertDecimalToBinary(decimal);
}
/**
* 二进制转十进制
*/
void binaryToDigital(char binary[]) {
char ch;
char integer = 0;
double decimal = 0.0;
int i;
bool hasDecimal = false;
ElemType e;
Stack *S=(Stack *)malloc(sizeof(Stack));
InitStack(S);
LinkQueue *Q = (LinkQueue *)malloc(sizeof(LinkQueue));
initQueue(Q);
// 计算整数和小数所占位数
for (i = 0; i < M; ++i) {
ch = binary[i];
if (ch == 0) {
break;
}
if (!hasDecimal && ch != '.') {
Push(S, ch - '0');
} else {
if (ch == '.') {
hasDecimal = true;
} else {
enQueue(Q, ch - '0');
}
}
}
// 计算整数部分
i = 0;
while(!StackEmpty(S))
{
Pop(S,e);
if (e == 1) {
integer += pow(2, i);
}
++i;
}
// 计算小数部分
if (hasDecimal) {
i = -1;
while (Q->front) {
deQueue(Q, e);
if (e == 1) {
decimal += (double)pow(2, i);
}
--i;
}
printf("%lf", (double)integer + decimal);
} else {
printf("%lld ", integer);
}
printf("\n\n");
}
int main() {
double digital;
char binary[M];
int n;
while(1)
{
printf("\n1:十进制转换二进制,2:二进制转换十进制,3:退出\n");
scanf("%d",&n);
// getchar();
switch(n)
{
case 1:
printf("请输入待转的十进制数:\n");
scanf("%lf", &digital);
printf("转换为二进制值为:\n");
digitalToBinary(digital);
break;
case 2:
printf("请输入待转的二进制数:\n");
scanf("%s", binary);
printf("转换为十进制值为:\n");
binaryToDigital(binary);
break;
case 3:
printf("您已退出\n");
exit(0);
default:
printf("error\n");
}
}
}