用C语言栈实现进制转换
#include<stdio.h> #include<stdlib.h> #include<windows.h> #define MAXSIZE 100 typedef struct //定义栈的结构体 { int data[MAXSIZE]; int top; }Se,*Pe; Pe begin(); //栈的初始化 void ru(Pe S,int x); //入栈函数 Pe zhuan(int m,int n); //进制转换函数 void xiao(Pe *S); //销毁栈 void chu(Pe S); //出栈函数 int main() { Pe S; int x,y; printf("输入要转换的十进制数和要转换的进制: "); scanf("%d %d",&x,&y); S=zhuan(x,y); chu(S); xiao(&S); } Pe begin() { Pe S; S=(Pe)malloc(sizeof(Se)); if(S) S->top=-1; return S; } void ru(Pe S,int x) { S->top++; S->data[S->top]=x; } Pe zhuan(int m,int n) { Pe S; S=begin(); int k; while(m!=0) { k=m%n; ru(S,k); m=m/n; } return S; } void xiao(Pe *S) { if(*S) free(*S); *S=NULL; } void chu(Pe S) { while(S->top!=-1) { printf("%d",S->data[S->top]); S->top--; } }
下一篇:
JDK中的设计模式之单例模式