结构体练习-电子词典

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 8000

struct Dictionary
{
    char english[250];
    char chinese[200];
    char cla[200];
};


struct Dictionary dict[8000];

void read_Dictionary();

int main()
{
    printf("欢迎使用优快云电子词典!\n");
    char key[10];
    read_Dictionary();
    do
    {
        printf("请输入要查询的英文单词:");
        scanf("%s",key);
        if (strcmp(key,"0000")==0)
            break;
        int low,high,mid,index;
        low=0,high=NUM-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if (strcmp(dict[mid].english,key)==0)
            {
                index=mid;
                break;
            }
            else if (strcmp(dict[mid].english,key)>0)
                high=mid-1;
            else low=mid+1;
        }
        if (index>=0)
        {
            printf("%s------>%s\t%s\n",key,dict[index].cla,dict[index].chinese);
        }
        else
            printf("查无此词!\n\n");
    }
    while (1);
    return 0;
}



void read_Dictionary()
{
    FILE *fp;
    int i;
    if ((fp=fopen("dictionary.txt","r"))==NULL)
    {
        printf("dictionary file cannot open!");
        exit(0);
    }
    else
    {
        for (i=0; i<NUM; i++)
        {
            fscanf(fp,"%s%s%s",dict[i].english,dict[i].chinese,dict[i].cla); //读入英文和中文
        }
    }
    fclose(fp);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值