level 2
霍世洁
楼主
#include<stdio.h>
#include<malloc.h>
typedef struct
{
int stack[200];
int top;
}seqstack;
seqstack *push(seqstack *p,int i)
{
p->stack[p->top]=i;
p->top++;
return(p);
}
void main()
{
int i,j,k;
do
{
printf("请输入需要转换的进制!\n");
fflush(stdin);
scanf("%d",&j);
}while(j>9||j<2);
printf("请输入需要转换的数!\n");
fflush(stdin);
scanf("%d",&i);
seqstack *p;
p=(seqstack *)malloc(sizeof(seqstack));
p->top=0;
while(i!=0&&p->top!=200)
{
k=i%j;
i=i/j;
p=push(p,k);
}
if(p->top==200)
printf("内存已满!");
else
{
printf("结果为:");
while(p->top!=0)
{
p->top--;
printf("%d",p->stack[p->top]);
}
printf("\n");
}
}
在转换到2进制时,如果数过大就不对了,是为啥?
2013年11月11日 10点11分
1
#include<malloc.h>
typedef struct
{
int stack[200];
int top;
}seqstack;
seqstack *push(seqstack *p,int i)
{
p->stack[p->top]=i;
p->top++;
return(p);
}
void main()
{
int i,j,k;
do
{
printf("请输入需要转换的进制!\n");
fflush(stdin);
scanf("%d",&j);
}while(j>9||j<2);
printf("请输入需要转换的数!\n");
fflush(stdin);
scanf("%d",&i);
seqstack *p;
p=(seqstack *)malloc(sizeof(seqstack));
p->top=0;
while(i!=0&&p->top!=200)
{
k=i%j;
i=i/j;
p=push(p,k);
}
if(p->top==200)
printf("内存已满!");
else
{
printf("结果为:");
while(p->top!=0)
{
p->top--;
printf("%d",p->stack[p->top]);
}
printf("\n");
}
}
在转换到2进制时,如果数过大就不对了,是为啥?