c语言用栈实现八进制转十进制,完整程序,求解

如题所述

第1个回答  2019-05-27
以下是我自己写的一个十进制数转化成任意进制数的程序,请参考:
#include<stdio.h>
#include<malloc.h>
#define
STACKSIZE
100
#define
StackIncrement
10
typedef
struct{
int
*base;
int
*top;
int
stacksize;
}SqStack;
SqStack
S;
int
w;
void
InitStack(){
S.base=(int*)malloc(STACKSIZE*sizeof(int));
if(!S.base)printf("存储空间已满\n");
else
{S.top=S.base;
S.stacksize=STACKSIZE;
}
}
void
GetTop(){
int
e;
if(S.top==S.base)printf("该栈为空\n");
else
e=*(S.top-1);
}
void
Push(int
e){
if(S.top-S.base>S.stacksize)
{
S.base=(int*)realloc(S.base,(S.stacksize+StackIncrement)*sizeof(int));
if(!S.base)printf("存储空间已满\n");
else
{S.top=S.base+S.stacksize;
S.stacksize+=StackIncrement;
}
}
else
*S.top++=e;
}
void
Pop(){
if(S.top==S.base)printf("该栈为空,不能删除\n");
w=*--S.top;
}
void
conversion()
{InitStack();
int
n,d,l;
printf("请输入一个十进制数:
");
scanf("%d",&n);
printf("\n");
printf("\n");
printf("请输入你要转化成的制数:
(小于10)
");
scanf("%d",&d);
printf("\n");
printf("\n");
printf("%d转换成%d进制的数为:",n,d);
while(n)
{Push(n%d);
n=n/d;
}
while(!(S.top==S.base)){Pop();printf("%d",w);}
printf("\n");
printf("\n================================================================================");
printf("\n

继续
请按:1\n");
printf("

退出
请按:2\n");
printf("\n================================================================================");
scanf("%d",&l);
switch(l)
{case
1
:printf("\n");conversion();break;
case
2
:break;
}
}
void
main()
{printf("********************************************************************************\n");
printf("

欢迎进入数制转换系统
☆\n");
printf("\n");
printf("
设计者:马兆瑞(信息09-2班)");
printf("********************************************************************************\n");
conversion();
}