给你个新的思路
如果是要进行任意的进制
就要考虑到有字符
所以进栈的元素应该字符型为最优选
#include <stdio.h>
#include <stdlib.h>
#define MAXS 8//
#define ADD
8//
typedef struct
{
char *base;
char *top;
int stacksize;
}Sqstack;
void InitStack( Sqstack &s)
{
s.base = (char *) malloc (MAXS*sizeof(char));
if( !s.base )
exit(0);
s.top = s.base;
s.stacksize = MAXS;
}
void Push( Sqstack &s, char temp )
{
if( s.top-s.base>=MAXS )
{
s.base =(char *) realloc (s.base, (MAXS+ADD)*sizeof(char));
if( !s.base )
exit(0);
s.top = s.base + s.stacksize;
s.stacksize += ADD;
}
*s.top++ = temp;
}
void Pop( Sqstack &s, char &temp )
{
if( s.top==s.base )
return;
temp = *--s.top;
}
int main()
{
Sqstack s;
InitStack(s);
char c;
int n, exchange;
int temp;
printf("input the number:");
scanf("%d", &n);
printf("input the number you exchange scale:");
scanf("%d", &exchange);
while( n )
{
temp= n%exchange;
if( temp>=10 )
Push( s, 'A'+temp-10 );
else
Push( s, 48+temp );
n = n/exchange;
}
while( s.base != s.top )
{
Pop( s, c );
printf("%c",c);
}
printf("\n");
return 0;
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
17进制数