鱼C论坛

 找回密码
 立即注册
查看: 1223|回复: 1

[技术交流] 一个二进制转换器.....

[复制链接]
发表于 2020-8-28 09:08:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define ok 1
  5. #define error 0
  6. #define MAX_STACK_SIZE 10
  7. #define SIZE 32
  8. typedef int Status;
  9. typedef char ElemType;

  10. typedef struct stack{
  11.         ElemType *top;
  12.         ElemType *base;
  13.         int stackSize;
  14. }Stack,*stackList;



  15. Status Pop(Stack *stack,ElemType *e){
  16.         if(stack->top==stack->base)
  17.                 return error;
  18.         stack->top--;
  19.         *e=*(stack->top);
  20.         *(stack->top)=0;
  21.         return ok;
  22. }

  23. Status Push(Stack *stack,ElemType e){
  24.        
  25.         if(stack->stackSize<=0){
  26.                 stack->top=(ElemType *)malloc(sizeof(ElemType)*MAX_STACK_SIZE);
  27.                 if(!stack->top)
  28.                         return error;
  29.         }
  30.        
  31.          stack->stackSize=MAX_STACK_SIZE;
  32.         * stack->top=e;
  33.         * stack->top++;
  34.          stack->stackSize--;
  35.          return ok;
  36. }

  37. Status initStack(Stack *stack){
  38.        
  39.         stack->base=(ElemType *)malloc(sizeof(ElemType)*MAX_STACK_SIZE);
  40.                
  41.         if(!stack->base)
  42.                 return error;
  43.                
  44.         int i;
  45.         for(i=0;i<MAX_STACK_SIZE;i++)
  46.                 stack->base[i]=0;
  47.        
  48.         stack->top=stack->base;
  49.         stack->stackSize=MAX_STACK_SIZE;
  50.        
  51.         return ok;
  52.        
  53. }

  54. ElemType * toHex(Stack *stack){
  55.         ElemType popData;
  56.         ElemType pop;
  57.         Stack stack02;
  58.         initStack(&stack02);
  59.         int sum=0;
  60.         int c=0;
  61.         int count=0;
  62.         ElemType tempData;
  63.         ElemType *retData=(ElemType *)malloc(sizeof(ElemType)*SIZE);
  64.         while(Pop(stack,&popData)){
  65.                 int oper=popData-48;
  66.                 sum+=oper*pow(2,c++);
  67.                 count++; //一个16进制位由4个二进制为构成,count用来判断是否已经pop出一个16进制位
  68.                 if(count==4){
  69.                
  70.                         //当超过9时要用字母表示
  71.                         if(sum>9){
  72.                                 sum=sum%9;
  73.                                 switch(sum){
  74.                                         case 1:
  75.                                                 tempData='a';
  76.                                                 break;
  77.                                         case 2:
  78.                                                 tempData='b';
  79.                                                 break;
  80.                                         case 3:
  81.                                                 tempData='c';
  82.                                                 break;
  83.                                         case 4:
  84.                                                 tempData='d';
  85.                                                 break;
  86.                                         case 5:
  87.                                                 tempData='e';
  88.                                                 break;
  89.                                         case 6:
  90.                                                 tempData='f';       
  91.                                                 break;         
  92.                                 }
  93.                         }else{
  94.                                 tempData=48+sum; //48是字符0的ascll码
  95.                         }
  96.                        
  97.                         Push(&stack02,tempData); //将得到的一个16进制数入栈
  98.                         count=0; //count重新计数
  99.                         sum=0;
  100.                         c=0;       
  101.                 }
  102.         }
  103.        
  104.         //计算剩下的不满四个位的二进制数,再一次push
  105.         if(sum!=0){
  106.                 tempData=48+sum;
  107.                 Push(&stack02,tempData);
  108.         }
  109.         int index=0;
  110.         //将栈2中的数据依次弹出,保存在retData数组中,作为字符输出
  111.         while(Pop(&stack02,&pop)){
  112.                 retData[index]=pop;
  113.                 index++;
  114.         }
  115.         retData[index]='\n';
  116.         return retData;
  117.        
  118. }

  119. int toDecimal(Stack *stack){
  120.         ElemType popData;
  121.         int sum=0;
  122.         int c=0;
  123.         while(Pop(stack,&popData)){
  124.                 int oper=popData-48;
  125.                 sum+=oper*pow(2,c++);
  126.         }
  127.        
  128.         return sum;
  129. }

  130. void inputData(Stack *stack){
  131.        
  132.         char check;       
  133.         initStack(stack);
  134.         printf("输入一个二进制数(以回车结束):");
  135.         scanf("%c",&check);
  136.         while(check!='0'){
  137.                 Push(stack,check);
  138.                 scanf("%c",&check);               
  139.         }
  140.         getchar();

  141. }



  142. int main(int argc, char *argv[]) {
  143.         int choice;
  144.         printf("++++++++++++++++++++二进制转换工具+++++++++++++++++\n\n");
  145.         printf("作者:鱼的七秒记忆\n");
  146.         printf("时间: 2020-08/23\n\n");
  147.         printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  148.         do{
  149.                  Stack stack;
  150.                     inputData(&stack);
  151.                     printf("请选择操作\n\n");
  152.                     printf("0.退出\n");
  153.                     printf("1.转十进制\n");
  154.              printf("2.转十六进制\n\n");
  155.              printf("选项:");
  156.                  int decimal;
  157.                  char *ret;
  158.                  scanf("%d",&choice);
  159.                          switch(choice){
  160.                                 case 1:
  161.                                         decimal=toDecimal(&stack);
  162.                                         printf("%d\n\n",decimal);
  163.                                         break;
  164.                                 case 2:
  165.                                         ret=toHex(&stack);
  166.                                         printf("%s\n\n",ret);
  167.                                         free(ret);
  168.                                         break;       
  169.                         }
  170.    
  171.         }while(choice!=0);
  172.    
  173.         system("pause");
  174.         return 0;
  175. }
复制代码

一个自己写的二进制转换器,可能会有漏洞,望大家指出

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-1 14:51:12 | 显示全部楼层
学习中
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 02:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表