使用栈的特性完成进制转换
seqtack.c
//进制转换
void converse(seqStack *S, int m, int n)//m是要转变的数 n是进制数
{
if(NULL==S)
{
printf("转换失败\n");
return;
}
int i;
while(m!=0)
{
i=m%n;
m=m/n;
stack_push(S,i);
}
}
main.c
#include <stdio.h>
#include "seqstack.h"
int main(int argc, const char *argv[])
{
seqStack *S=stack_create();
if(NULL==S)
{
return -1;
}
/*
stack_push(S,3);
stack_push(S,8);
stack_push(S,7);
stack_push(S,2);
stack_push(S,4);
//
stack_show(S);
//
stack_pop(S);
stack_pop(S);
stack_show(S);
//
printf("目前栈顶元素为:%d\n",*stack_top(S));
*stack_top(S)=520;
stack_show(S);
*/
//
converse(S,120,2);
stack_show(S);
//
stack_free(S);
S=NULL;
return 0;
}
结果: