注册 登录
编程论坛 数据结构与算法

新手求教,用栈实现的数制转换,哪儿不对啊

宁缺 发布于 2015-05-10 20:45, 2270 次点击
#include<stdio.h>
#include<stdlib.h>
#define  STACK_INIT_SIZE  100
#define  STACKINCREMENT    10
typedef struct {
   int * base;
   int * top;
   int  stacksize;
}SqStack;
void InitStack(SqStack&S){
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)printf("overflow");
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
   }
void Push (SqStack &S,int e){
     //插入元素e为新的栈顶元素
     if(S.top-S.base>=S.stacksize){
          //---栈满,追加存储空间---
          S.base=(int *)realloc(S.base,
                              (S.stacksize+STACKINCREMENT)
                               *sizeof(int));
          if(!S.base)printf("overflow");//存储分配失败
          S.top=S.base+S.stacksize;   
          S.stacksize+=STACKINCREMENT;}
    *S.top++=e;
   
}//Push
void  Pop( SqStack &S,int &e){
      //若栈不空,则删除s的栈顶元素,
      //用e返回其值,并返回OK;
      //否则返回ERROR
      if(S.top = S.base) printf("error");
       e = *--S.top;
      
} // Pop
int StackEmpty(SqStack&S){
if(S.top = S.base)
return 1;
else
return 0;
}
        
void  conversion (int N) {
        //对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数
            SqStack S;
            int e;
            InitStack(S);  //构造空栈
            while(N){
                     Push(S,N%8);
                     N = N/8; }
                 while (!StackEmpty(S)) {
                     
                       Pop(S, e);
                       printf("%",e);}
        } //Conversion
void main(){
    int N;
    printf("请输入一个非负十进制整数");
        scanf("%d",&N);
    conversion(N);

}
用栈实现数制转换,运行结果不对,哪儿错了啊
3 回复
#2
梦巷2015-05-17 10:37
#include<stdio.h>
#include<stdlib.h>
int i=0;
typedef struct node
{
   int date;
   struct node *next;   
}Nt;
 typedef struct stake
 {
     Nt *top;
     Nt *base;
 }stack;
  void initstack(stack *p )
  {
      Nt *p1;
      p->top=p1=(Nt*)malloc(sizeof(Nt));
      p->top=p->base;
      p1->next=NULL;
  }
 void push(stack *p,int n)
  {
          Nt *p1;
          p1=(Nt*)malloc(sizeof(Nt));
          p1->date=n;
        p1->next=p->top;
          p->top=p1;
          i++;
  }
  int *pop(stack *p,int *e)
  {   
   while(p->top!=p->base)
  {
    e=p->top;   
      p->top=p->top->next;
      return e;
  }
  }
  main( )
  { stack s;
    initstack(&s);
      int N;
      int b=1,m;
    int *e;
      printf("请输入一个整数\n");
      scanf("%d",&N);
      printf("请输入要转化成的进制数\n");
      scanf("%d",&m);
      do   
      {  
         push(&s,N%m);
        N=N/m;        
        
      }
      while(N!=0);
      
      while(b<=i)
      {
        e=pop(&s,&e);
     
      printf("%d",*e);
     b++;
  }
  }
#3
林月儿2015-05-17 11:51
回复 2楼 梦巷
#include<stdio.h>
#include<stdlib.h>
int i=0;
typedef struct node{
   int date;
   struct node *next;   
}Nt;
typedef struct stake{
     Nt *top;
     Nt *base;
}stack;
  void initstack(stack *p ){
      Nt *p1;
      p->top=p1=(Nt*)malloc(sizeof(Nt));
      p->top=p->base;
      p1->next=NULL;
  }
void push(stack *p,int n){
         Nt *p1;
          p1=(Nt*)malloc(sizeof(Nt));
          p1->date=n;
        p1->next=p->top;
          p->top=p1;
          i++;
}
Nt *pop(stack *p,Nt *e){  
 while(p->top!=p->base) {
      e=p->top;   
      p->top=p->top->next;
      return e;
  }
}
main( ){
  stack s;
  initstack(&s);
  int N;
  int b=1,m;
  Nt *e;
  printf("请输入一个整数\n");
  scanf("%d",&N);
  printf("请输入要转化成的进制数\n");
  scanf("%d",&m);
   do {  
         push(&s,N%m);
        N=N/m;        
        
   }
   while(N!=0);
   while(b<=i){
    e=pop(&s,e);
     printf("%d",*e);
    b++;
  }
}
1