用栈实现二进制转化

abaloon 2012-12-13 05:04:15
初学栈,自己写了个小程序,给出10进制数,根据用户输入,将其转换成2,8进制数。
但是出现了如下问题。
首先是push的前几行我给注销掉了,否则的话,总是输出stack is full.可是我明明在初始化的时候写的是
"s -> top = s -> base;",为什么还会出现这种情况呢?
第二点是,即使注销掉,当我向栈中插入最后一个数的时候仍然会抱错。编译没问题,是运行时出错。求大家帮忙参谋参谋哈~

#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 1000

typedef struct
{
int StackSize;
int * base;
int * top;
}Stack_Struct;

typedef Stack_Struct * Stack;

Stack InitStack()
{
Stack_Struct a;
Stack s = &a ;
s -> base = ( int * )malloc( STACK_INIT_SIZE * sizeof( int ) );
/*if( ! s -> base )
{
printf( "Allocation Failed!\n" );
exit( 1 );//??????????
}*/
s -> top = s -> base;
s -> StackSize = STACK_INIT_SIZE;
return s;
}

void ClrStack( Stack s )
{
while( s -> top != s -> base )
{
s -> top = NULL;
s -> top --;
}
printf( "Stack has been cleared!\n" );
}

void push( Stack s, int i )
{
/*if( (s -> top) - (s -> base) >= s -> StackSize )
{
printf( "Stack is full!\n" );
exit( 1 );
}*/
* (s -> top ) = i;
s -> top ++;
printf( "Push successfully!\n" );
}

int pop( Stack s )
{
int temp;
if( s -> top == s -> base )
{
printf( "Stack is empty!\n" );
return -1;
}

temp = * (s -> top );
return temp;
}

void DestroyStack( Stack s )
{
free( s );
printf( "Stack has been destroyed!\n" );
}



int main( void )
{
int num;
int conv;
int remainder;
Stack stack;
stack = InitStack();
printf( "Please input a number:\n" );
scanf( "%d", &num );
getchar();
printf( "Please input the conversion unit:\n" );
scanf( "%d", &conv );
getchar();

while( num )
{
remainder = num % conv;
push( stack, remainder );
num = num / conv;
}

while( pop( stack ) != -1 )
printf( "%d", pop( stack ) );

system( "pause" );
return 0;
}
...全文
160 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuh421 2012-12-13
  • 打赏
  • 举报
回复
Stack InitStack() { Stack_Struct a; //问题在这里, 你这个a是一个栈变量 , 这个函数结束 a就被释放了. 所以你这个函数返回的 指针本身就是一个不合法的地址, 访问肯定会出错的. 可以把Stack_Struct a ; 这句也改用malloc分配, 或者 new分配. Stack_Struct *a = (Stack_Struct*)malloc(sizeof(Stack_Struct)); Stack s = a;
newtee 2012-12-13
  • 打赏
  • 举报
回复
参考:
#include <stdio.h>
#include<stdlib.h>
#define MAXSIZE  20
typedef int ElemType;
typedef struct {
   ElemType  elem[MAXSIZE];
   int  top;
 }SqStack;
void InitStack( SqStack *S);
int  Empty( SqStack *S);
int  Push(SqStack *S, ElemType x);
int  Pop(SqStack *S );
 void fun(int n,int R);
void main()
{
  int n;
  char i;
  while(1)
  { printf("10进制转换为R(大小写都可以)进制\n"); 
    printf("b-------2\n");
    printf("o-------8\n");
    printf("x-------16\n");
    printf("e-------退出\n");
    printf("请输入10进制数n和转换的i进制:\n");
    scanf("%d,%c",&n,&i);
    switch(i)
    {  case'B':
       case'b': printf("转换为2进制后为:\n");
	            fun(n,2);
	            break;
       case'O':
       case'o': printf("转换为8进制后为:\n");
	           fun(n,8);
               break;
       case'X':
       case'x': printf("转换为16进制后为:\n");
		        printf("0x");
	            fun(n,16);
	            break; 
       case'E':
       case'e': printf("退出系统\n");
		    exit(0);
       default: printf("input error,input again");
     }
  }
}
void InitStack( SqStack *S)
{  S->top=0;
}
int  Empty( SqStack *S)
{return ( S->top==0);
}
int  Push(SqStack *S, ElemType x)
{  if( S->top==MAXSIZE )  return 0;
         S->elem[S->top]=x; S->top++;
         return 1;
}
int  Pop(SqStack *S )
{ --S->top;
  return (S->elem[S->top]);
}
void fun(int n,int R)
{  SqStack S;
   InitStack(&S);
   char m;
   int p;
   while(n!=0)
   { 
     Push(&S,n%R);
     n=n/R;
   }
   while(!Empty(&S))
   { p=Pop(&S);
	  if(p>=10)
	  { m=p%10+'A';
	    printf("%c",m);
	  }
      else  	  
	    printf("%d",p);
    }	
	  printf("\n");
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧