结构推理 将一个非负十进制整数转换成八进制数,使用非递归算法实现。
   算法分析:十进制转换成八进制的过程是将十进制整数除8得余数,直到商是0为止,然后倒排余数。为了得到倒排的余数,可以利用栈来实现,每次运算后将余数压入栈中,直到商为0,将栈中数据输出即是。使用顺序栈,将顺序栈的定义及其基本操作的实现写在头文件“seqstack.h”中。
【正确答案】算法实现如下:
   #include"stdio.h"
   #include"seqstack.h"
   void d_to_o(unsigned x)
   {  SEQSTACK stack,*s;
       s=&stack;
       Init_Stack(s);
       while(x!=0)
       {    push_Stack(s,x/%8);
           x=x/8;
       }
       while(!Stack_Empty(s))
       {printf("/%4d",Gettop_Stack(s));
           pop_Stack(s);
       }
   }
   main()
   {  unsigned x=150;
       d_to_o(x);
   }
【答案解析】