设计一个算法,将一个十进制的数转化为二进制

#include<iostream.h>

const int StackSize=10;

class SeqStack

{

    public:

       SeqStack(){top=-1;}

       ~SeqStack(){}

       voidPush(int x);

       void Pop();

    private:

       intdata[StackSize];

        int top;

};

void SeqStack::Push(int x)

{

    top=-1;

    int k;

    int r=2;

    while(x!=0)

    {

       k=x%r;

       data[++top]=k;

       x=x/r;

    }

}

void SeqStack::Pop()

{  

    if (top == -1)throw "下溢";   

   while(top!=-1)   

    {   

    intx=data[top--];   

   cout<<x;   

    }   

}  

 

int main()

{     

    int i=1;   

    intnumber;   

    SeqStackn;       

   cout<<"请输入一个十进制整数!"<<endl;   

   cin>>number;   

   n.Push(number);   

    n.Pop();

   cout<<endl;

    return 0;

运行结果:


猜你喜欢

转载自blog.csdn.net/Air_JQM/article/details/80087482