第十四周项目4 电子词典

来源:互联网 发布:网络侵权法律规定 编辑:程序博客网 时间:2024/05/23 01:22
/*  *Copyright (c) 2014,烟台大学计算机学院  *All rights reserved.  *文件名称:main.cpp  *作者:苏强  *完成日期:2014年11月31日  *版本号:v1.0  *  *问题描述:电子词典*输入描述:单词*程序输出:汉语意思 */ #include<fstream>    //处理文件要包括头文件fstream#include<iostream>#include<string>#include<cstdlib>   //调用exit(1)需要包含cstdlibusing namespace std;int wordnum=0;string e[8000],c[8000];int binsearch(int low,int high,string k);int main(){    string key;    ifstream infile("dictionary.txt",ios::out);    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    while(infile>>e[wordnum]>>c[wordnum])    {        ++wordnum;    }    infile.close();  //关闭文件。用完了必须关闭,否则会丢失数据    do    {        cout<<"请输入要查找的词(输入0000退出):";        cin>>key;        if(key=="0000")            break;        else        {            int low=0,high=wordnum-1;            int a=binsearch(low,high,key);            if(a==-1)                cout<<"查无此词"<<endl<<endl;            else                cout<<key<<"的中文意思是"<<c[a]<<endl<<endl;        }    }    while(1);    cout<<"*****欢迎再次使用*****"<<endl;    cout<<endl;    return 0;}int binsearch(int low,int high,string k){    int mid;    while(low<=high)    {        mid=(low+high)/2;        if(e[mid]==k)        {            return mid;        }        if(e[mid]>k)            high=mid-1;        else            low=mid+1;    }    return -1;}

0 0