不错,我也有一个和大家分享下。可以从十进制转换成2至16进制。不过用处不大了
#include<iostream.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>
#include<process.h> /* exit() */
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef int Status;
typedef int SElemType;
typedef int Boolean;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
// 栈是由栈底指针和栈顶指针以及栈的容量组成。top-1便是栈的长度
Status InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(100 * sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status PushStack(SqStack &S, SElemType e)
{
if(S.top - S.base >= S.stacksize)
{
S.base = (SElemType *)realloc(S.base,(S.stacksize+10) * sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
Status PopStack(SqStack &S, SElemType &e)
{
if(S.top == S.base) return ERROR;
e = * --S.top;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top == S.base)
return TURE;
else
return FALSE;
return OK;
}
void conversion(int N,int r)
{
SqStack S;
SElemType e;
InitStack(S);
if(N<0)
{
cout<<"您输入的数据不在范围之内!";
cout<<endl;
return;
}
/*if(!N)
PushStack(S,0);*/
while(N)
{
PushStack(S,N%r);
N = N/r;
}
while(!StackEmpty(S))
{
PopStack(S,e);
if(e<=9)
cout<<e;
else
cout<<char(e+55);
}
cout<<endl;
}
void main()
{
int N,r;
do
{
cout<<"请输入任意一个整数:
";
cin>>N;
cout<<endl;
cout<<"请输入要转换的进制:
";
cin>>r;
cout<<endl;
cout<<"转换数据为:";
conversion(N,r);
system("cls");
}while(N);
}