gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:01:45 26 4
gpt4 key购买 nike

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

文字:

wwe w fa

结果:

o u i

#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;
}

最佳答案

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

如果我正确理解您的代码,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);

关于c - 查找文本中所有单词中从未使用过的所有元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34424426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com