循环缓冲类

4月27日去迈瑞面试,要求写一个循环缓冲类,真心没接触过,结果可想而知。

下午回到实验室,认真写了一下,完成代码贴出来。

//mindry_buffer.h

#ifndef MINDRY_BUFFER_H_H
#define MINDRY_BUFFER_H_H

class CMindryBuffer
{
public:
	bool isFull();//缓冲区是否满
	bool isEmpty();//缓冲区是否空
	void Empty();//置空缓冲区
	int GetLength();//缓冲区大小

	CMindryBuffer(int size);
	virtual ~CMindryBuffer();
    
	int Write(char * buf, int count);//写缓冲
	int Read(char * buf, int count);//读缓冲

private:
	bool m_bEmpty;//缓冲区空标识
	bool m_bFull;//缓冲区满标识

	char * m_pBuf;//缓冲区指针
	int m_nBufSize;//缓冲区大小
	int m_nReadPos;//读数据位置
	int m_nWritePos;//写数据位置

};

#endif
#include "mindry_buffer.h"
#include 
   
   
    
    

CMindryBuffer::CMindryBuffer(int size)
{
	m_nBufSize = size;
	m_nReadPos = 0;
	m_nWritePos = 0;
	m_pBuf = new char[m_nBufSize];
	m_bEmpty = true;
	m_bFull = false;
}

CMindryBuffer::~CMindryBuffer()
{
	delete[] m_pBuf;
}

int CMindryBuffer::Write(char *buf, int count)
{
	if(count <= 0) return 0;
	
	m_bEmpty = false;//buffer is full

	if(m_bFull)
	{
		return 0;
	}
	else if(m_nReadPos == m_nWritePos) //buffer is empty
	{
	/*
		       empty      m_nReadPos        empty
    |-------------------------|---------------------------| m_nBufSize
                         m_nWritePos  
    */
		int rightcount = m_nBufSize - m_nWritePos;
		if(rightcount > count)//space is enough
		{
			memcpy(m_pBuf + m_nWritePos, buf, count);
			m_nWritePos += count;
			m_bFull = (m_nWritePos == m_nReadPos);
			return count;
		}
		else
		{
			memcpy(m_pBuf + m_nWritePos, buf, rightcount);
			m_nWritePos = (m_nReadPos > count - rightcount) ? \
				count - rightcount : m_nWritePos;//is left space enough?yes,copy;no, no action;
			memcpy(m_pBuf, buf + rightcount, m_nWritePos);
			m_bFull = (m_nWritePos == m_nReadPos);
			return rightcount + m_nWritePos;
		}
	}

	/*
           empty   m_nReadPos        m_nWritePos    empty
    |-----------------|------------------|-------------------| m_nBufSize
                            data               rightcount
	*/
	else if(m_nReadPos < m_nWritePos)//buffer is not full
	{
		int rightcount = m_nBufSize - m_nWritePos;
		if(rightcount > count) //rest space is enough
		{
			memcpy(m_pBuf + m_nWritePos, buf, count);
			m_nWritePos += count;
			m_bFull = (m_nReadPos == m_nWritePos);
			return count;
		}
		else//rest space is not enough
		{
			memcpy(m_pBuf + m_nWritePos, buf, rightcount);
			m_nWritePos = (m_nReadPos > count - rightcount) ? \
				count - rightcount : m_nReadPos;
			memcpy(m_pBuf, buf + rightcount, m_nWritePos);
			m_bFull = (m_nWritePos == m_nReadPos);
			return rightcount + m_nWritePos;
		}
	}
}	

int CMindryBuffer::Read(char *buf, int count)
{
	if(count < 0) return 0;
	
	m_bFull = false;
	if(m_bEmpty)
	{
		return 0;
	}
	else if(m_nReadPos == m_nWritePos) //buffer is full
	{
		/*
              data       m_nReadPos        data
		|--------------------|--------------------|m_nBufSize
                         m_nWritePos    rightcount
		*/
		int rightcount = m_nBufSize - m_nReadPos;
		if(rightcount > count)
		{
			memcpy(buf, m_pBuf + m_nReadPos, count);
			m_nReadPos += count;
			m_bEmpty = (m_nReadPos == m_nWritePos);
			return count;
		}
		else
		{
			memcpy(buf, m_pBuf + m_nReadPos, rightcount);
			m_nReadPos = (m_nWritePos >= count - rightcount) ? \
				count - rightcount : m_nWritePos;
            memcpy(buf + rightcount, m_pBuf, m_nReadPos);
			m_bEmpty = (m_nReadPos == m_nWritePos);
			return rightcount + m_nReadPos;
		}

	}
	else if(m_nReadPos < m_nWritePos)
	{
		/*
                    m_nReadPos  data  m_nWritePos  
		|-----------------|----------------|-----------------|m_nBufSize

		*/
		int rightcount = m_nWritePos - m_nReadPos;
		
		int temp = (rightcount > count) ? count : rightcount;
	    memcpy(buf, m_pBuf + m_nReadPos, temp);
		m_nReadPos += temp;
		m_bEmpty = (m_nWritePos == m_nReadPos);
		return temp;
	}
	else if(m_nReadPos > m_nWritePos)
	{
		/*
               data     m_nWritePos       m_nReadPos    data        m_nBufSize
        |------------------|------------------|--------------------|

		*/
		int rightcount = m_nBufSize - m_nReadPos;
		
		if(rightcount > count)
		{
			memcpy(buf, m_pBuf + m_nReadPos, count);
			m_nReadPos += count;
			m_bEmpty = (m_nWritePos == m_nReadPos);
			return count;
		}
		else
		{
			memcpy(buf, m_pBuf + m_nReadPos, rightcount);
			m_nReadPos = (m_nWritePos > count - rightcount) ? \
				count - rightcount : m_nWritePos;
			memcpy(buf, m_pBuf, m_nReadPos);
			m_bEmpty = (m_nWritePos == m_nReadPos);
		    return rightcount + m_nReadPos;
		}
	}
}


int CMindryBuffer::GetLength()
{
	if(m_bEmpty)
	{
		return 0;
	}
	if(m_bFull)
	{
		return m_nBufSize;
	}
	if(m_nReadPos < m_nWritePos)
	{
		return m_nReadPos - m_nWritePos;
	}
	else  
	{
		return m_nBufSize - (m_nReadPos - m_nWritePos);
	}
}


void CMindryBuffer::Empty()
{
	m_nReadPos = 0;
	m_nWritePos =0;
	m_bEmpty = true;
	m_bFull = false;
}

bool CMindryBuffer::isEmpty()
{
	return m_bEmpty;
}

bool CMindryBuffer::isFull()
{
	return m_bFull;
}
#include "mindry_buffer.h"
#include 
    
    
     
     
using namespace std;

int main(int argc, char * argv[])
{
	CMindryBuffer * mindrybuffer = new CMindryBuffer(100);
	char * datachar = new char[40];
	char * reschar = new char[20];

	int res = 0;
	int res0 = 0;

	for(int i=0; i < 40; ++i)
	{
		datachar[i] = i + 1;
	}

	res = mindrybuffer->Write(datachar, 40);
	printf("write total:%d\n", res);
	res = mindrybuffer->Write(datachar, 40);
	printf("write total:%d\n", res);
	res = mindrybuffer->Write(datachar, 40);
	printf("write total:%d\n", res);
	res = mindrybuffer->Write(datachar, 40);
	printf("write total:%d\n", res);

	res0 = mindrybuffer->Read(reschar, 20);
	printf("read total:%d\n", res0);

	res = mindrybuffer->Write(datachar, 40);
	printf("write total:%d\n", res);
	res = mindrybuffer->Write(datachar, 40);
	printf("write total:%d\n", res);
	res = mindrybuffer->Write(datachar, 40);
	printf("write total:%d\n", res);

	for(int j=0; j < 20; ++j)
	{
		if(j % 10 == 0) cout<
     
      

问题:面试的主管说我之前写的代码不合规范,请各位大牛指点一下。

(面试的时候没有能够实现方法,但是基本的框架结构都和这个是一样)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值