3136
This commit is contained in:
12
include/solution/3136.h
Normal file
12
include/solution/3136.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef INC_3136_H
|
||||||
|
#define INC_3136_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <stdbool.h>
|
||||||
|
bool isValid(char *word);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
33
src/3136.c
Normal file
33
src/3136.c
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <solution/3136.h>
|
||||||
|
|
||||||
|
bool isVowel(char c)
|
||||||
|
{
|
||||||
|
return c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' ||
|
||||||
|
c == 'U';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isConsonant(char c)
|
||||||
|
{
|
||||||
|
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && !isVowel(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isValid(char *word)
|
||||||
|
{
|
||||||
|
int length = 0;
|
||||||
|
bool haveVowel = false;
|
||||||
|
bool haveConsonant = false;
|
||||||
|
|
||||||
|
for (; *word != '\0'; word++, length++)
|
||||||
|
{
|
||||||
|
haveVowel = haveVowel || isVowel(*word);
|
||||||
|
haveConsonant = haveConsonant || isConsonant(*word);
|
||||||
|
if (!isdigit(*word) && !isVowel(*word) && !isConsonant(*word))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length < 3)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return haveVowel && haveConsonant;
|
||||||
|
}
|
||||||
35
tests/test_3136.cpp
Normal file
35
tests/test_3136.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/3136.h>
|
||||||
|
|
||||||
|
// 测试类定义
|
||||||
|
class IsValidTest : public ::testing::Test
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
// 示例 1:输入 "234Adas",输出 true
|
||||||
|
TEST_F(IsValidTest, ValidWord)
|
||||||
|
{
|
||||||
|
char word[] = "234Adas";
|
||||||
|
EXPECT_TRUE(isValid(word));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 2:输入 "b3",输出 false(长度 < 3 且无元音)
|
||||||
|
TEST_F(IsValidTest, TooShortNoVowel)
|
||||||
|
{
|
||||||
|
char word[] = "b3";
|
||||||
|
EXPECT_FALSE(isValid(word));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 3:输入 "a3$e",输出 false(包含非法字符 $ 且无辅音)
|
||||||
|
TEST_F(IsValidTest, HasSymbolNoConsonant)
|
||||||
|
{
|
||||||
|
char word[] = "a3$e";
|
||||||
|
EXPECT_FALSE(isValid(word));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 3:输入 "3pp",输出 false(无元音)
|
||||||
|
TEST_F(IsValidTest, NoConsonant)
|
||||||
|
{
|
||||||
|
char word[] = "3pp";
|
||||||
|
EXPECT_FALSE(isValid(word));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user