把十进制的数转化为八进制的数然后输出。求大神告诉我是哪里出问题了,怎么改,蟹蟹!!!
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkLi;
Status Push(SqStack &S,SElemType e)
{ p=new StackNode;
p->next=S;
S=p;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S==NULL)
return ERROR;
e=S->data;
p=S;
S=S->next;
delete p;
return OK;
}
void conversion(int N)
{ Initstack(S);
while(N)
{ Push(S,N%8);
N=N/8;
}
while(!StackEmpty(S))
{ Pop(S,e);
printf("%d",e);
}
}
int main()
{ printf("请输入十进制的整数:");
scanf("%3d",&N);
Push(S,e); Pop(S,e);
printf("输出八进制的数为:");
conversion(N)
}
你的程序结构定义部分“完全”不对,若你是要用栈来做,可以参考以下程序
#include <stdio.h>
#define StackSize 20 /*假定预分配的栈空间最多为10*/
typedef int DataType; /*栈的元素类型设为整型*/
typedef struct
{
DataType data[StackSize];
int top;
} SeqStack;
void InitStack(SeqStack *S) /*初始栈*/
{
S->top = -1;
}
int StackEmpty(SeqStack *S) /*判栈空*/
{
if(S->top == -1)
return 1;
else
return 0;
}
int StackFull(SeqStack *S) /*判栈满*/
{
if(S->top == StackSize - 1)
return 1;
else
return 0;
}
void Push(SeqStack *S, DataType x) /*进栈*/
{
if(StackFull(S))
printf("Stack overflow"); /*上溢退出*/
else
S->data[++(S->top)] = x; /*栈顶指针加1后将x进栈*/
}
DataType Pop(SeqStack *S) /*出栈*/
{
if(StackEmpty(S))
printf("Stack underflow"); /*下溢退出*/
else
return S->data[(S->top)--]; /*栈顶指针返回后将栈顶指针减1*/
}
void conversion(int N)
{
SeqStack S;
DataType e;
InitStack(&S);
while(N)
{
Push(&S,N%8);
N=N/8;
}
while(!StackEmpty(&S))
{
e=Pop(&S);
printf("%d",e);
}
}
int main()
{
int N;
printf("请输入十进制的整数:");
scanf("%3d",&N);
printf("输出八进制的数为:");
conversion(N);
}