69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typedef int ElemType;
typedef struct {
ElemType elem[MAXSIZE];
int top;
}SqStack;
void InitStack( SqStack *S);
int Empty( SqStack *S);
int Push(SqStack *S, ElemType x);
int Pop(SqStack *S );
void fun(int n,int R);
void main()
{
int n;
char i;
while(1)
{ printf("10进制转换为R(大小写都可以)进制\n");
printf("b-------2\n");
printf("o-------8\n");
printf("x-------16\n");
printf("e-------退出\n");
printf("请输入10进制数n和转换的i进制:\n");
scanf("%d,%c",&n,&i);
switch(i)
{ case'B':
case'b': printf("转换为2进制后为:\n");
fun(n,2);
break;
case'O':
case'o': printf("转换为8进制后为:\n");
fun(n,8);
break;
case'X':
case'x': printf("转换为16进制后为:\n");
printf("0x");
fun(n,16);
break;
case'E':
case'e': printf("退出系统\n");
exit(0);
default: printf("input error,input again");
}
}
}
void InitStack( SqStack *S)
{ S->top=0;
}
int Empty( SqStack *S)
{return ( S->top==0);
}
int Push(SqStack *S, ElemType x)
{ if( S->top==MAXSIZE ) return 0;
S->elem[S->top]=x; S->top++;
return 1;
}
int Pop(SqStack *S )
{ --S->top;
return (S->elem[S->top]);
}
void fun(int n,int R)
{ SqStack S;
InitStack(&S);
char m;
int p;
while(n!=0)
{
Push(&S,n%R);
n=n/R;
}
while(!Empty(&S))
{ p=Pop(&S);
if(p>=10)
{ m=p%10+'A';
printf("%c",m);
}
else
printf("%d",p);
}
printf("\n");
}