diff --git a/include/solution/8.h b/include/solution/8.h new file mode 100644 index 0000000..e8a0b80 --- /dev/null +++ b/include/solution/8.h @@ -0,0 +1,14 @@ +// This file is generated by mkfile.py +// Date: 2026-06-25 + +#ifndef INC_8_H +#define INC_8_H +#ifdef __cplusplus +extern "C" +{ +#endif + int myAtoi(char *s); +#ifdef __cplusplus +} +#endif +#endif // INC_8_H diff --git a/src/8.c b/src/8.c new file mode 100644 index 0000000..1116268 --- /dev/null +++ b/src/8.c @@ -0,0 +1,58 @@ +// This file is generated by mkfile.py +// Date: 2026-06-25 + +#include + +#include +#include +int myAtoi(char *s) +{ + long ret = 0; + int length = 0; + bool isPos = true; + bool hasSign = false; + bool hasDigit = false; + bool hasSpace = true; + + for (; *s != '\0' && length < 11; s++) + { + // step 1 skip space + if (hasSpace) + { + if (*s == ' ') + continue; + hasSpace = false; + } + // step 2 check is sign while has no number + if (!hasDigit && !hasSign && (*s == '-' || *s == '+')) + { + hasSign = true; + isPos = *s == '+'; + continue; + } + // step 3 read num + if (*s - '0' >= 0 && *s - '9' <= 0) + { + hasDigit = true; + // check if front zero + if (length == 0 && *s == '0') + continue; + + // not front zero + if (length > 0) + ret *= 10; + ret += *s - '0'; + length++; + continue; + } + // step 4 check char + break; + } + + if (isPos) + ret = ret > INT_MAX ? INT_MAX : ret; + else + ret = ret * -1 < INT_MIN ? INT_MIN : ret * -1; + + return (int)ret; +} \ No newline at end of file diff --git a/tests/test_8.cpp b/tests/test_8.cpp new file mode 100644 index 0000000..963814d --- /dev/null +++ b/tests/test_8.cpp @@ -0,0 +1,87 @@ +// This file is generated by mkfile.py +// Date: 2026-06-25 + +#include +#include + +#include +#include +#include + +class MyAtoiTest : public ::testing::Test +{ + protected: + void AssertMyAtoi(const std::string &input, int expected) + { + std::vector buffer(input.begin(), input.end()); + buffer.push_back('\0'); + + EXPECT_EQ(myAtoi(buffer.data()), expected); + } +}; + +TEST_F(MyAtoiTest, Example1) +{ + AssertMyAtoi("42", 42); +} + +TEST_F(MyAtoiTest, Example2) +{ + AssertMyAtoi(" -42", -42); +} + +TEST_F(MyAtoiTest, Example3) +{ + AssertMyAtoi("4193 with words", 4193); +} + +TEST_F(MyAtoiTest, ReturnsZeroWhenStringStartsWithNonDigit) +{ + AssertMyAtoi("words and 987", 0); +} + +TEST_F(MyAtoiTest, ReadsOptionalPlusSign) +{ + AssertMyAtoi("+1", 1); +} + +TEST_F(MyAtoiTest, RejectsMultipleOrSeparatedSigns) +{ + AssertMyAtoi("+-12", 0); + AssertMyAtoi("-+12", 0); + AssertMyAtoi(" + 413", 0); +} + +TEST_F(MyAtoiTest, StopsAtFirstNonDigitAfterReadingDigits) +{ + AssertMyAtoi("3.14159", 3); + AssertMyAtoi("00000-42a1234", 0); + AssertMyAtoi(" +0 123", 0); +} + +TEST_F(MyAtoiTest, HandlesEmptyAndSignOnlyStrings) +{ + AssertMyAtoi("", 0); + AssertMyAtoi(" ", 0); + AssertMyAtoi("+", 0); + AssertMyAtoi("-", 0); +} + +TEST_F(MyAtoiTest, ClampsPositiveOverflow) +{ + AssertMyAtoi("2147483647", INT_MAX); + AssertMyAtoi("2147483648", INT_MAX); + AssertMyAtoi("91283472332", INT_MAX); +} + +TEST_F(MyAtoiTest, ClampsNegativeOverflow) +{ + AssertMyAtoi("-2147483648", INT_MIN); + AssertMyAtoi("-2147483649", INT_MIN); + AssertMyAtoi("-91283472332", INT_MIN); +} + +TEST_F(MyAtoiTest, IgnoresLeadingZerosBeforeValidDigits) +{ + AssertMyAtoi(" 0000000000012345678", 12345678); +}