金山——弱智的翻译程序

感谢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: }

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

时间: 2024-10-30 05:19:13

金山——弱智的翻译程序的相关文章

金山笔试总结

金山公司的笔试题目,过后把自己在考场写的代码测试一下,发现存在不少问题,仔细地总结了一番,如下: 一.检查一个值是否为NaN 1 <html> 2 <head> 3 <script language="JavaScript"> 4 function isNumber(){ 5 var btnObj=document.getElementById("idTel"); 6 var strValue=btnObj.value; 7 8

【141030】金山卫士开源代码,VC++完整源码

VC++金山卫士开源代码,包含所有模块的源码,促进互联网行业的开源计划 :也是你学习VC++的绝佳范例,可以接触到中国最专业的安全类软件源代码,你可以自由的使用/研究/修订/再发布 这些代码以及延伸作品.VC++金山卫士开源代码下载. 游戏源码下载地址:点击下载

黑马程序员金山卫士实战短信备份2集

黑马程序员 大师班 安卓项目实战-分分钟搞定 金山卫士实战短信备份2集 有很多用户们在使用智能手机的过程中有及时清理手机,删除电话记录以及短信的习惯,以此来保证手机的纯净,但是有时我们会有一些重要的信息想要保存下来,这时该怎么办呢?备份就是一个很好的办法... 课程包含 短信备份.代码优化 下载地址:http://www.feixueteam.net/thread-947-1-1.html

微软Office与金山WPS Office有何私密关系?

新浪科技讯,9月1日午间消息,国家工商总局在其官网公布消息称.对微软公司副总裁陈实进行反垄断调查询问,要求微软就其综合情况以及企业反映的微软公司Windows操作系统和Office办公软件相关信息没有全然公开造成的兼容性问题等相关问题,在20日内做出书面说明. 经过查证.陈实1981年毕业于上海华东师范大学数学系.获得理科学士学位.他还获得美国普渡大学数学与电子project硕士学位.密歇根大学电子project与计算机博士学位以及斯坦福大学(斯隆)管理学院管理学硕士学位. 国家工商总局对微软副

Ubuntu安装搜狗拼音和金山快盘

搜狗拼音和金山快盘是UbuntuKylin中的特色中文应用,通过下述方法在Ubuntu或UbuntuKylin中安装. 一.更新Fcitx 1.由于 Ubuntu 12.04自带的 小企鹅输入法Fcitx 版本较旧,安装搜狗拼音需要更新Fcitx,需要先通过 PPA 升级,才能安装下载的 deb 软件包$sudo add-apt-repository ppa:fcitx-team/nightly$sudo apt-get update$sudo apt-get install fcitx 注意:

【云快讯】之五十一《惠普借道金山云正式进入中国》

2015-07-21 张晓东 东方云洞察 点击上面的链接文字,可以快速关注"东方云洞察"公众号 [个人观点] 在双方合作的备忘录中显示,金山云将提供底层基础架构资源支持,整合惠普的应用变革.开发和管理经验.这种模式看起更像是双方达成战略联盟,共享客户资源,在各自的优势行业取长补短,共同拓展市场. 因此看来,金山和惠普的合作重点似乎并不是HP公有云入华,和IBM.微软于世纪互联的合作有些不同,惠普的云核心战略就是其Helion品牌下的云产品和方案推广战略.对于公有云惠普明显不像微软.IB

人人,金山西山居,腾讯互娱,微信,网易游戏offer及面经

转自:http://www.itmian4.com/forum.php?mod=viewthread&tid=3985 首先感谢师兄在两年前发的贴([天道酬勤] 腾讯.百度.网易游戏.华为Offer及笔经面经 ),这篇文章对我帮助很大. 我写这篇文章一是为了感谢这位同是华南理工但素未谋面的师兄,给我推荐这篇文章的炳爷,以及为我解决难题的浩子,羊兄给的项目帮助,洁洁的理解,王兄提供的两个月免费住宿,以及互娱的BOSS,leader,金山的朱先生等人对我择业的帮助,以及所有直接间接帮助过我的人.二是

利用金山快盘和TortoiseSVN架设个人svn服务器

作为程序员,可能经常遇到这样的问题,想自己有一个公网的svn服务器,像公司的svn一样,存储自己的东西,还可以追溯版本,可是我们又不想花费购买公网服务器和ip,也不想自己架设服务器,映射公网(因为这样费电啊,也要花钱),那怎么办呢?很久之前看到一个类似的文章,当时看了后,觉得可行,但一直没有做,今天有时间,正好也想整理一下电脑上的文件了,同时又不想删除东西,就找一个存储的地方,然后就想到组一个这样的私人SVN了. 说下步骤吧: 1.下载金山快盘,next,安装 2.下载TortoiseSVN-1

金山 东林寺祈福

最近工作生活都不是很顺利,很多琐事云绕,问了一之前的同事说可以去放放生拜拜佛,起初不是相信后决心虔诚去礼佛算是给自己一个期许与激励吧! 到了金山之后,打电话朋友(村长),开车一并到了东林寺,在路上聊了下最近之前同事的情况,李明从翠竹搬到了绿地8楼前些天才开业,胖子在绿地六楼月初开业的,良良带的团队做了70多W,村长自己带的都是新人也还好有10来W,还有郭鹏兄弟公司.村长说又想自己开公司的想法.看到大家都做有梦,心中有种很难说的滋味.觉得今天来东林寺拜拜是对的,理一理最近的心境,开一开最近的事业运