高分!呵!

handywu 2004-03-23 09:06:16
怎样利用堆栈特性,将一个十进制数转为十六进制数输出
请给我源代码解注释,谢谢!
...全文
54 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
happlyman 2004-03-23
  • 打赏
  • 举报
回复
哈哈,没关系,看看大家这么热情,多好啊
wshcdr 2004-03-23
  • 打赏
  • 举报
回复
我觉得还是用队好
wshcdr 2004-03-23
  • 打赏
  • 举报
回复
void Convert(int iNum, Stack s)
{
while(iNum>15)
{
s.push(iNum/16);
iNum=iNum%16;
}
s.push(iNum);

这里就反序输出栈s里的数字就可以了(同时要加一些iNum大于9时要变为相应的A,B,C,D,E)
}
handywu 2004-03-23
  • 打赏
  • 举报
回复
我不是楼主,我是楼猪,刚刚失恋,脑子里空空的,
大家来骂我吧,
让砖头来得更猛烈些吧!
wythust 2004-03-23
  • 打赏
  • 举报
回复
/* c3-1.h 栈的顺序存储表示 */
#define STACK_INIT_SIZE 10 /* 存储空间初始分配量 */
#define STACKINCREMENT 2 /* 存储空间分配增量 */
typedef struct SqStack
{
SElemType *base; /* 在栈构造之前和销毁之后,base的值为NULL */
SElemType *top; /* 栈顶指针 */
int stacksize; /* 当前已分配的存储空间,以元素为单位 */
}SqStack; /* 顺序栈 */

/* bo3-1.c 顺序栈(存储结构由c3-1.h定义)的基本操作(9个) */
Status InitStack(SqStack *S)
{ /* 构造一个空栈S */
(*S).base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!(*S).base)
exit(OVERFLOW); /* 存储分配失败 */
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}

Status DestroyStack(SqStack *S)
{ /* 销毁栈S,S不再存在 */
free((*S).base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stacksize=0;
return OK;
}

Status ClearStack(SqStack *S)
{ /* 把S置为空栈 */
(*S).top=(*S).base;
return OK;
}

Status StackEmpty(SqStack S)
{ /* 若栈S为空栈,则返回TRUE,否则返回FALSE */
if(S.top==S.base)
return TRUE;
else
return FALSE;
}

int StackLength(SqStack S)
{ /* 返回S的元素个数,即栈的长度 */
return S.top-S.base;
}

Status GetTop(SqStack S,SElemType *e)
{ /* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */
if(S.top>S.base)
{
*e=*(S.top-1);
return OK;
}
else
return ERROR;
}

Status Push(SqStack *S,SElemType e)
{ /* 插入元素e为新的栈顶元素 */
if((*S).top-(*S).base>=(*S).stacksize) /* 栈满,追加存储空间 */
{
(*S).base=(SElemType *)realloc((*S).base,((*S).stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!(*S).base)
exit(OVERFLOW); /* 存储分配失败 */
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCREMENT;
}
*((*S).top)++=e;
return OK;
}

Status Pop(SqStack *S,SElemType *e)
{ /* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
if((*S).top==(*S).base)
return ERROR;
*e=*--(*S).top;
return OK;
}

Status StackTraverse(SqStack S,Status(*visit)(SElemType))
{ /* 从栈底到栈顶依次对栈中每个元素调用函数visit()。 */
/* 一旦visit()失败,则操作失败 */
while(S.top>S.base)
visit(*S.base++);
printf("\n");
return OK;
}

贴完了,长是长了点,但这是最正统的数据结构算法了
herryhuang 2004-03-23
  • 打赏
  • 举报
回复
楼主好像也不明白什么叫“利用堆栈特性”

xstring(麻雀)的方法就很好,函数参数就是放在堆栈中传递的
wythust 2004-03-23
  • 打赏
  • 举报
回复
/* c3-1.h 栈的顺序存储表示 */
#define STACK_INIT_SIZE 10 /* 存储空间初始分配量 */
#define STACKINCREMENT 2 /* 存储空间分配增量 */
typedef struct SqStack
{
SElemType *base; /* 在栈构造之前和销毁之后,base的值为NULL */
SElemType *top; /* 栈顶指针 */
int stacksize; /* 当前已分配的存储空间,以元素为单位 */
}SqStack; /* 顺序栈 */
wythust 2004-03-23
  • 打赏
  • 举报
回复
/* c1.h */
#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
wythust 2004-03-23
  • 打赏
  • 举报
回复
typedef int SElemType; /* 定义栈元素类型为整型 */
#include"c1.h"
#include"c3-1.h" /* 采用顺序栈 */
#include"bo3-1.c" /* 利用顺序栈的基本操作 */

void conversion()
{ /* 对于输入的任意一个非负10进制整数,打印输出与其等值的16进制数 */
SqStack s;
unsigned n; /* 非负整数 */
SElemType e;
InitStack(&s); /* 初始化栈 */
printf("n(>=0)=");
scanf("%u",&n); /* 输入非负十进制整数n */
while(n) /* 当n不等于0 */
{
Push(&s,n%16); /* 入栈n除以16的余数(16进制的低位) */
n=n/16;
}
while(!StackEmpty(s)) /* 当栈不空 */
{
Pop(&s,&e); /* 弹出栈顶元素且赋值给e */
if(e<=9)
printf("%d",e);
else
printf("%c",e+55);
}
printf("\n");
}

void main()
{
conversion();
}
xstring 2004-03-23
  • 打赏
  • 举报
回复
局部变量就是在堆栈内啊
这是典型的用栈实现的

如果你觉得还不是的话

看下一个

void print_hex (unsigned int dec)
{
char hex [8]; // 32位的unsigned int最多有八个hex字符
int top = 8;

for (;dec != 0;)
{
unsigned int rem = dec % 16;
dec /= 16;

// 以下是将求得的字符入栈
if (rem >= 10)
hex [--top] = rem - 10 + 'A';
else
hex [--top] = rem + '0';
}

// 以下是将栈内的字符也就是16进制输出打印
for (;top<8;++top)
printf ("%c", hex [top]);
};
handywu 2004-03-23
  • 打赏
  • 举报
回复
请用堆栈的方法,呵呵!
我正在学堆栈!谢谢!
xstring 2004-03-23
  • 打赏
  • 举报
回复
void print_hex (unsigned int dec)
{
unsigned int rem = dec % 16;
dec /= 16;
if (dec != 0) // dec为零时要终止递归
print_hex (dec);
if (rem >= 10)
printf ("%c", rem - 10 + 'A');
else
printf ("%c", rem + '0');
};
xstring 2004-03-23
  • 打赏
  • 举报
回复
再改一下没有终止递归

void print_hex (unsigned int dec)
{
unsigned int rem = dec % 16;
dec /= 16;
if (dec != 0)
print_hex (dec);
if (rem >= 10)
printf ("%c", rem - 10 + 'A');
else
printf ("%c", rem + '0');
};
xstring 2004-03-23
  • 打赏
  • 举报
回复
void print_hex (unsigned int dec)
{
unsigned int rem = dec % 16;
dec /= 16;
print_hex (dec);
if (rem >= 10)
printf ("%c", rem - 10 + 'A');
else
printf ("%c", rem + '0');
};

举例子:
1234转换成16进制
假设结果是xyz
那么就有等示 1234 = x * 16 * 16 + y * 16 + z
也就是 1234 = (x * 16 + y) * 16 + z

所以z = 1234 % 16
且x * 16 + y = 1234 / 16

对于等示 x * 16 + y = 1234 / 16

又有y = 1234 / 16 % 16
x = 1234 / 16 / 16

这样x, y, z就全求出来了

上面代码用的递归
只要你看明白了我后面的解释又懂得递归就能看明白上面的代码
handywu 2004-03-23
  • 打赏
  • 举报
回复
这里作的是算法研讨,看看你的数据结构的功底咯,请用堆栈特性!谢谢!
oo 2004-03-23
  • 打赏
  • 举报
回复
用得着堆栈吗?
int n;
printf("%X",n);
chenzhichao2008 2004-03-23
  • 打赏
  • 举报
回复
1
playboyxp 2004-03-23
  • 打赏
  • 举报
回复
#define STACK_INIT_SIZE 100
#define STACKINCREAMENT 10
typedef int elemtype;
typedef struct
{
elemtype *base;
elemtype *top;
int stacksize;
}sqstack;
void initstack(sqstack *p)
{
p->base=(elemtype *)malloc(STACK_INIT_SIZE*sizeof(elemtype));
p->top=p->base;
}
void push(sqstack *p,elemtype str)
{
*p->top++=str;
}
elemtype pop(sqstack *p,elemtype *e)
{
if(p->base==p->top) return 1;
else
{
--p->top;
*e=*p->top;
}
}
void conversion(elemtype num)
{
elemtype e;
sqstack p;
initstack(&p);
while(num)
{
push(&p,num%16);
num=num/16;
}
while(p.base!=p.top)
{
pop(&p,&e);
printf("%d",e);
}
printf("\n");
}
main()
{
elemtype num;
printf("输入一个十进制数\n");
scanf("%d",&num);
conversion(num);
}


handywu 2004-03-23
  • 打赏
  • 举报
回复
就你不热情,听海人!嘻嘻!

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧