把握命运,追逐梦想

对自己所做的事要有兴趣,同时还要能够坚持不懈

统计

留言簿(1)

阅读排行榜

评论排行榜

C++作业二----实现一个栈的基本功能

#include "Stack.h"

CStack::CStack(
void)
{
    
this->m_pTopMost = new char[100];
    
this->m_pBottom = &(this->m_pTopMost[99])+1;
    
this->m_pTop = this->m_pBottom;
    
}


CStack::CStack(CStack 
&stack)
{
    
this->m_pTopMost = new char[100];
    
this->m_pBottom = &(this->m_pTopMost[99])+1;
    
this->m_pTop = this->m_pBottom - (stack.m_pBottom - stack.m_pTop);
    memcpy(
this->m_pTop,stack.m_pTop,this->m_pBottom-this->m_pTop);
}


CStack::
~CStack(void)
{
    delete[] m_pTopMost;
}




void CStack::reset()
{
    
this->m_pBottom = &(this->m_pTopMost[99])+1;
    
this->m_pTop = m_pBottom;
}


void CStack::push(char c)
{
    
if(!this->full())
    
{
        
this->m_pTop--;
        
*(this->m_pTop) = c;        
    }

    
else
    
{
        cout
<<"栈已满"<<endl;
    }

    
}


void CStack::push(char c,int n)
{
    
for(int i = 0; i < n; i++)
    
{
        
this->push(c);
    }

}


char CStack::pop()
{
    
if(!this->empty())
    
{
        
char ret= *(this->m_pTop);
        
this->m_pTop++;
        
return ret;
    }

    
else
    
{
        cout
<<"栈为空"<<endl;
        
return '\0';
    }

}


string CStack::pop(int n)
{
    
string str="";
    
for(int i = 0; i < n; i++)
    
{
        str
+=this->pop();
    }

    
return str;
}


char CStack::top()   
{
    
return *(this->m_pTop);
}


bool CStack::empty()
{
    
if(this->m_pTop == this->m_pBottom)
        
return true;
    
else
        
return false;
}


bool CStack::full()
{
    
if(this->m_pTop == this->m_pTopMost)
        
return true;
    
else
        
return false;
}
#pragma once
#include
"stdafx.h"

class CStack
{
public:
    CStack(
void);
    CStack(CStack
& stack);
    
virtual ~CStack(void);

    
void reset();
    
void push(char c);
    
void push(char c,int n);  //连续压入n个字符
    char pop();
    
string pop(int n);         //连续弹出n个字符
    char top();                 //获得栈顶字符
    bool empty();
    
bool full();

private:
    
char *m_pBottom;
    
char *m_pTop;
    
char *m_pTopMost;

}
;
//stdafx.h
#pragma once
#include
<string>
#include
<iostream>
using namespace std;
//test.cpp
#include"stdafx.h"
#include
"Stack.h"

#ifdef   _DEBUG
#define  SET_CRT_DEBUG_FIELD(a) \
            _CrtSetDbgFlag((a) 
| _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#define  CLEAR_CRT_DEBUG_FIELD(a) \
            _CrtSetDbgFlag(
~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#else
#define  SET_CRT_DEBUG_FIELD(a)   ((void) 0)
#define  CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
#endif

int main()
{

    _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
    _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
    _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );

    

    CStack 
*pStack = new CStack();

    
char ch[20]="0123456789abcdefghi";

    
for(int i = 0; i < 19; i++)
    
{
        pStack
->push(ch[i]);
    }


    pStack
->push('Y',10);

    CStack stack2(
*pStack);

    
string str = pStack->pop(29);

    cout
<<str;


    _CrtMemDumpAllObjectsSince( NULL );
    



    
string str2 = stack2.pop(19);
    cout
<<str2;

    delete pStack;

    _CrtDumpMemoryLeaks( );

    SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF );

    
return 0;

}

posted on 2009-08-12 17:48 把握命运 阅读(244) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理