qq_19701837
qq_19701837
采纳率70%
2019-03-04 10:42

单词复数 萌新求助 用c语言解决

5
已采纳

单词复数。某单词本(文件)中有若干行单词单数形式,请在该单词后面给出单词复数形式,即单
词本中每一行是“单数 复数”形式。注:如果单词以 s、x、z、ch、或 sh 结尾,单词加 es;如果单词以 y 结
尾,并且 y 前面是一个辅音,将 y 改为 ies;对其他单词,后面加 s。
要代码,最好有备注

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • nituxiaocao nituxiaocao 2年前

    适用于每行一个单词的情况

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define BUFFER_SIZE  1024
    
    typedef enum
    {
        WORDS_ADD_S = 0,
        WORDS_ADD_ES,
        WORDS_ADD_IES,
    }words_add_enum;
    
    char yuanyi_judge(char c)
    {
        int i = 0;
        char yuanyin[] = {'a','e','i','o','u'};
    
        for(;i < strlen(yuanyin); i++)
        {
            if(c == yuanyin[i])
            {
            //  printf("是元音\n");
                return 1;
            }
        }
    
        //printf("不是元音\n");
        return 0;
    }
    
    //判断末尾是否需要加es,last 最后一个单词,second 倒数第二个单词
    char words_is_nedd_add_es(char last,char second)
    {
        char res = 0;
    
        if(last == 'h')
        {
            if(second == 'c' || second == 's')
            {
                res = 1;
            }
        }
        else if(last == 's' || last == 'x'|| last == 'z')
        {
            res = 1;
        }
        else
        {
            res = 0;
        }
    
        //printf("末尾需要加es吗:%s\n",res == 1 ? "yes":"no");
        return res;
    }
    
    char data_combin(char *src,FILE *save)
    {
        int data_len = 0;
        char *temp = NULL;
        words_add_enum words_add = WORDS_ADD_S;
        if(!src || !save)
        {
            //printf("%s,参数为空\n",__FUNC__);
            return 0;
        }
    
    
    
        data_len = strlen(src) ;
        //数据长度不含换行符
        if(src[data_len - 1] == '\n')
        {
            src[data_len - 1] = '\0';
            data_len -= 1;
        }
    
        ///拷贝单词
        temp = (char *)malloc(data_len + 1);
        strcpy(temp,src);
    
        if(data_len <= 1)
        {
            printf("单词无复数,原样输出\n");
            fputs(src,save);
            fputs("\n",save);
        }
        else
        {
            if(src[data_len -1] == 'y')
            {
                //是元音,加s
                if(!yuanyi_judge(src[data_len -2]))
                {
                    words_add = WORDS_ADD_IES;
                }       
            }
            else if(words_is_nedd_add_es(src[data_len -1],src[data_len -2]))
            {
                words_add = WORDS_ADD_ES;
            }
    
            //src[data_len-1]='\0';
            if(words_add == WORDS_ADD_ES)
            {
                sprintf(src + data_len," %s%s\n",temp,"es");
            }
            else if(words_add == WORDS_ADD_IES)
            {
                sprintf(src + data_len," %s",temp);
                //去掉y
                sprintf(src + strlen(src) - 1 ,"%s\n","ies");
            }
            else
            {
                //sprintf(src + data_len," %s%s",src,"s");
                sprintf(src + data_len," %s%s\n",temp,"s");
    
            }
    
            ///不会自动输出换行
            fputs(src,save);
    
        }
        //释放内存
        free(temp);
        printf("复数%s\n",src);
    
        return 1;
    }
    
    //in_path 数据原始文件,out_path 转换后保存的文件
    void words_convert(char *in_path,char *out_path)
    {
        FILE *fp = NULL;
        FILE *fp_save = NULL;
        int num = 0;
        //char *temp_path = "./temp.txt";
        char buf[BUFFER_SIZE] = {0};
    
        if(!in_path || !out_path)
        {
            return ;
        }
    
        if((fp = fopen(in_path,"r")) == NULL)
        {
            printf("读取数据文件失败\n");
            return ;
        }
    
        if((fp_save = fopen(out_path,"w")) == NULL)
        {
            printf("创建临时数据文件失败\n");
            return ;
        }
    
        while(!feof(fp))
        {
            memset(buf,0,BUFFER_SIZE);
            if(!fgets(buf,BUFFER_SIZE,fp))
            {   
                printf("读取数据文件出错\n");
                return;
            }
    
            data_combin(buf,fp_save);
        }
    
        fclose(fp);
        fclose(fp_save);
    
    
    }
    
    //函数适用用每行一个单词的情况
    void main(void)
    {
        words_convert("words.txt","temp.txt");
    
        system("pause");
    }
    
    点赞 1 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 2年前

    如果问题得到解决,请点我回答左上角的采纳,采纳后可以回答你的别的问题

    char word[200];
    scanf("%s", word);
    int n = strlen(word);
    if (word[n - 1] == 's' || word[n - 1] == 'x' || word[n - 1] == 'z')
    {
    word[n] = 'e';
    word[n + 1] = 's';
    word[n + 2] = '\0';
    }
    else if (n >= 2 && word[n - 1] == 'h')
    {
    if  (word[n - 2] == 'c' || word[n - 2] == 's')
    {
    word[n] = 'e';
    word[n + 1] = 's';
    word[n + 2] = '\0';
    }
    else
    {word[n]='s';word[n+1]='\0';}
    }
    else if (n >= 2 && word[n - 1] == 'y')
    {
    if (word[n - 2] != 'a' && word[n - 2] != 'e' && word[n - 2] != 'i' && word[n - 2] != 'o' && word[n - 2] != 'u')
    {
    word[n - 1] = 'i';
    word[n] = 'e';
    word[n + 1] = 's';
    word[n + 2] = '\0';
    }
    else
    {word[n]='s';word[n+1]='\0';}
    }
    else
    {word[n]='s';word[n+1]='\0';}
    printf("%s", word);
    
    点赞 评论 复制链接分享

为你推荐