第17周项目电子词典结构体版

/*
*copyright (c)2014,烟台大学计算机学院
*All rights reserved
*文件名称:789.cpp
*作者:孙春红
*完成日期:2014年2月12日
*版本号:v1.0
*
*问题描述:做一个简单的电子词典,输入英语,输出他的汉语和词性。
*程序输入: 输入要查找的英语单词;
*程序输出:输出英语的汉语和词性。 
*/
#include <fstream>   //操作文件必写
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct Word
{
    string english;
    string chinese;
    string word_class;
};
int main( )
{
    Word words[8000];
    int wordsNum=0;
    string key;
    //以输入的方式(ios::in)打开文件
    ifstream infile("dictionary.txt",ios::in);    //测试是否成功打开,打开失败时(如要读的数据文件不存在)退出
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while (!infile.eof())
    {
        infile>>words[wordsNum].english;
        infile>>words[wordsNum].chinese;
        infile>>words[wordsNum].word_class;
        ++wordsNum;
    }
    infile.close(); //读入完毕一定要关闭文件
    cout<<"请输入您想要查找的单词(0000结束):"<<endl;
    while (cin>>key&&key!="0000")  //一直输入,直到输入0000
    {
        int low=0,high=wordsNum-1,mid,f1,f2;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(words[mid].english==key)
            {
                f1=1;
                f2=mid;
                break;
            }
            else
            {
                if(words[mid].english>key)
                    high=mid-1;
                else
                    low=mid+1;
            }

        }
        if(f1==0)
        {
            cout<<"对不起,查无此词。"<<endl;
        }
        if(f1==1)
        {
            cout<<key<<"-->"<<words[f2].word_class<<"-->"<<words[f2].chinese<<endl;
            f1--;
        }


    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值