正文

练习:用栈实现10进制到8进制的转换2006-05-13 11:59:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/bclz/13985.html

分享到:

//1、实现栈类
//2、实现10进制到8进制的转换
//2006 05 13
#include <iostream.h>
#include<string>

class Node{
public:
    int data;
    Node *next;
};
class Stack:public Node{
    Node *Bottom;
    Node *Top;
    int SLen;
public:
    void InitStack();
    int Pop();
    void Push(int e);
    bool StackEmpty();
};

void Stack::InitStack()
{
    Top=new Node;
    Bottom=Top;
    SLen=0;
}
int Stack::Pop()
{
    int e;
    Node *p;
    if(!(Top->next))
        exit(0);
    else
    {
        p=Top;
        e=p->data;
        Top=p->next;
        delete p;
        SLen--;
    }
    return e;
}
void Stack::Push(int e)
{
    Node* p=new Node;
    p->next=Top;
    p->data=e;
    Top=p;
    SLen++;
}
bool Stack::StackEmpty()
{
    if(SLen==0)
        return 1;
    else
        return 0;
}

void conversion(int N)
{
    Stack s;
    s.InitStack();
//    cin>>N;
    while(N)
    {
        s.Push(N%8);
        N=N/8;
    }
    while(!s.StackEmpty())
    {
        int e;
        e=s.Pop();
        cout<<e;
    }
    cout<<endl;
}//end conversion

void main()
{
    conversion(10);
}

阅读(2905) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册