程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 循環緩沖類

循環緩沖類

編輯:C++入門知識

4月27日去邁瑞面試,要求寫一個循環緩沖類,真心沒接觸過,結果可想而知。

下午回到實驗室,認真寫了一下,完成代碼貼出來。

 

  1: 
  2: 
  3: #ifndef MINDRY_BUFFER_H_H
  4: #define MINDRY_BUFFER_H_H
  5: 
  6:  CMindryBuffer
  7: {
  8: :
  9:    isFull();
 10:    isEmpty();
 11:    Empty();
 12:    GetLength();
 13: 
 14:   CMindryBuffer( size);
 15:    ~CMindryBuffer();
 16:     
 17:    Write( * buf,  count);
 18:    Read( * buf,  count);
 19: 
 20: :
 21:    m_bEmpty;
 22:    m_bFull;
 23: 
 24:    * m_pBuf;
 25:    m_nBufSize;
 26:    m_nReadPos;
 27:    m_nWritePos;
 28: 
 29: };
 30: 
 31: #endif

 

 

  1: 
  2: #include ""
  3: #include <cstring>
  4: 
  5: CMindryBuffer::CMindryBuffer( size)
  6: {
  7:   m_nBufSize = size;
  8:   m_nReadPos = 0;
  9:   m_nWritePos = 0;
 10:   m_pBuf =  [m_nBufSize];
 11:   m_bEmpty = ;
 12:   m_bFull = ;
 13: }
 14: 
 15: CMindryBuffer::~CMindryBuffer()
 16: {
 17:   [] m_pBuf;
 18: }
 19: 
 20:  CMindryBuffer::Write( *buf,  count)
 21: {
 22:   (count <= 0)  0;
 23:   
 24:   m_bEmpty = ;
 25: 
 26:   (m_bFull)
 27:   {
 28:      0;
 29:   }
 30:    (m_nReadPos == m_nWritePos) 
 31:   {
 32:   
 33:            empty      m_nReadPos        empty
 34:     |-------------------------|---------------------------| m_nBufSize
 35:                          m_nWritePos  
 36:     */
 37:      rightcount = m_nBufSize - m_nWritePos;
 38:     (rightcount > count)
 39:     {
 40:       memcpy(m_pBuf + m_nWritePos, buf, count);
 41:       m_nWritePos += count;
 42:       m_bFull = (m_nWritePos == m_nReadPos);
 43:        count;
 44:     }
 45:     
 46:     {
 47:       memcpy(m_pBuf + m_nWritePos, buf, rightcount);
 48:       m_nWritePos = (m_nReadPos > count - rightcount) ? \
 49:         count - rightcount : m_nWritePos;
 50:       memcpy(m_pBuf, buf + rightcount, m_nWritePos);
 51:       m_bFull = (m_nWritePos == m_nReadPos);
 52:        rightcount + m_nWritePos;
 53:     }
 54:   }
 55: 
 56:   
 57:            empty   m_nReadPos        m_nWritePos    empty
 58:     |-----------------|------------------|-------------------| m_nBufSize
 59:                             data               rightcount
 60:   */
 61:    (m_nReadPos < m_nWritePos)
 62:   {
 63:      rightcount = m_nBufSize - m_nWritePos;
 64:     (rightcount > count) 
 65:     {
 66:       memcpy(m_pBuf + m_nWritePos, buf, count);
 67:       m_nWritePos += count;
 68:       m_bFull = (m_nReadPos == m_nWritePos);
 69:        count;
 70:     }
 71:     
 72:     {
 73:       memcpy(m_pBuf + m_nWritePos, buf, rightcount);
 74:       m_nWritePos = (m_nReadPos > count - rightcount) ? \
 75:         count - rightcount : m_nReadPos;
 76:       memcpy(m_pBuf, buf + rightcount, m_nWritePos);
 77:       m_bFull = (m_nWritePos == m_nReadPos);
 78:        rightcount + m_nWritePos;
 79:     }
 80:   }
 81: }  
 82: 
 83:  CMindryBuffer::Read( *buf,  count)
 84: {
 85:   (count < 0)  0;
 86:   
 87:   m_bFull = ;
 88:   (m_bEmpty)
 89:   {
 90:      0;
 91:   }
 92:    (m_nReadPos == m_nWritePos) 
 93:   {
 94:     
 95:               data       m_nReadPos        data
 96:     |--------------------|--------------------|m_nBufSize
 97:                          m_nWritePos    rightcount
 98:     */
 99:      rightcount = m_nBufSize - m_nReadPos;
100:     (rightcount > count)
101:     {
102:       memcpy(buf, m_pBuf + m_nReadPos, count);
103:       m_nReadPos += count;
104:       m_bEmpty = (m_nReadPos == m_nWritePos);
105:        count;
106:     }
107:     
108:     {
109:       memcpy(buf, m_pBuf + m_nReadPos, rightcount);
110:       m_nReadPos = (m_nWritePos >= count - rightcount) ? \
111:         count - rightcount : m_nWritePos;
112:             memcpy(buf + rightcount, m_pBuf, m_nReadPos);
113:       m_bEmpty = (m_nReadPos == m_nWritePos);
114:        rightcount + m_nReadPos;
115:     }
116: 
117:   }
118:    (m_nReadPos < m_nWritePos)
119:   {
120:     
121:                     m_nReadPos  data  m_nWritePos  
122:     |-----------------|----------------|-----------------|m_nBufSize
123: 
124:     */
125:      rightcount = m_nWritePos - m_nReadPos;
126:     
127:      temp = (rightcount > count) ? count : rightcount;
128:       memcpy(buf, m_pBuf + m_nReadPos, temp);
129:     m_nReadPos += temp;
130:     m_bEmpty = (m_nWritePos == m_nReadPos);
131:      temp;
132:   }
133:    (m_nReadPos > m_nWritePos)
134:   {
135:     
136:                data     m_nWritePos       m_nReadPos    data        m_nBufSize
137:         |------------------|------------------|--------------------|
138: 
139:     */
140:      rightcount = m_nBufSize - m_nReadPos;
141:     
142:     (rightcount > count)
143:     {
144:       memcpy(buf, m_pBuf + m_nReadPos, count);
145:       m_nReadPos += count;
146:       m_bEmpty = (m_nWritePos == m_nReadPos);
147:        count;
148:     }
149:     
150:     {
151:       memcpy(buf, m_pBuf + m_nReadPos, rightcount);
152:       m_nReadPos = (m_nWritePos > count - rightcount) ? \
153:         count - rightcount : m_nWritePos;
154:       memcpy(buf, m_pBuf, m_nReadPos);
155:       m_bEmpty = (m_nWritePos == m_nReadPos);
156:          rightcount + m_nReadPos;
157:     }
158:   }
159: }
160: 
161: 
162:  CMindryBuffer::GetLength()
163: {
164:   (m_bEmpty)
165:   {
166:      0;
167:   }
168:   (m_bFull)
169:   {
170:      m_nBufSize;
171:   }
172:   (m_nReadPos < m_nWritePos)
173:   {
174:      m_nReadPos - m_nWritePos;
175:   }
176:     
177:   {
178:      m_nBufSize - (m_nReadPos - m_nWritePos);
179:   }
180: }
181: 
182: 
183:  CMindryBuffer::Empty()
184: {
185:   m_nReadPos = 0;
186:   m_nWritePos =0;
187:   m_bEmpty = ;
188:   m_bFull = ;
189: }
190: 
191:  CMindryBuffer::isEmpty()
192: {
193:    m_bEmpty;
194: }
195: 
196:  CMindryBuffer::isFull()
197: {
198:    m_bFull;
199: }

 

  1: #include ""
  2: #include <iostream>
  3:   std;
  4: 
  5:  main( argc,  * argv[])
  6: {
  7:   CMindryBuffer * mindrybuffer =  CMindryBuffer(100);
  8:    * datachar =  [40];
  9:    * reschar =  [20];
 10: 
 11:    res = 0;
 12:    res0 = 0;
 13: 
 14:   ( i=0; i < 40; ++i)
 15:   {
 16:     datachar[i] = i + 1;
 17:   }
 18: 
 19:   res = mindrybuffer->Write(datachar, 40);
 20:   printf("", res);
 21:   res = mindrybuffer->Write(datachar, 40);
 22:   printf("", res);
 23:   res = mindrybuffer->Write(datachar, 40);
 24:   printf("", res);
 25:   res = mindrybuffer->Write(datachar, 40);
 26:   printf("", res);
 27: 
 28:   res0 = mindrybuffer->Read(reschar, 20);
 29:   printf("", res0);
 30: 
 31:   res = mindrybuffer->Write(datachar, 40);
 32:   printf("", res);
 33:   res = mindrybuffer->Write(datachar, 40);
 34:   printf("", res);
 35:   res = mindrybuffer->Write(datachar, 40);
 36:   printf("", res);
 37: 
 38:   ( j=0; j < 20; ++j)
 39:   {
 40:     (j % 10 == 0) cout<<endl;
 41:     printf("",reschar[j]);
 42:   }
 43:    0;
 44: }

問題:面試的主管說我之前寫的代碼不合規范,請各位大牛指點一下。

(面試的時候沒有能夠實現方法,但是基本的框架結構都和這個是一樣)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved