实现将十进制数转换成八进制数

来源:百度知道 编辑:UC知道 时间:2024/06/30 13:00:18
必须用栈来实现

一个根本不是用栈,一个写得太麻烦,用我这个吧。

void conversion()
{
pSqStack S;
SElemType e;
int n;
InitStack(&S);
printf("Input a number to convert to OCT:\n");
scanf("%d",&n);
if(n<0)
{
printf("\nThe number must be over 0.");
return;
}
if(!n) Push(S,0);
while(n)
{
Push(S,n%8);
n=n/8;
}
printf("the result is: ");
while(!StackEmpty(*S))
{
Pop(S,&e); printf("%d",e);
}
}

#include <iostream>
using namespace std;
int stack[101],top;
int main()
{
top=0;
int n;
cin>>n;
stack[top]=0;
while(n)
stack[top++]=n%8,n/=8;//入栈
while(top--)
cout<<stack[top];//出栈
cout<<endl;
return 0;
}
用数组模拟栈

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
struct Stack