金山——弱智の翻訳プログラム

20882 ワード

  spencerzou   :http://blog.csdn.net/spencerzou/article/details/24255189
タイトル:知的障害のある翻訳プログラムがあります.テキストファイルの入力元source.txtを受信して、所与の辞書dictionary.txtを検索して、対応する翻訳を行い、翻訳結果を指定のファイルout.txtに出力します.入力された内容に対して、辞書で対応する翻訳が見つけられたら、翻訳された語句を出力します.そうでなければそのまま出力します.
辞書はテキストファイルとして保存されています.各行は単語を表しています.ソースと翻訳の間はコンマで区切られています.たとえば、次の辞書があれば、
ハロー、こんにちは
ワールド
「ハローワールド!」を入力すると、翻訳機が「こんにちは世界!」を出力し、「ハローブログ!」を入力すると「こんにちはブログ!」を出力します.
1.このような翻訳プログラムを実現し、翻訳するファイルと辞書ファイルを読み込み、翻訳結果をout.txtファイルに出力します.
2.辞書を要求する項目は単語に限らず、またフレーズであってもよい.
たとえば辞書:
come、来ます
come out、出現
アウト
入力元のドキュメントに「come out」がある場合は、「出ます」ではなく「出ます」に翻訳します.
完了したクラスコードは以下の通りです.
 
  1: #include "mindry_buffer.h"

  2: #include <iostream>

  3: using namespace std;

  4: 

  5: int main(int argc, char * argv[])

  6: {

  7:   CMindryBuffer * mindrybuffer = new CMindryBuffer(100);

  8:   char * datachar = new char[40];

  9:   char * reschar = new char[20];

 10: 

 11:   int res = 0;

 12:   int res0 = 0;

 13: 

 14:   for(int i=0; i < 40; ++i)

 15:   {

 16:     datachar[i] = i + 1;

 17:   }

 18: 

 19:   res = mindrybuffer->Write(datachar, 40);

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

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

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

 26:   printf("write total:%d
", res);
 27: 

 28:   res0 = mindrybuffer->Read(reschar, 20);

 29:   printf("read total:%d
", res0);
 30: 

 31:   res = mindrybuffer->Write(datachar, 40);

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

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

 36:   printf("write total:%d
", res);
 37: 

 38:   for(int j=0; j < 20; ++j)

 39:   {

 40:     if(j % 10 == 0) cout<<endl;

 41:     printf("%d ",reschar[j]);

 42:   }

 43:   return 0;

 44: }
 
  1: //mindry_buffer.h

  2: 

  3: #ifndef MINDRY_BUFFER_H_H

  4: #define MINDRY_BUFFER_H_H

  5: 

  6: class CMindryBuffer

  7: {

  8: public:

  9:   bool isFull();//      

 10:   bool isEmpty();//      

 11:   void Empty();//     

 12:   int GetLength();//     

 13: 

 14:   CMindryBuffer(int size);

 15:   virtual ~CMindryBuffer();

 16:     

 17:   int Write(char * buf, int count);//   

 18:   int Read(char * buf, int count);//   

 19: 

 20: private:

 21:   bool m_bEmpty;//      

 22:   bool m_bFull;//      

 23: 

 24:   char * m_pBuf;//     

 25:   int m_nBufSize;//     

 26:   int m_nReadPos;//     

 27:   int m_nWritePos;//     

 28: 

 29: };

 30: 

 31: #endif
 
  1: #include "mindry_buffer.h"

  2: #include <cstring>

  3: 

  4: CMindryBuffer::CMindryBuffer(int size)

  5: {

  6:   m_nBufSize = size;

  7:   m_nReadPos = 0;

  8:   m_nWritePos = 0;

  9:   m_pBuf = new char[m_nBufSize];

 10:   m_bEmpty = true;

 11:   m_bFull = false;

 12: }

 13: 

 14: CMindryBuffer::~CMindryBuffer()

 15: {

 16:   delete[] m_pBuf;

 17: }

 18: 

 19: int CMindryBuffer::Write(char *buf, int count)

 20: {

 21:   if(count <= 0) return 0;

 22:   

 23:   m_bEmpty = false;//buffer is full

 24: 

 25:   if(m_bFull)

 26:   {

 27:     return 0;

 28:   }

 29:   else if(m_nReadPos == m_nWritePos) //buffer is empty

 30:   {

 31:   /*

 32:            empty      m_nReadPos        empty

 33:     |-------------------------|---------------------------| m_nBufSize

 34:                          m_nWritePos  

 35:     */

 36:     int rightcount = m_nBufSize - m_nWritePos;

 37:     if(rightcount > count)//space is enough

 38:     {

 39:       memcpy(m_pBuf + m_nWritePos, buf, count);

 40:       m_nWritePos += count;

 41:       m_bFull = (m_nWritePos == m_nReadPos);

 42:       return count;

 43:     }

 44:     else

 45:     {

 46:       memcpy(m_pBuf + m_nWritePos, buf, rightcount);

 47:       m_nWritePos = (m_nReadPos > count - rightcount) ? \

 48:         count - rightcount : m_nWritePos;//is left space enough?yes,copy;no, no action;

 49:       memcpy(m_pBuf, buf + rightcount, m_nWritePos);

 50:       m_bFull = (m_nWritePos == m_nReadPos);

 51:       return rightcount + m_nWritePos;

 52:     }

 53:   }

 54: 

 55:   /*

 56:            empty   m_nReadPos        m_nWritePos    empty

 57:     |-----------------|------------------|-------------------| m_nBufSize

 58:                             data               rightcount

 59:   */

 60:   else if(m_nReadPos < m_nWritePos)//buffer is not full

 61:   {

 62:     int rightcount = m_nBufSize - m_nWritePos;

 63:     if(rightcount > count) //rest space is enough

 64:     {

 65:       memcpy(m_pBuf + m_nWritePos, buf, count);

 66:       m_nWritePos += count;

 67:       m_bFull = (m_nReadPos == m_nWritePos);

 68:       return count;

 69:     }

 70:     else//rest space is not enough

 71:     {

 72:       memcpy(m_pBuf + m_nWritePos, buf, rightcount);

 73:       m_nWritePos = (m_nReadPos > count - rightcount) ? \

 74:         count - rightcount : m_nReadPos;

 75:       memcpy(m_pBuf, buf + rightcount, m_nWritePos);

 76:       m_bFull = (m_nWritePos == m_nReadPos);

 77:       return rightcount + m_nWritePos;

 78:     }

 79:   }

 80: }  

 81: 

 82: int CMindryBuffer::Read(char *buf, int count)

 83: {

 84:   if(count < 0) return 0;

 85:   

 86:   m_bFull = false;

 87:   if(m_bEmpty)

 88:   {

 89:     return 0;

 90:   }

 91:   else if(m_nReadPos == m_nWritePos) //buffer is full

 92:   {

 93:     /*

 94:               data       m_nReadPos        data

 95:     |--------------------|--------------------|m_nBufSize

 96:                          m_nWritePos    rightcount

 97:     */

 98:     int rightcount = m_nBufSize - m_nReadPos;

 99:     if(rightcount > count)

100:     {

101:       memcpy(buf, m_pBuf + m_nReadPos, count);

102:       m_nReadPos += count;

103:       m_bEmpty = (m_nReadPos == m_nWritePos);

104:       return count;

105:     }

106:     else

107:     {

108:       memcpy(buf, m_pBuf + m_nReadPos, rightcount);

109:       m_nReadPos = (m_nWritePos >= count - rightcount) ? \

110:         count - rightcount : m_nWritePos;

111:             memcpy(buf + rightcount, m_pBuf, m_nReadPos);

112:       m_bEmpty = (m_nReadPos == m_nWritePos);

113:       return rightcount + m_nReadPos;

114:     }

115: 

116:   }

117:   else if(m_nReadPos < m_nWritePos)

118:   {

119:     /*

120:                     m_nReadPos  data  m_nWritePos  

121:     |-----------------|----------------|-----------------|m_nBufSize

122: 

123:     */

124:     int rightcount = m_nWritePos - m_nReadPos;

125:     

126:     int temp = (rightcount > count) ? count : rightcount;

127:       memcpy(buf, m_pBuf + m_nReadPos, temp);

128:     m_nReadPos += temp;

129:     m_bEmpty = (m_nWritePos == m_nReadPos);

130:     return temp;

131:   }

132:   else if(m_nReadPos > m_nWritePos)

133:   {

134:     /*

135:                data     m_nWritePos       m_nReadPos    data        m_nBufSize

136:         |------------------|------------------|--------------------|

137: 

138:     */

139:     int rightcount = m_nBufSize - m_nReadPos;

140:     

141:     if(rightcount > count)

142:     {

143:       memcpy(buf, m_pBuf + m_nReadPos, count);

144:       m_nReadPos += count;

145:       m_bEmpty = (m_nWritePos == m_nReadPos);

146:       return count;

147:     }

148:     else

149:     {

150:       memcpy(buf, m_pBuf + m_nReadPos, rightcount);

151:       m_nReadPos = (m_nWritePos > count - rightcount) ? \

152:         count - rightcount : m_nWritePos;

153:       memcpy(buf, m_pBuf, m_nReadPos);

154:       m_bEmpty = (m_nWritePos == m_nReadPos);

155:         return rightcount + m_nReadPos;

156:     }

157:   }

158: }

159: 

160: 

161: int CMindryBuffer::GetLength()

162: {

163:   if(m_bEmpty)

164:   {

165:     return 0;

166:   }

167:   if(m_bFull)

168:   {

169:     return m_nBufSize;

170:   }

171:   if(m_nReadPos < m_nWritePos)

172:   {

173:     return m_nReadPos - m_nWritePos;

174:   }

175:   else  

176:   {

177:     return m_nBufSize - (m_nReadPos - m_nWritePos);

178:   }

179: }

180: 

181: 

182: void CMindryBuffer::Empty()

183: {

184:   m_nReadPos = 0;

185:   m_nWritePos =0;

186:   m_bEmpty = true;

187:   m_bFull = false;

188: }

189: 

190: bool CMindryBuffer::isEmpty()

191: {

192:   return m_bEmpty;

193: }

194: 

195: bool CMindryBuffer::isFull()

196: {

197:   return m_bFull;

198: }
 
問題があります.クラスのパッケージと方法の区分は適切ではなく、さらに改善する価値があります.