|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define ok 1
- #define error 0
- #define MAX_STACK_SIZE 10
- #define SIZE 32
- typedef int Status;
- typedef char ElemType;
- typedef struct stack{
- ElemType *top;
- ElemType *base;
- int stackSize;
- }Stack,*stackList;
- Status Pop(Stack *stack,ElemType *e){
- if(stack->top==stack->base)
- return error;
- stack->top--;
- *e=*(stack->top);
- *(stack->top)=0;
- return ok;
- }
- Status Push(Stack *stack,ElemType e){
-
- if(stack->stackSize<=0){
- stack->top=(ElemType *)malloc(sizeof(ElemType)*MAX_STACK_SIZE);
- if(!stack->top)
- return error;
- }
-
- stack->stackSize=MAX_STACK_SIZE;
- * stack->top=e;
- * stack->top++;
- stack->stackSize--;
- return ok;
- }
- Status initStack(Stack *stack){
-
- stack->base=(ElemType *)malloc(sizeof(ElemType)*MAX_STACK_SIZE);
-
- if(!stack->base)
- return error;
-
- int i;
- for(i=0;i<MAX_STACK_SIZE;i++)
- stack->base[i]=0;
-
- stack->top=stack->base;
- stack->stackSize=MAX_STACK_SIZE;
-
- return ok;
-
- }
- ElemType * toHex(Stack *stack){
- ElemType popData;
- ElemType pop;
- Stack stack02;
- initStack(&stack02);
- int sum=0;
- int c=0;
- int count=0;
- ElemType tempData;
- ElemType *retData=(ElemType *)malloc(sizeof(ElemType)*SIZE);
- while(Pop(stack,&popData)){
- int oper=popData-48;
- sum+=oper*pow(2,c++);
- count++; //一个16进制位由4个二进制为构成,count用来判断是否已经pop出一个16进制位
- if(count==4){
-
- //当超过9时要用字母表示
- if(sum>9){
- sum=sum%9;
- switch(sum){
- case 1:
- tempData='a';
- break;
- case 2:
- tempData='b';
- break;
- case 3:
- tempData='c';
- break;
- case 4:
- tempData='d';
- break;
- case 5:
- tempData='e';
- break;
- case 6:
- tempData='f';
- break;
- }
- }else{
- tempData=48+sum; //48是字符0的ascll码
- }
-
- Push(&stack02,tempData); //将得到的一个16进制数入栈
- count=0; //count重新计数
- sum=0;
- c=0;
- }
- }
-
- //计算剩下的不满四个位的二进制数,再一次push
- if(sum!=0){
- tempData=48+sum;
- Push(&stack02,tempData);
- }
- int index=0;
- //将栈2中的数据依次弹出,保存在retData数组中,作为字符输出
- while(Pop(&stack02,&pop)){
- retData[index]=pop;
- index++;
- }
- retData[index]='\n';
- return retData;
-
- }
- int toDecimal(Stack *stack){
- ElemType popData;
- int sum=0;
- int c=0;
- while(Pop(stack,&popData)){
- int oper=popData-48;
- sum+=oper*pow(2,c++);
- }
-
- return sum;
- }
- void inputData(Stack *stack){
-
- char check;
- initStack(stack);
- printf("输入一个二进制数(以回车结束):");
- scanf("%c",&check);
- while(check!='0'){
- Push(stack,check);
- scanf("%c",&check);
- }
- getchar();
-
- }
-
- int main(int argc, char *argv[]) {
- int choice;
- printf("++++++++++++++++++++二进制转换工具+++++++++++++++++\n\n");
- printf("作者:鱼的七秒记忆\n");
- printf("时间: 2020-08/23\n\n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
- do{
- Stack stack;
- inputData(&stack);
- printf("请选择操作\n\n");
- printf("0.退出\n");
- printf("1.转十进制\n");
- printf("2.转十六进制\n\n");
- printf("选项:");
- int decimal;
- char *ret;
- scanf("%d",&choice);
- switch(choice){
- case 1:
- decimal=toDecimal(&stack);
- printf("%d\n\n",decimal);
- break;
- case 2:
- ret=toHex(&stack);
- printf("%s\n\n",ret);
- free(ret);
- break;
- }
-
- }while(choice!=0);
-
- system("pause");
- return 0;
- }
复制代码
一个自己写的二进制转换器,可能会有漏洞,望大家指出 |
|