请帮忙看看下面的程序的问题!它是将十进制转换为八进制,用栈实现的程序。

来源:百度知道 编辑:UC知道 时间:2024/06/05 06:22:11
程序是可以运行的,只是结果有问题。
#include <iostream>
#include <conio.h>
using namespace std;
typedef char datatype;
struct node{
datatype data;
node *next;
};
typedef node *pointer;

void push(pointer &t,datatype x)
{pointer p;
p=new node ;
p->data=x;
p->next=t;
t=p;}

void pop(pointer &t,datatype y)
{pointer p;
if(t==NULL)
cout<<"empty";
else{
p=t;
t=t->next;
y=p->data;
free(p);}
}

int main()
{
pointer ss;
ss=NULL;
int m=0;
int n;
cout<<"Input a number to convert to octal:";
cin>>n;
if(n<=0)
{
cout<<"the number must to be over 0.";
return 0;}

while(n>0)
{
push(ss,n%8);
n=n/8;
}
cout<<"the result is: ";
while(ss!=NULL)
{ pop(ss,m);
cou

下面是改过的代码,修改的地方标出来了

#include <iostream>
#include <conio.h>
using namespace std;
typedef char datatype;
struct node{
datatype data;
node *next;
};
typedef node *pointer;

void push(pointer &t,datatype x)
{pointer p;
p=new node ;
p->data=x;
p->next=t;
t=p;}

void pop(pointer &t,datatype &y)//调用用引用,否则改不了y的值
{pointer p;
if(t==NULL)
cout<<"empty";
else{
p=t;
t=t->next;
y=p->data;
free(p);}
}

int main()
{
pointer ss;
ss=NULL;
char m=0;//类型改为char
int n;
cout<<"Input a number to convert to octal:";
cin>>n;
if(n<=0)
{
cout<<"the number must to be over 0.";
return 0;}

while(n>0)
{
push(ss,n%8);
n=n/8;
}
cout<<"the result is: ";
while(ss