如何用C语言栈实现十进制转二进制的示例代码?
- 内容介绍
- 相关推荐
本文共计526个文字,预计阅读时间需要3分钟。
原文示例讲述了一种C语言使用栈实现十进制转二进制的方法。以下为简化版:
使用C语言栈结构,实现十进制转二进制的步骤如下:
1. 初始化栈。
2.读取十进制数。
3.循环除以2,将余数入栈。
4.当十进制数减至0时,出栈余数,得到二进制数。
参考示例代码:
c
#include #include#define MAX_SIZE 100
typedef struct { int data[MAX_SIZE]; int top;} Stack;
void initStack(Stack *s) { s->top=-1;}
int isEmpty(Stack *s) { return s->top==-1;}
void push(Stack *s, int value) { if (s->top data[++s->top]=value; }}
int pop(Stack *s) { if (!isEmpty(s)) { return s->data[s->top--]; } return -1;}
void decimalToBinary(int decimal) { Stack stack; initStack(&stack);
while (decimal) { push(&stack, decimal % 2); decimal /=2; }
while (!isEmpty(&stack)) { printf(%d, pop(&stack)); }}
int main() { int decimal; printf(Enter a decimal number: ); scanf(%d, &decimal); printf(Binary representation: ); decimalToBinary(decimal); return 0;}
本文实例讲述了C语言用栈实现十进制转换为二进制的方法。分享给大家供大家参考,具体如下:
#include<stdio.h> #include<malloc.h> #include<math.h> #include<string.h> #include "process.h" #define SIZE 100 #define STACKINCREMENT 10 #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status; typedef struct { int a; } SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; } SqStack; SqStack S; //定义全局变量 Status InitStack(SqStack *S) { S->base=(SElemType *)malloc(SIZE*sizeof(SElemType)); if(!S->base) exit(OVERFLOW); S->top=S->base; S->stacksize=SIZE; return OK; } Status Push(SqStack *S,SElemType e) { if(S->top-S->base>=S->stacksize) { S->base=(SElemType *)malloc((S->stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S->base) exit(OVERFLOW); S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *S->top++=e; //printf("%dwww\n",*--S->top); return OK; } Status Stackempty(SqStack *S) { if(S->top==S->base) return TRUE; else return FALSE; } Status Pop(SqStack *S,SElemType *e) { if(S->top==S->base) return ERROR; *e=*--S->top; return OK; } Status DtoBTrans(int N,SqStack *S) { SElemType e; while(N) { e.a=N%2; Push(S,e); N=N/2; } while(!Stackempty(S)) { Pop(S,&e); printf("%d",e); } return OK; } void main() { int x; InitStack(&S); printf("请输入十进制数:"); scanf("%d",&x); DtoBTrans(x,&S); }
运行结果:
希望本文所述对大家C语言程序设计有所帮助。
本文共计526个文字,预计阅读时间需要3分钟。
原文示例讲述了一种C语言使用栈实现十进制转二进制的方法。以下为简化版:
使用C语言栈结构,实现十进制转二进制的步骤如下:
1. 初始化栈。
2.读取十进制数。
3.循环除以2,将余数入栈。
4.当十进制数减至0时,出栈余数,得到二进制数。
参考示例代码:
c
#include #include#define MAX_SIZE 100
typedef struct { int data[MAX_SIZE]; int top;} Stack;
void initStack(Stack *s) { s->top=-1;}
int isEmpty(Stack *s) { return s->top==-1;}
void push(Stack *s, int value) { if (s->top data[++s->top]=value; }}
int pop(Stack *s) { if (!isEmpty(s)) { return s->data[s->top--]; } return -1;}
void decimalToBinary(int decimal) { Stack stack; initStack(&stack);
while (decimal) { push(&stack, decimal % 2); decimal /=2; }
while (!isEmpty(&stack)) { printf(%d, pop(&stack)); }}
int main() { int decimal; printf(Enter a decimal number: ); scanf(%d, &decimal); printf(Binary representation: ); decimalToBinary(decimal); return 0;}
本文实例讲述了C语言用栈实现十进制转换为二进制的方法。分享给大家供大家参考,具体如下:
#include<stdio.h> #include<malloc.h> #include<math.h> #include<string.h> #include "process.h" #define SIZE 100 #define STACKINCREMENT 10 #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status; typedef struct { int a; } SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; } SqStack; SqStack S; //定义全局变量 Status InitStack(SqStack *S) { S->base=(SElemType *)malloc(SIZE*sizeof(SElemType)); if(!S->base) exit(OVERFLOW); S->top=S->base; S->stacksize=SIZE; return OK; } Status Push(SqStack *S,SElemType e) { if(S->top-S->base>=S->stacksize) { S->base=(SElemType *)malloc((S->stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S->base) exit(OVERFLOW); S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *S->top++=e; //printf("%dwww\n",*--S->top); return OK; } Status Stackempty(SqStack *S) { if(S->top==S->base) return TRUE; else return FALSE; } Status Pop(SqStack *S,SElemType *e) { if(S->top==S->base) return ERROR; *e=*--S->top; return OK; } Status DtoBTrans(int N,SqStack *S) { SElemType e; while(N) { e.a=N%2; Push(S,e); N=N/2; } while(!Stackempty(S)) { Pop(S,&e); printf("%d",e); } return OK; } void main() { int x; InitStack(&S); printf("请输入十进制数:"); scanf("%d",&x); DtoBTrans(x,&S); }
运行结果:
希望本文所述对大家C语言程序设计有所帮助。

