首页 > 代码库 > 金山——弱智的翻译程序

金山——弱智的翻译程序

感谢spencerzou的博文:http://blog.csdn.net/spencerzou/article/details/24255189

题目:有一个弱智的翻译程序,它接收一个文本文件作文输入源source.txt,通过查找给定的字典dictionary.txt,进行一一对应的翻译,并讲翻译结果输出到指定的文件out.txt。对于输入的内容,如果在字典中能找到对应的翻译,则输出翻译后的词条,否则原样输出。

字典存储为一个文本文件,每一行表示一个词条,源词条和翻译之间用逗号分隔,例如,如果有下面这个字典:

hello,你好

world,世界

当输入“hello world!”,翻译器输出“你好 世界!”,当输入“hello Blog!”,则·输出“你好 Blog!”。

1.实现这样子一个翻译程序,读入待翻译的文件和词典文件,输出翻译结果到out.txt文件中

2.要求字典的条目不限于单词(word),还可以是短语(phrase),

例如字典:

come,来

come out,出现

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\n", res);
 21:   res = mindrybuffer->Write(datachar, 40);
 22:   printf("write total:%d\n", res);
 23:   res = mindrybuffer->Write(datachar, 40);
 24:   printf("write total:%d\n", res);
 25:   res = mindrybuffer->Write(datachar, 40);
 26:   printf("write total:%d\n", res);
 27: 
 28:   res0 = mindrybuffer->Read(reschar, 20);
 29:   printf("read total:%d\n", res0);
 30: 
 31:   res = mindrybuffer->Write(datachar, 40);
 32:   printf("write total:%d\n", res);
 33:   res = mindrybuffer->Write(datachar, 40);
 34:   printf("write total:%d\n", res);
 35:   res = mindrybuffer->Write(datachar, 40);
 36:   printf("write total:%d\n", 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: }

 

存在问题:类的封装和方法划分不很合适,值得进一步改进。