第十六周 任务三

﹏ヽ暗。殇╰゛Y 2022-06-11 02:59 269阅读 0赞
  1. /*
  2. * 程序头部注释开始
  3. * 程序的版权和版本声明部分
  4. * Copyright (c) 2011, 烟台大学计算机学院学生
  5. * All rights reserved.
  6. * 文件名称:电子词典
  7. * 作 者:薛广晨
  8. * 完成日期:2011 年 06 月 03 日
  9. * 版 本号:x1.0
  10. * 对任务及求解方法的描述部分
  11. * 输入描述:文件dictionary.txt
  12. * 问题描述:做一个简单的电子词典。在文件dictionary.txt 中,保存的是英汉对照的一个词典,词汇量近8000 个,
  13. 英文、中文释义与词性间用’\t’隔开。建一个表示词条的类Word,Word 类的一个对象可以描述一个词,
  14. 类对象数组可以存储词库。将文件中的内容读到对象数组中,由用户输入英文词,显示中文释义。
  15. 允许用户运行程序后,连续地查词典,直到输入”0000”结束
  16. * 程序头部的注释结束
  17. */
  18. #include <fstream>
  19. #include <string>
  20. #include <iostream>
  21. using namespace std;
  22. class Word
  23. {
  24. private:
  25. string english;
  26. string chinese;
  27. string cx;
  28. public:
  29. Word();
  30. Word(string english, string chinese, string cx);
  31. string get_English();
  32. string get_Chinese();
  33. string get_Cx();
  34. void set_English(string english);
  35. void set_Chinese(string chinese);
  36. void set_Cx(string cx);
  37. friend void ouput(Word w[]);
  38. friend void seek(Word w[]);
  39. };
  40. int main()
  41. {
  42. Word word[8000];
  43. ouput(word);
  44. seek(word);
  45. return 0;
  46. }
  47. Word::Word()
  48. {
  49. this->english = "0000";
  50. this->chinese = "0000";
  51. this->cx = "0000";
  52. }
  53. Word::Word(string english, string chinese, string cx)
  54. {
  55. this->english = english;
  56. this->chinese = chinese;
  57. this->cx = cx;
  58. }
  59. string Word::get_English()
  60. {
  61. return english;
  62. }
  63. string Word::get_Chinese()
  64. {
  65. return chinese;
  66. }
  67. string Word::get_Cx()
  68. {
  69. return cx;
  70. }
  71. void Word::set_English(string english)
  72. {
  73. this->english = english;
  74. }
  75. void Word::set_Chinese(string chinese)
  76. {
  77. this->chinese = chinese;
  78. }
  79. void Word::set_Cx(string cx)
  80. {
  81. this->cx = cx;
  82. }
  83. void ouput(Word w[])
  84. {
  85. int i;
  86. string English;
  87. string Chinese;
  88. string Cx;
  89. ifstream infile("dictionary.txt",ios::in);
  90. if(!infile)
  91. {
  92. cerr<<"open error!"<<endl;
  93. exit(1);
  94. }
  95. for(i=0;i<8000;i++)
  96. {
  97. infile>>English>>Chinese>>Cx;
  98. w[i].set_English(English);
  99. w[i].set_Chinese(Chinese);
  100. w[i].set_Cx(Cx);
  101. }
  102. infile.close();
  103. }
  104. void seek(Word w[])
  105. {
  106. string cha;
  107. int low, high, mid;
  108. do
  109. {
  110. cout<<"请输入要查的词(0000结束):";
  111. cin>>cha;
  112. if(cha=="0000")break;
  113. low=0,high=8000-1;
  114. while(low<=high)
  115. {
  116. mid=(low+high)/2;
  117. if(w[mid].get_English()==cha)
  118. {
  119. cout<<"“"<<w[mid].get_English()<<"”"<<"中文意思是 : "<<w[mid].get_Chinese() << " " << "词性是:" <<w[mid].get_Cx()<<endl<<endl;
  120. break;
  121. }
  122. else
  123. {
  124. if(w[mid].get_English()>cha) high=mid-1;
  125. else low=mid+1;
  126. }
  127. if(low>high) cout<<"查无此词"<<endl<<endl;
  128. }
  129. }while(1);
  130. }

1338656342_5589.jpg

上机感言:有这代码查词就容易了,很好用哦,用自己的代码查词就是比用电子词典感觉爽啊。

发表评论

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

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

相关阅读

    相关 任务

    /实验目的:设计平面坐标点类 实验内容:设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等 程序的版权和版本声明部分