第十四周【项目3-OOP版电子词典】

缺乏、安全感 2022-08-05 07:37 187阅读 0赞

问题及代码:

  1. /* 【项目3-OOP版电子词典】
  2. *Copyright (c) 2014,烟台大学计算机学院
  3. *ALL right reserved
  4. *文件名: 版电子词典
  5. *作者;童宇
  6. *完成日期:2015 年 6月 11日
  7. *版本号v1.0
  8. *问题描述:
  9. 编程序,由用户输入英文词,显示词性和中文释义。
  10. *输入描述:
  11. *程序输出:
  12. */
  13. #include <fstream>
  14. #include<iostream>
  15. #include<string>
  16. #include<cstdlib>
  17. using namespace std;
  18. //定义词条类
  19. class Word
  20. {
  21. public:
  22. void set(string e, string c, string wc);
  23. int compare(string); //英语部分与给定字符串比较,等于返回,大于返回,小于返回-1
  24. string getChinese();
  25. string getWord_class();
  26. private:
  27. string english;
  28. string chinese;
  29. string word_class;
  30. };
  31. void Word::set(string e, string c, string wc)
  32. {
  33. english=e;
  34. chinese=c;
  35. word_class=wc;
  36. }
  37. int Word::compare(string k)
  38. {
  39. return english.compare(k);
  40. }
  41. string Word::getChinese()
  42. {
  43. return chinese;
  44. }
  45. string Word::getWord_class()
  46. {
  47. return word_class;
  48. }
  49. class Dictionary
  50. {
  51. public:
  52. Dictionary();
  53. void searchWord(string k);
  54. private:
  55. int BinSeareh(int low, int high, string k);
  56. int wordsNum;
  57. Word words[8000]; //用于保存词库
  58. };
  59. Dictionary::Dictionary()
  60. {
  61. string e,c,wc;
  62. wordsNum=0;
  63. ifstream infile("dictionary.txt",ios::in);
  64. if(!infile)
  65. {
  66. cerr<<"dictionary open error!"<<endl;
  67. exit(1);
  68. }
  69. while (!infile.eof())
  70. {
  71. infile>>e>>c>>wc;
  72. words[wordsNum].set(e,c,wc);
  73. ++wordsNum;
  74. }
  75. infile.close();
  76. }
  77. void Dictionary::searchWord(string k)
  78. {
  79. int low=0,high=wordsNum-1;
  80. int index=BinSeareh(low,high,k);
  81. if(index>=0)
  82. cout<<k<<"--->"<<words[index].getWord_class()+"\t"<<words[index].getChinese();
  83. else
  84. cout<<"查无此词";
  85. cout<<endl<<endl;
  86. }
  87. int Dictionary::BinSeareh(int low, int high, string k)
  88. {
  89. int m;
  90. while (low<=high)
  91. {
  92. m=(low+high)/2;
  93. if(words[m].compare(k)==0)
  94. {
  95. return m;
  96. }
  97. if(words[m].compare(k)>0)
  98. {
  99. high=m-1;
  100. }
  101. else
  102. low=m+1;
  103. }
  104. return -1;
  105. }
  106. int main( )
  107. {
  108. Dictionary dict;
  109. string key;
  110. do
  111. {
  112. cout<<"请输入待查询的关键词(英文),0000结束:"<<endl;
  113. cin>>key;
  114. if (key!="0000")
  115. {
  116. dict.searchWord(key);
  117. }
  118. }
  119. while(key!="0000");
  120. cout<<"欢迎再次使用!"<<endl<<endl;
  121. return 0;
  122. }

运行结果:

Center

发表评论

表情:
评论列表 (有 0 条评论,187人围观)

还没有评论,来说两句吧...

相关阅读