十六进制转换
十进制转换为十六进制——利用栈的“先进后出”的思想
题目:设计一个进制转换程序,使用顺序栈设计一个把十进制数转换为十六进制数的接口,实现当通过键盘输入一个非负的十进制数,可以在终端输出对应的十六进制数。
思路:
1.输入一个十进制数num
2.定义一个链表
3.将余数(num%16)放入链表中,再把商(num/16)赋值给新的十进制数,循环操作。
4.输出链表中所有元素。
5.释放内存
代码段
1.进制转换函数
/**********************************************************************************************
* func name : HexTransfo
* function : Hexadecimal number print
* func parameter :
@num :Decimal number
* @Head :address of head node
* return resuolt : Transform success result (true or false)
* author : liaojx2016@126.com
* date : 2024/04/25
* note : None
* modify history : None
* function section: v1.0
*
**********************************************************************************************/
//十六进制转换
bool HexTransfo(unsigned int num,StackLList_t *head )
{
int temp=num; //备份输入的十进制数
//循环,把十进制转为十六进制
while (temp>=16) {
StackLList_Push(head,temp%16); //把 十进制数/16 的余数放入栈内存
temp/=16; //往高位进位
}
StackLList_Push(head,temp); //把剩余的余数,即最高位放入栈内存
return true;
}
2.入栈函数
/**********************************************************************************************
* func name : StackLList_Push
* function : Do stack push for hexadecimal digit(0~F)
* func parameter :
* @Head :address of head node
@temp :Disposed remainder
* return resuolt : Stack push success result (true or false)
* author : liaojx2016@126.com
* date : 2024/04/25
* note : None
* modify history : None
* function section: v1.0
*
**********************************************************************************************/
//将十六进制(0~F)数字入栈
bool StackLList_Push(StackLList_t *Head,unsigned int temp)
{
char data;
data=Ch_Transfo(temp); //将数字 (0~15) 转化为对应的字符型 (0~F)
//1.创建新的结点,并对新结点进行初始化
StackLList_t *New = StackLList_NewNode(data);
if (NULL == New) {
printf("can not insert new node\n");
return false;
}
//2.判断链表是否为空,如果为空,则直接插入即可
if (NULL == Head->next) {
Head->next = New;
return true;
}
//3.如果链表为非空,则把新结点插入到链表的头部
New->next = Head->next;
Head->next = New;
return true;
}
注:函数形参为十进制整型,需要转化为十六进制字符型数字
/**********************************************************************************************
* func name : Ch_Transfo
* function : Transform decial digit(0~9) into hexadecimal digit(0~F)
* func parameter :
* @temp :decial digit(0~9)
* return resuolt : hexadecimal digit(0~F)
* author : liaojx2016@126.com
* date : 2024/04/25
* note : None
* modify history : None
* function section: v1.0
*
**********************************************************************************************/
//将数字 (0~15) 转化为对应的字符型 (0~F)
char Ch_Transfo(unsigned int temp)
{
//将十进制数字转化为十六进制数字
if ( temp<10 ) return 48+temp;
else return 97+temp-10; //当十进制数字大于等于10,转为小写字母字符(a~f)
}
3.输出函数
/**********************************************************************************************
* func name : Hex_Print
* function : Hexadecimal number print
* func parameter :
* @Head :address of head node
* return resuolt : Print success result (true or false)
* author : liaojx2016@126.com
* date : 2024/04/25
* note : None
* modify history : None
* function section: v1.0
*
**********************************************************************************************/
//十六进制输出函数
bool Hex_Print(StackLList_t *Head)
{
//当链表为空时,结束函数并输出提示
if (Head->next==NULL) {
printf("stack link list is empty\n");
return false;
}
//对链表的头结点的地址进行备份
StackLList_t *Phead = Head;
printf("num's Hexadecimal digit is ");
//输出十六进制的前导符
printf("0x");
//遍历每个结点,并输出数据域字符
do {
//把当前结点的直接后继作为新的结点
Phead = Phead->next;
printf("%c",Phead->data);
}
while(Phead->next);
printf("\n");
return true;
}
4.释放内存函数
/**********************************************************************************************
* func name : FreeMemory
* function : Free stack memory
* func parameter :
* @Head :address of head node
* return resuolt : None
* author : liaojx2016@126.com
* date : 2024/04/25
* note : None
* modify history : None
* function section: v1.0
*
**********************************************************************************************/
void FreeMemory(StackLList_t *head)
{
StackLList_t *p;
//循环,删除首结点,直到链表为空
while(head->next) {
p=head->next; //每次循环都把首结点备份,以便删除
head->next=p->next;
p->next=NULL;
free(p);
}
//删除头结点。彻底释放内存
free(head);
//printf("stack memory free success!\n");
}
5.主函数
int main(int argc, char const *argv[])
{
StackLList_t *head=StackLList_Create();
unsigned int num;
printf("Please input a number : ");
scanf("%d",&num);
HexTransfo(num,head);
Hex_Print(head);
FreeMemory(head);
return 0;
}
测试输出结果


浙公网安备 33010602011771号