第十七周项目7 电子词典结构体版

/*
*Copyright (c) 2014,烟台大学计算机学院void change(int a[8][8]);
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2014年12月23日
*版本号:v1.0
*
*问题描述:电子词典
*输入描述:英语单词
*程序输出:英语单词所对应的汉语意思
*/

#include <fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
struct Word
{
    string english;
    string chinese;
    string word_class;
};
Word words[8000];
int wordsnum=0;
using namespace std;
void readDictionary();
void searchwords(string);
int binsearch(int low,int high,string k);
int main()
{
    readDictionary();
    string key;
    do
    {
        cout<<"请输入要查询的词(输入0000结束):"<<endl;
        cin>>key;
        if(key!="0000")
            searchwords(key);
    }
    while(key!="0000");
    cout<<"欢迎再次使用!"<<endl<<endl;
    return 0;
}
void readDictionary()
{
    ifstream infile("dictionary.txt",ios::in);
    if(!infile)
    {
        cerr<<"dictionary open error!"<<endl;
        exit(1);
    }
    while (!infile.eof())
    {
        infile>>words[wordsnum].english;
        infile>>words[wordsnum].chinese;
        infile>>words[wordsnum].word_class;
        ++wordsnum;
    }
    infile.close();
}
void searchwords(string key)
{
    int low=0,high=wordsnum-1;
    int a=binsearch(low,high,key);
    if(a==-1)
        cout<<"查无此词"<<endl<<endl;
    else
          cout<<key<<"--->"<<words[a].word_class+"\t"<<words[a].chinese<<endl<<endl;
}
int binsearch(int low,int high,string k)
{
    int mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(words[mid].english==k)
        {
            return mid;
        }
        if(words[mid].english>k)
            high=mid-1;
        else
            low=mid+1;
    }
    return -1;
}


 

结构体应用不熟练

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值