使用棧和隊列的特性來實現進制轉換
需要看普通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");
}
}
}