zoukankan      html  css  js  c++  java
  • 编写电子词典(含自定义头文件和链表的建立)

    编辑器加载中...

    本电子词典功能:查单词,单词复习,单词学习,单词填空,根据汉语输入英文,
    有时间提示的单词风暴

    第一部分:main函数编写
    #include <stdio.h>
    #include <string.h>
    #include "list.h"
    #include "tool.h"
    #include "game.h"
    #include <stdlib.h>
    int main()
    {
    int m=0,n=7950,c;
    char ch[130];
    struct wordnode *head=NULL;

    head=Create(head,m);

    printf("欢迎来到本电子词典:\n");
    printf("\n本电子词典功能:\n");
    printf("0 退出");
    printf(" ");
    printf("1 查询\n");
    printf("2 帮助");
    printf(" ");
    printf("3 复习\n");
    printf("4 学习");
    printf(" ");
    printf("5 填空\n");
    printf("6 汉译英");
    printf(" ");
    printf("7 快速记忆\n");
    printf("8 本电子词典详细介绍\n");
    printf("9 清屏\n\n");
    while(1)
    {
    printf(" *007牌电子词典*\n");
    printf("*******************************************************************************\n");
    printf("输入指令(0退出、1查询、2帮助、3复习、4学习、5填空、6翻译、7记忆、8介绍、9清屏)\n");
    printf("*******************************************************************************\n");
    if(scanf("%d",&c)==1)
    {
    if(c==2)
    {
    printf("\n本电子词典功能:\n");
    printf("0 退出");
    printf(" ");
    printf("1 查询\n");
    printf("2 帮助");
    printf(" ");
    printf("3 复习\n");
    printf("4 学习");
    printf(" ");
    printf("5 填空\n");
    printf("6 汉译英");
    printf(" ");
    printf("7 快速记忆\n");
    printf("9 清屏\n\n");
    }
    else if(c==9)
    system("cls");
    else if(c==6)
    {
    game(head,n); //在list.h中
    }
    else if(c==8) //说明文件
    {
    printf("正则表达式:\n");
    printf("输入:正则表达式字符串 待匹配字符串\n");
    printf("输出:如匹配成功,输出整个字符串\n");
    printf("^ 代表字符串开始\n. 代表任意字符\n$ 代表字符串末尾\n");
    printf("示例:\n");
    printf("输入:ads\n");
    printf("输出:croseroads n.交叉路口,十字路\n\n");
    printf("选择3单词学习选项,会每次随机抽出7个单词供您记忆\n");
    printf("1 查询一个单词(支持正则表达式)\n");
    printf("0 退出电子词典\n");
    printf("2 打印帮助文件\n");
    printf("3 单词复习选项\n");
    printf("4 单词学习选项\n");
    printf("5 单词填空\n");
    printf("6 根据汉语写出英文\n");
    printf("7 快速记忆\n");
    printf("9 清屏\n\n");
    }
    else if(c==0)
    {
    printf("欢迎下次使用本词典\n");
    break;
    }
    else if(c==5)
    {
    fill(head,n); //填空
    }
    else if(c==1)
    {
    printf("请输入要搜索的单词:\n");
    getchar();
    gets(ch);
    n=LookUp(head, ch); //n是单词数目
    }
    else if(c==7) //快速记忆
    {
    storm(head,n); //在game.h中
    }
    else if(c==3) //单词复习选项
    {
    Review(); //在list.h中
    }
    else if(c==4) //单词学习选项
    {
    MemWord(head,n); //在list.h中
    }
    else
    printf("非法操作!\n");
    }
    else
    printf("非法操作!\n");
    }

    return 0;
    }

    第二部分:

    game.c文件

    #include "game.h"
    #include <stdio.h>
    #include "list.h"
    #include "tool.h"
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    int game(wordnode *head,int n) //根据汉语拼写
    {
    int count,m,c,flag;
    char ch[32];
    wordnode *p=head;

    if(head == NULL)
    return 0;

    srand(time(NULL));
    while(1)
    {
    flag=0;
    count=0;
    m=rand()%n;
    p=head;
    while(p!=NULL)
    {
    if(count==m)
    {
    printf("%s\n",p->Chinese);
    while(1)
    {
    printf("请输入该汉语的英文翻译: ");
    scanf("%s",ch);
    if(strcmp(ch,p->Eword)==0)
    {
    printf("拼写正确\n");
    break;
    }
    else
    {
    printf("拼写错误,");
    printf("继续输入,请输入1,");
    printf("退出该单词,请输入任意数字\n");

    scanf("%d",&c);
    if(c!=1)
    {
    printf("%s %s\n",p->Eword,p->Chinese);
    break;
    }
    }
    }
    }
    count++;
    p=p->next;
    }
    printf("想要继续进行汉译英检测,请输入1,退出,请输入任意数字\n");
    scanf("%d",&c);
    if(c!=1)
    break;
    }
    return 0;
    }

    int fill(wordnode *head,int n) //随机抽查单词,随机挖空,检查单词拼写情况
    {
    int count,i,x[2],m,c,len,q;
    char ch[32],str[32];
    wordnode *p = head;

    while(1)
    {
    memset(x,0,sizeof(x)); //初始化,清零
    count=0;
    srand(time(NULL));
    m=rand()%n;
    p=head;
    while(p!=NULL)
    {
    if(count==m)
    {
    strcpy(ch,p->Eword);
    len=strlen(p->Eword);
    if(len>4) //单词长度大于四个字母的
    {
    srand(time(NULL)); //随机数种子
    for(i=0;i<2;i++)
    x[i]=rand()%len;
    BubbleSort(x,2);
    q=0;
    for(i=0;i<len;i++)
    {
    if(i==x[q])
    {
    ch[i]='_';
    q++;
    }
    printf("%c",ch[i]);
    }
    printf("\n");
    ch[len]='\0';
    while(1)
    {
    scanf("%s",str);
    if(strcmp(str,p->Eword)==0)
    {
    printf("正确!\n%s %s\n",p->Eword,p->Chinese);
    break;
    }
    else
    {
    printf("错误,重新输入:1;汉语提示:2;退出该单词并显示答案:任意数字\n");
    scanf("%d",&c);
    if(c==2)
    {
    printf("%s\n",p->Chinese);
    printf("请继续输入\n");
    }
    else if(c!=1&&c!=2)
    {
    printf("%s %s\n",p->Eword,p->Chinese);
    break;
    }
    else
    continue;
    }
    }
    }

    else //单词长度小于5个字母的
    {
    srand(time(NULL));
    x[0]=rand()%len;
    for(i=0;i<len;i++)
    {
    if(i==x[0])
    ch[i]='_';
    printf("%c",ch[i]);
    }
    ch[len]='\0';
    printf("\n");
    while(1)
    {
    scanf("%s",str);
    if(strcmp(str,p->Eword)==0)
    {
    printf("正确!\n%s %s\n",p->Eword,p->Chinese);
    break;
    }
    else
    {
    printf("错误,重新输入:1,汉语提示:2,退出该单词并显示答案:任意数字\n");
    scanf("%d",&c);
    if(c==2)
    {
    printf("%s\n",p->Chinese);
    }
    else if(c!=1&&c!=2)
    {
    printf("%s %s\n",p->Eword,p->Chinese);
    break;
    }
    else
    continue;
    }
    }
    }
    }
    count++;
    p=p->next;
    }
    printf("想要继续,请输入数字1,退出,请输入任意数字\n");
    scanf("%d",&c);
    if(c!=1)
    break;
    }
    return 0;
    }
    int storm(wordnode *head,int n)
    {
    int per,x,t,i,j,flag,count,m;
    char str1[100][31],str2[100][101],str3[31];
    wordnode * p = head;
    if(head == NULL)
    return 0;

    printf("请输入要检测的单词数目,以及每个单词限制的时间数\n");
    scanf("%d%d",&x,&per);
    t=x;
    x=x*per;
    i=0;
    while(x)
    {
    srand(time(NULL));
    flag=0;
    count=0;
    printf("\r"); //使得时间光标停在同一处地方
    printf("剩余时间:%d秒",x--); //输出时间
    m=rand()%n;
    Sleep(1000); //产生时间间隔
    p=head;
    while(p!=NULL)
    {
    if(count==m&&(x+1)%per==0)
    {
    printf(" %s %s",p->Eword,p->Chinese); //输出中英文,记忆
    flag++; //记录已出现的单词数
    strcpy(str1[i],p->Eword); //以下四条语句是保存该出现的单词
    str1[i][strlen(p->Eword)]='\0'; //使该数组结尾
    strcpy(str2[i],p->Chinese);
    str2[i++][strlen(p->Chinese)]='\0';
    }
    count++;
    p=p->next;
    }
    if(flag==x) //输出与输入的单词数一致时,跳出循环
    break;
    }

    system("cls"); //清屏
    for(i=0;i<t;i++)
    {
    printf("现在开始测试:\n");
    printf("第%d个 %s\n",i+1,str2[i]); //输出中文提示
    scanf("%s",str3); //输入该单词
    if(strcmp(str3,str1[i])==0) //判断是否一致
    printf("正确\n");
    else
    printf("错误,答案是 %s\n",str1[i]);
    if(i<t-1) //当还没把所有单词输出,提问是否继续考查,若已输出完毕,不执行该语句
    {
    printf("继续完成下面的单词,输入1,否则,按任意数字键\n");
    scanf(" %d",&j);
    if(j!=1)
    break;
    }
    }
    return 0;
    }

    第三部分:

    game.h文件

    #ifndef _GAME_H
    #define _GAME_H
    #include "list.h"
    #include "tool.h"
    int game(wordnode *head,int n);
    int fill(wordnode *head,int n);
    int storm(wordnode *head,int n);
    #endif

    第四部分

    list.c文件

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "list.h"
    #include "tool.h"
    #include <time.h>
    int n;
    int RegSearch(char re[],char target[])
    {
    int len_re,len_target;
    int i,j;

    len_re = strlen(re);
    len_target = strlen(target);

    if(re[0]=='^'&&re[len_re-1] == '$')
    {
    if((len_re-2)!=len_target)
    return 0;
    else
    {
    for(i= 1;i<len_re-1;i++)
    if(re[i]!=target[i-1]&&re[i]!='.')
    return 0;
    if(i==len_re-1)
    return 1;
    }
    }

    if(re[0]=='^'&&re[len_re-1] != '$')
    {
    if(len_re-1>len_target)
    return 0;
    else
    {
    for(i= 1;i<len_re;i++)
    if(re[i]!=target[i-1]&&re[i]!='.')
    return 0;
    if(i==len_re)
    return 1;
    }
    }

    if(re[0]!='^'&&re[len_re-1] == '$')
    {
    if(len_re-1>len_target)
    return 0;
    else
    {
    for(i= len_re-2,j=len_target-1;i>=0;i--,j--)
    if(re[i]!=target[j]&&re[i]!='.')
    return 0;
    if(i==-1)
    return 1;
    }
    }

    if(re[0]!='^'&&re[len_re-1] != '$')
    {
    if(len_re>len_target)
    return 0;
    else
    {
    for(j = 0;j<=len_target-len_re;j++)
    {
    for(i = 0;i<len_re;i++)
    {
    if(re[i]!=target[j+i]&&re[i]!='.')
    {
    break;
    }
    }
    if(i==len_re)
    return 1;
    }
    if(j==len_target-len_re+1)
    return 0;
    }
    }
    return 0;
    }

    wordnode * Create(struct wordnode *head,int m) //创建链表
    {
    char ch[133];
    char str1[31],str2[101];

    struct wordnode *p=NULL,*pr=head;

    FILE * f1 = NULL;
    f1 = fopen("dict.TXT","r");
    if(f1 ==NULL)
    return 0;

    while(fgets(ch,300,f1)!=NULL)
    {
    m++;
    Trim(ch);
    Seg(ch,str1,str2);
    p=(struct wordnode*)malloc(sizeof(struct wordnode));
    strcpy(p->Eword,str1);
    strcpy(p->Chinese,str2);

    if(head==NULL)
    {
    head=p;
    pr=head;
    }
    else
    {
    while(pr->next!=NULL)
    pr=pr->next;
    pr->next=p;
    }
    p->next=NULL;
    }
    n=m;
    printf("本电子词典共收录单词总数%d条\n",m);
    fclose(f1);
    return head;
    }

    int LookUp(wordnode * head, char word[]) //查找单词
    {
    int count=0;
    wordnode * p = head;
    if(head == NULL)
    return 0;

    while(p!=NULL)
    {
    if(RegSearch(word,p->Eword)==1)
    {
    printf("%s %s\n",p->Eword,p->Chinese);
    count++;
    }
    p=p->next;
    }
    printf("共找到%d项相匹配\n",count);
    return n;
    }

    int MemWord(wordnode *head,int num)
    {
    int i,nums[7];
    FILE *f1 = NULL;
    FILE *f2=NULL;

    struct tm *nt;
    time_t lt;
    char str[80];
    int n=0;
    wordnode * p = head;

    lt=time(NULL);
    nt=localtime(&lt);
    strftime(str,sizeof(str),"%Y-%m-%d",nt);
    printf("%s\n",str);

    f1=fopen(str,"w");
    f2 = fopen("dict.TXT","r");

    memset(nums,0,sizeof(nums));

    srand(time(NULL)); //产生随机数
    for(i=0;i<7;i++)
    nums[i]=rand()%num;

    BubbleSort(nums,7); //利用bubble来排序

    i=0;
    if(head == NULL)
    return 0;

    while(p!=NULL&&i<=6)
    {
    if(n==nums[i])
    {
    i++;
    printf("%s %s\n",p->Eword,p->Chinese);
    fputs(p->Eword,f1); //学习到的单词记录到另一个文件
    fputs(" ",f1);
    fputs(p->Chinese,f1);
    fputs("\n",f1);
    }
    n++;
    p=p->next;
    }

    fclose(f1);
    fclose(f2);
    return 0;
    }

    int Review()
    {
    int day;
    char ch[132];
    FILE *f1=NULL;
    struct tm *nt;
    time_t lt,t1;
    char str[80];
    lt=time(NULL);
    nt=localtime(&lt);
    strftime(str,sizeof(str),"%Y-%m-%d",nt);
    printf("今天日期:%s\n",str);

    printf("想要复习哪时的单词?请输入几天前(想看今天的可以输入0)\n");
    scanf("%d",&day);
    nt->tm_sec =nt->tm_sec-86400*day; //现在的时间减去这几天的过去了的时间,就是当时日期

    t1=mktime(nt);
    nt=localtime(&t1);
    strftime(str,sizeof(str),"%Y-%m-%d",nt);
    printf("%s\n",str);

    printf("您现在复习的日期是:%s\n\n",str);
    f1=fopen(str,"r");
    if(f1==NULL)
    {
    printf("此文件不存在\n");
    return 0;
    }
    else
    {
    while(fgets(ch,132,f1)!=NULL)
    printf("%s",ch);
    }
    printf("\n");
    fclose(f1);

    return 0;
    }

    第五部分

    list.h文件

    #ifndef _LIST_H
    #define _LIST_H
    typedef struct wordnode{
    char Eword[31];
    char Chinese[101];
    struct wordnode * next;
    } wordnode;
    int RegSearch(char reg[ ], char target[ ]);
    wordnode * Create(struct wordnode *head,int m);
    int LookUp(wordnode * head, char word[]);
    int MemWord(wordnode *head,int num);
    int Review();
    #endif

    第六部分

    tool.c文件

    #include<stdio.h>
    #include"tool.h"
    #include "list.h"
    #include<string.h>
    #include<ctype.h>
    #include "game.h"
    int Seg(char ch[],char str1[101],char str2[31])
    {
    int i,len,p,flag=0;

    len=strlen(ch);
    for(i=0;i<len;i++)
    {
    if(ch[i]!=' '&&flag==0)
    {
    str1[i]=ch[i];
    p=i;
    }
    else
    {
    flag=1;
    str2[i-p-1]=ch[i+1];
    }
    }

    str1[p+1]='\0';
    str2[len-p-1]='\0';

    return 0;
    }


    int Trim(char word[])
    {
    int i,p=0,q=0,x;

    x=strlen(word);

    for(i=0;i<x;i++)
    {
    if(word[i]==' '||word[i]=='\n'||word[i]=='\t')
    p++;
    else
    break;
    }
    for(i=0;i<x;i++)
    word[i]=word[p+i];
    x=x-p;
    q=x;
    for(i=x-1;i>=0;i--)
    {
    if(word[i]==' '||word[i]=='\n'||word[i]=='\t')
    q--;
    else
    break;
    }
    word[q]='\0';
    return 0;
    }

    int BubbleSort(int nums[], int n)
    {
    int i,j,flag=0,swap;

    for(i=0;i<n;i++)
    {
    for(j=1;j<n;j++)
    {
    if(nums[j-1]>nums[j])
    {
    swap=nums[j];
    nums[j]=nums[j-1];
    nums[j-1]=swap;
    flag=1;
    }
    }
    if(flag==0)
    break;
    }

    return 0;
    }

    第七部分

    tool.h文件

    #ifndef _TOOL_H
    #define _TOOL_H
    int Trim(char word[132]);
    int Seg(char word[132],char p[101],char q[31]);
    int BubbleSort(int nums[], int n);
    #endif

    第八部分

    自己弄个七八千的单词量的txt文件,改名为dict.TXT即可

  • 相关阅读:
    Citrix Receiver running on my mobile phone
    is undfined javascript error
    系统架构设计随笔
    计算机与数理化“最高”期刊之比较zt
    Tikhonov regularization
    关于Likelihood 和 Probability的差别
    Cross Validation
    八卦 Knuth zt
    Eclipse切换IDE界面语言
    数学家对数学的论述
  • 原文地址:https://www.cnblogs.com/yejinru/p/2374747.html
Copyright © 2011-2022 走看看