查找从未在文本中的所有单词中使用的所有元音

时间:2015-12-22 21:06:52

标签: c

我编写的代码可以找到文本中所有单词中使用的所有元音。我不知道如何转移它。我需要重写所有代码吗? 所以,我需要有这样的结果:

文本:

  

wwe w fa

结果:

  

你我

#include <stdio.h>
#include <ctype.h>
#define vowel (1u<<('a'-'a') | 1u<<('e'-'a') | 1u<<('i'-'a') | 1u<<('o'-'a') | 1u<<('u'-'a'))
unsigned int char_to_set(char c) 
{
    c = tolower(c);
    if (c < 'a' || c > 'z') 
        return 0; else return 1u<<(c-'a'); 
}
int letter(int c) 
{
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
int sign(int c) 
{
    return c == ' ' || c == ',' || c == '\n' || c == '\t';
}
int main () 
{
    int c, flag=0;
    char alpha;
    unsigned int sl = 0, mn = vowel;
    FILE *pf;
    pf=fopen("l13.txt","r");
    printf ("Ishodnyi text:\n\n");
    while (!feof(pf)) 
    {
        c=getc(pf);
        printf("%c",c);
        switch (flag) 
        {
            case (0): 
            {
                if (letter(c)) 
                             { 
                               sl = sl | char_to_set(c); 
                               flag = 1;
                            } 
                if (sign(c)) flag = 0;
                break;
            }
            case (1): 
            {
                if (letter(c)) 
                              { 
                                sl = sl | char_to_set(c); 
                                flag = 1;
                              }
                if (sign(c)) 
                             { 
                              mn = mn & sl; 
                              sl = 0; 
                              flag = 0;
                             }
                break;
            }
        }
    }

    if (mn == 0) { printf ("\n\n no vowels are included in all word"); } else {                                                                         printf ("\n\n vowels are included in all word:\n");                                                                         for(alpha='a'; alpha <= 'z'; alpha++){                                                                                                                          if((mn & char_to_set(alpha)) != 0){                                                                                                                             printf("%c ", alpha);
    }  
}
} 
fclose(pf);
getchar();
return 0;
}

1 个答案:

答案 0 :(得分:1)

有很多方法可以做你想要的。以下是一种方式。可能有更好的方法,但希望它会给你一些改进它的想法。

如果我正确理解您的代码,mn包含文本中存在的元音的位掩码。所以你可以编写一个函数来检查所有未设置的元音位。以下代码仅检查ae,但我认为应该清楚如何将其扩展为其他元音。

#define A_MASK (1u<<('a'-'a'))
#define E_MASK (1u<<('e'-'a'))

/*
 * Convenience struct for associating masks with characters.
 * Could be done without this by deriving the character from the mask
 * but this (IMHO) makes the code simpler to understand.
 */
struct {
     unsigned int mask
     char c;
} masks[] =  { { A_MASK, 'a'} , { E_MASK, 'e'} };

void vowels_not_present (unsigned int vowels_mask)
{
    int ix;
    for (ix = 0; ix < sizeof(masks) / sizeof(masks[0]); ix++) {
        if (!(vowels_mask & masks[ix].mask)) {
            printf("vowel %c is not present\n", masks[ix].c);
        }
    }
}

然后在你的main中调用上述函数:

vowels_not_present(mn);