这是我用栈实现的十进制数转换为八进制数,其他进制转换同理,由于是在Turbo C++ 3.0上编写的,所以在Turbo C 2.0 或其他版本的C编译工具上可能不能通过编译而成为可执行文件。 #define maxlength 20#include<malloc.h>typedef struct stacknode{int *base; int *top; int length;}stack; stack *initstack(){stack *S; (*S).base=(stack*)malloc(maxlength*sizeof(stack)); if(!(*S).base)exit(0); (*S).top=(*S).base; return(S);} stack *push(stack *S,int r){*(*S).top++=r; if((*S).top-(*S).base>=maxlength) {(*S).top=(int*)realloc((*S).base,sizeof(int)); *(*S).top++=r; } return(S);} int pop(stack *S){int r; if((*S).top==(*S).base)exit(0); r=*(--(*S).top); return(r);} void main(){stack *S; int n,m; printf("Please input the number you want to convert:"); scanf("%d",&n); printf("\n"); S=initstack(); while(n!=0) {push(S,n%8); n=n/8; } while(S!=NULL) {m=pop(S); printf("%d",m); }}

评论