zoukankan      html  css  js  c++  java
  • 666

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define INITSIZE 100
    //定义结构体类型,用于表示单词类型
    typedef struct word
    {
      char spell[100];//定义spell成员,用于存储单词的英文拼写
      char interpretation[50] ;//定义interpretation成员,用于存储单词的中文意思
    }word;
    typedef struct SqList   //定义存储单词的顺序表存储结构
    {
        word *list;     //list数组用于存放从文件读入的单词表
        int length;     //顺序表的长度
        int listsize;   //顺序表的容量
    }SqList;
    void Initiate(SqList &L);
    void Load(SqList &L);//子函数声明
    void Print(SqList L) ; 
    void ceshi(SqList L);
    int main()
    {
         SqList L;         //定义顺序表变量L
         Initiate(L);      //初始化顺序表L
         Load(L);          //从文本文件中读取单词存入顺序表L中
         Print( L);       // 将顺序表L中的单词输出到屏幕上    
    	 ceshi(L);        //测试英文 
    }
    void Initiate(SqList &L)//初始化顺序表L
    {
         L.list=(word*)malloc(INITSIZE*sizeof(word)) ;  //为顺序表申请内存空间
        if(!L.list)        //如果申请空间失败,退出程序
        {
            printf("内存空间不足!");//在屏幕上输出“内存空间不足!”
            exit(0);             //退出程序
        }
            L.length=0;//初始化单词表的长度值为0
            L.listsize=INITSIZE;//初始化单词表的容量为INITSIZE值
    }
    void Load(SqList &L)
     //从文本文件中读取单词存入顺序表L中
    {
        FILE *fp;//定义文件指针fp
        char filename[30];
        printf("请输入文件路径\n");
        scanf("%s",filename);
        if((fp=fopen(filename,"r"))==NULL)    //打开文件,如果文件打开失败,退出程序
        {
          printf("文件不存在!");//在屏幕上输出“文件不存在!”
            exit(1);
        }
        int i=0;//i变量用于记录从文件读入的单词个数
    
        while(!feof(fp)&&i<L.listsize)    //循环   printf("请输入要测试单词的文件名:");读入fp指针指向的文本文件中的每一个单词极其中文翻译
        {
             fgets(L.list[i].spell,100,fp);  //在文件中读取数据参数L.list[i].spell存储位置  100最大字符数  fp文件指针
             fgets(L.list[i].interpretation,100,fp); //在文件中读取数据参数L.list[i].interpretationl存储位置  100最大字符数  fp文件指针
             i++;   //单词个数增加1
        }
    
         fclose(fp); //关闭文件
         L.length=i;  //将顺序表中单词个数赋值给L.length
    }
    void Print(SqList L)
     //将顺序表L中的单词输出到屏幕上
    {
    	for(int i=0;i< L.length;i++)
    	{
    	  printf("%s\n",L.list[i].spell);
          printf("%s\n",L.list[i].interpretation);
        }
    }
    void ceshi(SqList L)
    { char zhong[50];
    	for(int i=0;i< L.length;i++)
            {
    		printf("%s\n",L.list[i].spell);
            scanf("%s",zhong);
            if(!(strcmp(zhong, L.list[i].interpretation)))
                printf("恭喜你答对了");
    		else
    	      	{
    			  printf("输入错误从新作答" );
                  continue;
    			} 
    
    
    }    
    }
  • 相关阅读:
    2019/1/2
    2018/12/22
    2018/12/18
    2018/12/17
    2018/12/16
    编程总结汇总
    学习总结汇总
    第十三周学习总结--助教
    第十一周编程总结
    第10周编程总结
  • 原文地址:https://www.cnblogs.com/tanbo/p/15591085.html
Copyright © 2011-2022 走看看