匿名用户
1级
2008-07-04 回答
不用系统的函数,用栈实现
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
struct stack
{
char* base;
char* top;
int length;
};
void InitStack(struct stack *p)
{
p->base=(char*)malloc(100);
p->top=p->base;
*(p->top)=0;
p->length=0;
}
bool push(struct stack *p,char a)
{
if(p->length >=100) return false;
*(p->top) = a;
p->top++;
*(p->top)=0;
p->length++;
return true;
}
bool pop(struct stack *p)
{
if(p->length <= 0) return false;
p->top--;
*(p->top)=0;
p->length--;
return true;
}
char Get_Top(struct stack *p)
{
if(p->length <= 0) return false;
return *(p->top-1);
}
int main(int argc, char* argv[])
{
struct stack MyStrack;
InitStack(&MyStrack);
int a;
printf("请输入一个整数:");
scanf("%d",&a);
while(a)
{
int tempint=a%16;
char temp = tempint>9 ? 'A'+tempint-10:'0'+tempint;
push(&MyStrack,temp);
a=a/16;
}
int len=MyStrack.length;
printf("转化为十六进制为:");
for(int i=0;i
{
char temp=Get_Top(&MyStrack);
if(!temp) return 0;
pop(&MyStrack);
printf("%c",temp);
}
return 0;
}