(codes)a program convert a dec into a hex

// keyhex.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
# include <malloc.h>

#define ESC  0x001b  /* exit*/
# define    ERROR          -1
# define    OK             1
#define stackinitsize   10
#define stackincrement   5

 


typedef int status;

typedef struct{
  int *base;
  int *top;
  int stacksize;
}sqstack;

/*算法initstack()*/
status initstack(sqstack &s)
{
 s.base=(int*)malloc(stackinitsize*sizeof(int));
 if(!s.base)  return (-2);
 s.top=s.base;
 s.stacksize=stackinitsize;
 return 1;
}
/*算法stackempty()*/
status stackempty(sqstack &s)
{
    if(s.base==s.top)
        return 1;
    else return 0;
}

/*算法empty()*/
status push(sqstack &s,int e)
{
   if(s.top-s.base>=s.stacksize)
   {
      s.base=(int*)realloc(s.base,(s.stacksize+stackincrement)*sizeof(int));
      if(!s.base) return(-2);
      s.top=s.base+s.stacksize;
      s.stacksize+=stackincrement;
   }
   *s.top++=e;
   return 1;
}
/*算法pop()*/
status pop(sqstack &s,int &e)
{
   if(s.top==s.base)return 0;
   e=*--s.top;
   return 1;
}

void get(char key)
{  
 int n,e;
 sqstack s;
 initstack(s);
 n=(int)key;
 if(n!=10){
  printf(" %c/t/t%d/t/t",key,key);
 }
  while(n&&n!=10)
    {  push(s,n%16);
       n=(n-n%16)/16;
    };
 while(!stackempty(s))
 {  pop(s,e);
    switch(e){
        case 10:e='A';printf("%c",e);break;
        case 11:e='B';printf("%c",e);break;
        case 12:e='C';printf("%c",e);break;
        case 13:e='D';printf("%c",e);break;
        case 14:e='E';printf("%c",e);break;
        case 15:e='F';printf("%c",e);break;
        default: printf("%d",e);  };
 };
 if(n!=10){printf(" H/n");}
}
/*================main=============================*/
void main()
{  
  char key;
  printf("/t输入一个字符串,顺序输出它每个字符的ASCII码和ASCII码对应的十六进制码:/n");
  printf("说明:按ctrl+c退出程序/t/t/t/t|作者:熊磊/n/t/t/t/t/t/t|制作日期:2006年6月/n/t/t/t/t/t/t|联系我:cexodddf@163.com/n");
  printf("字符           ASCII          十六进制码/n");
  while(1){
          scanf("%c",&key);
          get(key);
          continue;
  };
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值