<< c >>字典查单词

单词查询
通过他们可以练习知识点:
1.函数
2.结构体
3.指针
4.存储类型,动态,静态,全局,局部
5.文件的使用
6.顺序查询,折半查询

include “stdio.h”

include “stdafx.h”

include “stdlib.h”

include “string.h”

define N 3600

typedef struct word {
char english[20];
char chinese[30];
}word;
word s[N];
FILE * in;
typedef struct table {
word * elem;
int length;
}table;
table st;
void init();
void data();
int search(table *a, char *key);
int main()
{
int i;
char sd[30];
data();
init();
printf(“\n请输入待查找单词英文: “);
scanf(“%s”, sd);
i = search(&st, sd); /* 顺序查找 */

return 0;
//printf("%d",search(st, "apple"));

}
void data(){
if ((in = fopen(“d://word.txt”,”a+”))== NULL){
exit(0);
}
int i = 0;
while( fscanf(in,”%s”,&s[i].english)!=EOF ){
fscanf(in, “%s”, &s[i].chinese);
//printf(s[i].english);
i++;
}
printf(“\n”);
//printf(“%s %s”,s[418].english,s[418].chinese);//查看数据是否导入
fclose(in);
}
void init() {
int i = 0;
st.elem = (word *)calloc(N,sizeof(word));
word * head = st.elem;
if (!st.elem)
exit(0);

    while (i<N) {
        *(st.elem) = s[i];
        i++;
        //printf("%s\n", st.elem->english);//查看顺序表是否生成
        st.elem++;
        }   
    st.length = N;
    st.elem = head;
}
int search(table *a, char *key) 
{               
    for (int i =0;i<N;i++,(a->elem)++)
    {       
        char *ww = a->elem->english;

        if (!(strcmp(ww, key))) {
            printf("\n:%s", a->elem->chinese);
            return i;
        }

    }
    printf("can't find it");
    return 1;
}

你可能感兴趣的:(<< c >>字典查单词)