《数据结构-十进制转换为十六进制C语言版.doc》由会员分享,可在线阅读,更多相关《数据结构-十进制转换为十六进制C语言版.doc(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date数据结构-十进制转换为十六进制C语言版#include#include#include#include#define OK 1#define ERROR 0#define OVERFLOW -2#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int Status;typedef int SElem
2、Type;typedef struct SElemType *base; SElemType *top; int stacksize; SqStack;SqStack S;/栈的初始化Status InitStack(SqStack &S) S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType); if(!S.base) exit(OVERFLOW); S.top = S.base; return OK;/入栈Status Push(SqStack &S,SElemType e) if(S.top - S.base = S
3、.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) if(S.top = S.base) return ERROR; e = *-S.top; re
4、turn OK;int main() int num,e; int i = 0; /计数 InitStack(S); printf(请输入一个十进制数字:); scanf (%d,&num); while(num) Push(S,num % 16); num /= 16; i+; printf(将数字转换为十六进制:); while(i) Pop(S,e); /* switch(e) case 1: printf(%d,e); break; case 2: printf(%d,e); break; case 3: printf(%d,e); break; case 4: printf(%d,e
5、); break; case 5: printf(%d,e); break; case 6: printf(%d,e); break; case 7: printf(%d,e); break; case 8: printf(%d,e); break; case 9: printf(%d,e); break; case 10: printf(%c,A); break; case 11: printf(%c,B); break; case 12: printf(%c,C); break; case 13: printf(%c,D); break; case 14: printf(%c,E); break; case 15: printf(%c,F); break; */ printf(%X,e); i-; return 0;-