栾海鹏吧 关注:6贴子:23
  • 1回复贴,共1

【C语言学习】十进制转换成8进制或者2进制,或者其他小于10的进制

只看楼主收藏回复

#include<stdio.h>
#include <malloc.h>
typedef struct
{
int * base;
int * top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=S.top=(int *)malloc(100*sizeof(int));
}
void Push(SqStack & S,int  e)
{
*S.top=e;
S.top++;
}
void Pop(SqStack & S,int & e)
{
S.top--;
e=*S.top;
}
int StackEmpty(SqStack & S)
{
if(S.top==S.base) return 1;
else return 0;
}
void conversin()
{
int N;
int M;
SqStack S;
int e;
InitStack(S);//构造空栈
printf("输入要转化的进制(例如 2 ,8 ):");
scanf("%d",&M);
printf("输入要转化的十进制数字(例如 21 ):");
scanf("%d",&N);
int nn;
nn=N;
while(N)
{
Push(S,N%M);
N=N/M;
}
printf("%d转换成%d进制后的数字是:",nn,M);
while(!StackEmpty(S))//判断s是否为空,为空StackEmpty(S)返回1;
{
Pop(S,e);
printf("%d",e);
}
printf("\n\n\n\n");
}
void main()
{
conversin();
}


1楼2009-10-26 16:49回复
    • 219.143.204.*
    不错!


    3楼2009-10-26 17:19
    回复