#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAXSIZE 20
typedef struct //实现顺序栈结构体
{
int top;
int data[MAXSIZE];
}Stack;
void InitStack(Stack *&S) //初始化栈
{
S->top = -1;
S->data[MAXSIZE]={0};
}
int Push(Stack *S,int i) //进栈
{
if(S->top > MAXSIZE-1)
{
return 0;
}
S->top++;
S->data[S->top]=i;
}
int Pop(Stack *S) //出栈
{
int e;
if(S->top == -1)
{
return 0;
}
e = S->data[S->top];
S->top--;
return e;
}
int GetTop(Stack *S) //取栈顶元素
{
int e;
if(S->top == -1)
{
return 0;
}
e = S->data[S->top];
return e;
}
void DTranB(Stack *S,int i) //十进制 -> 二进制
{
int n = i;
while(i>0)
{
Push(S,i%2);
i /= 2;
}
printf("%d转换为二进制为:",n);
while(S->top > -1)
{
printf("%d",Pop(S));
}
printf("\n\n");
}
void DTranH(Stack *S,int i) //十进制 -> 十六进制
{
int n = i;
while(i>0)
{
Push(S,i%16);
i /= 16;
}
printf("%d转换为十六进制为:0X",n);
while(S->top > -1)
{
switch(GetTop(S)) //根据栈顶元素来实现不同情况输出十六进制
{
case(10):{ //当栈顶元素为10输出'a'
printf("a");
Pop(S);
break;
}
case(11):{ //当栈顶元素为11输出'b'
printf("b");
Pop(S);
break;
}
case(12):{ //当栈顶元素为12输出'c'
printf("c");
Pop(S);
break;
}
case(13):{ //当栈顶元素为13输出'd'
printf("d");
Pop(S);
break;
}
case(14):{ //当栈顶元素为14输出'e'
printf("e");
Pop(S);
break;
}
case(15):{ //当栈顶元素为15输出'f'
printf("f");
Pop(S);
break;
}
default:{ //其他情况输出0--9
printf("%d",Pop(S));
break;
}
}
}
printf("\n\n");
}
int BTranD(Stack *S,int i) //二进制 -> 十进制
{
int n=i,D=0;
while(i>0)
{
Push(S,i%10);
i /= 10;
}
while(S->top > -1)
{
int b = (int)pow(2,S->top); //把当前位数上的1或0乘以2的S->top次方
D += Pop(S)*b;
}
printf("%d转换为十进制为:%d\n\n",n,D);
return D;
}
int HTranD(Stack *S,char s[]) //十六进制 -> 十进制
{
int len = strlen(s),D=0,h; //算出字符串的长度,D为十进制加总
char o[10]; //将原字符串保留到o中
strcpy(o,s);
for(int i=0; i<len; i++) //根据不同情况实现十进制加总
{
h = (int)pow(16,(len-1)-i); //当前位数下应该乘以的16的某次幂
if(s[i]<97) //当前字符为0-9时,数字乘以16的某次幂
{ //97和48是根据字符转换为ascII码进行的运算
D += (s[i]-48)*h;
}
else if(s[i] == 'a') //当前字符为'a'
{
D += 10*h;
}
else if(s[i] == 'b') //当前字符为'b'
{
D += 11*h;
}
else if(s[i] == 'c') //当前字符为'c'
{
D += 12*h;
}
else if(s[i] == 'd') //当前字符为'd'
{
D += 13*h;
}
else if(s[i] == 'e') //当前字符为'e'
{
D += 14*h;
}
else //当前字符为'f'
{
D += 15*h;
}
}
printf("0X%s转换为十进制为:%d\n",o,D);
return D;
}
void BTranH(Stack *S,int i) //二进制 -> 十六进制
{
DTranH(S,BTranD(S,i)); //二进制 -> 十进制 -> 十六进制
}
void HTranB(Stack *S,char s[]) //十六进制 -> 二进制
{
DTranB(S,HTranD(S,s)); //十六进制 -> 十进制 -> 二进制
}
int main()
{
Stack *S;
S = (Stack*)malloc(sizeof(Stack)); //为栈分配空间
InitStack(S);
while(1)
{
printf("**********进制转换**********\n");//根据用户输入实现不同功能
printf("1.二 -> 十\n");
printf("2.二 -> 十六\n");
printf("3.十六 -> 二\n");
printf("4.十六 -> 十\n");
printf("5.十 -> 二\n");
printf("6.十 -> 十六\n");
int o,i;
printf("功能:");
scanf("%d",&o);
if(o==1||o==2||o==5||o==6)
{
printf("要转换的数字:");
scanf("%d",&i);
}
switch(o) //根据用户输入实现不同功能
{
case(1):{
BTranD(S,i);
break;
}
case(2):{
BTranH(S,i);
break;
}
case(3):{
char s[0];
printf("要转换的数字:0X");
scanf("%s",&s);
HTranB(S,s);
break;
}
case(4):{
char s[0];
printf("要转换的数字:0X");
scanf("%s",&s);
HTranD(S,s);
break;
}
case(5):{
DTranB(S,i);
break;
}
case(6):{
DTranH(S,i);
break;
}
default:printf("输入错误\n");break;
}
}
}
运行结果