1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAXSIZE 100 4 5 /*链栈类型定义*/ 6 typedef struct node 7 { 8 int data1[MAXSIZE]; 9 int top; 10 }StackNode; 11 12 /*队列类型定义*/ 13 typedef struct point 14 { 15 int data2[MAXSIZE]; 16 int frot, rear; 17 }LQueue; 18 19 /*整数部分转换*/ 20 void Push_LinkStack(StackNode *s,int z,int k) 21 { 22 int n; 23 while(z!=0) 24 { 25 n=z%k; 26 z=z/k; 27 if(s->top==MAXSIZE-1) 28 { 29 printf("栈满!"); 30 return; 31 } 32 else 33 { 34 s->top++; 35 s->data1[s->top]=n; 36 } 37 } 38 39 } 40 41 /*小数部分转换*/ 42 void In_LQueue(LQueue *q,float f,int k,int m) 43 { 44 int i,j; 45 f=f*k; 46 for(i=0;i<m;i++) 47 { 48 j=(int)f; 49 if(q->rear==MAXSIZE-1) 50 { 51 printf("队满!"); 52 return; 53 } 54 else 55 { 56 q->rear++; 57 q->data2[q->rear]=j; 58 } 59 f=f-j; 60 f=f*k; 61 } 62 } 63 64 void Output(StackNode *s,LQueue *q,int m) 65 { 66 int y,i; 67 int x,z; 68 if(s->top==-1) 69 { 70 printf("栈空"); 71 } 72 else 73 { 74 while(s->top!=-1) 75 { 76 x=s->data1[s->top]; 77 s->top--; 78 if(x<=9) 79 { 80 printf("%d",x); 81 } 82 else 83 { 84 z=x-10; 85 printf("%c",'A'+z); 86 } 87 } 88 } 89 printf("."); 90 while(q->frot!=q->rear) 91 { 92 q->frot++; 93 y=q->data2[q->frot]; 94 if(y<=9) 95 { 96 printf("%d",y); 97 } 98 else 99 { 100 i=y-10; 101 printf("%c",'A'+i); 102 } 103 } 104 printf(" "); 105 } 106 107 void menu() 108 { 109 printf("********************* "); 110 printf("* 进制转化器 * "); 111 printf("*1----------开始转换* "); 112 printf("*0--------------退出* "); 113 printf("********************* "); 114 } 115 116 int main() 117 { 118 StackNode *s; 119 s=(StackNode *)malloc(sizeof(StackNode)); 120 s->top=-1; 121 LQueue *q; 122 q=(LQueue *)malloc(sizeof(LQueue)); 123 q->frot=-1; 124 q->rear=-1; 125 float n,f; 126 int k,sel,z,m; 127 do 128 { 129 menu(); 130 printf(" 请输入你的选择:"); 131 scanf("%d",&sel); 132 switch(sel) 133 { 134 case 1:printf(" 请输入一个十进制的非负小数:"); 135 scanf("%f",&n); 136 printf(" 要将其转换成几进制数; "); 137 printf("(2、8、16) "); 138 printf("你的选择:"); 139 scanf("%d",&k); 140 printf(" 结果需保留几位小数:"); 141 scanf("%d",&m); 142 z=(int)n; 143 f=n-z; 144 Push_LinkStack(s,z,k); 145 In_LQueue(q,f,k,m); 146 Output(s,q,m); 147 } 148 }while(sel!=0); 149 return 0; 150 }