第十五周项目3(拓展)-读入一篇英语文章,输出每个词的意思

/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 6 月 3 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: 文件导入单词
*问题描述:输出文件导入单词对应的意思
*程序输出:单词意思
*问题分析:
*算法设计:
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
class Word
{
public:
    Word() {}
    string getEnglish();
    string getChinese();
    string getWord_Class();
    friend istream & operator >>(istream &in,Word &w);
    friend ostream & operator <<(ostream &out,Word &w);
private:
    string english;
    string chinese;
    string word_class;
};
class Dictionary
{
public:
    Dictionary();//导入所有单词组成词典
    string search(string keyword);
private:
    Word words[8000];
    int wordsum;
};
Dictionary::Dictionary()
{
    wordsum=0;
    ifstream infile ("D:\\dictionary.txt",ios::in);
    if(!infile)
    {
        cerr<<"Open Error!"<<endl;
        exit(1);
    }
    while(!infile.eof())
        infile>>words[wordsum++];
    infile.close();
}
string Dictionary::search(string keyword)
{
    int head=0,end=wordsum,middle=(head+end)/2;
    while(head<end&&words[middle].getEnglish()!=keyword)
    {
        if(words[middle].getEnglish()<keyword)
            head=middle+1;
        if(words[middle].getEnglish()>keyword)
            end=middle-1;
        middle=(head+end)/2;
    }
    if(words[middle].getEnglish()==keyword)
        return words[middle].getChinese()+words[middle].getWord_Class();
    else
        return "查无此词";
}
string Word::getEnglish()
{
    return english;
}
string Word::getChinese()
{
    return chinese;
}
string Word::getWord_Class()
{
    return word_class;
}
istream & operator >>(istream &in,Word &w)
{
    in>>w.english>>w.chinese>>w.word_class;
    return in;
}
ostream & operator <<(ostream &out,Word &w)
{
    out<<w.getChinese()<<"\t"<<w.getWord_Class()<<endl;
    return out;
}
int main()
{
    Dictionary dic;
    string english;
    ifstream inFile ("D:\\aboutcpp.txt",ios::in);
    if(!inFile)
    {
        cerr<<"Open Error!"<<endl;
        exit(1);
    }
    while(inFile>>english&&!inFile.eof())
        cout<<english<<"<----------"<<dic.search(english)<<endl;
    inFile.close();
    return 0;
}


运行结果:

心得体会:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值