首页 > 技术知识 > 正文

【C语言面试】源码实现标准库函数atoi

你有面试中,要求写atoi的源码实现吗?本文给出一个参考写法!

1 需求说明2 源码实现2.1 函数申明2.2 功能实现3 源码测试4 小小总结

1 需求说明

题目大意如下:

请参考标准库对atoi的申明定义,使用C语言的语法写出其实现源码。

2 源码实现

2.1 函数申明

通过查看man帮助,我们可以知道atoi函数的功能及其简要申明。

复制NAME atoi, atol, atoll – convert a string to an integer ​ SYNOPSIS #include int atoi(const char *nptr); long atol(const char *nptr); long long atoll(const char *nptr); ​ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ​ atoll(): _ISOC99_SOURCE || || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE ​ DESCRIPTION The atoi() function converts the initial portion of the string pointed to by nptr to int. The behavior is the same asstrtol(nptr, NULL, 10); ​ except that atoi() does not detect errors. ​ The atol() and atoll() functions behave the same as atoi(), except that they convert the initial portion of the string to their return type of long or long long. ​ RETURN VALUE The converted value.

2.2 功能实现

以下是我的一个简单实现源码,仅供参考:

复制int my_atoi(const char *nptr) { int flag = 1; int ret = 0; char *p = (char *)nptr; ​ while (*p == ) { p++; } ​ if (*p == + || *p == ) { if (*p == ) { flag = -1; } p++; } ​ while (*p != ) { if ((*p >= 0) && (*p <= 9)) { ret = ret * 10 + *p – 0; p++; } else { p++; break; } } ​ return flag * ret; } 3 源码测试

简单的测试代码如下:

复制#include #include ​ static const char *test_case_list[] = { “1234”, “-1234”, “1234a5678”, “-1234a5678”, ” 1234″, ” -1234″, ” 1234a5678″, ” -1234a5678″, “abcd”, “”, “01234”, “-01234” }; ​ #ifndef ARRAY_SIZE #define ARRAY_SIZE(n) sizeof(n) / sizeof(n[0]) #endifint main(int argc, const char *argv[]) { int i; ​ for (i = 0; i < ARRAY_SIZE(test_case_list); i++) { printf(“atoi-%d : [%s] => %d “, i, test_case_list[i], atoi(test_case_list[i])); printf(“my-atoi-%d: [%s] => %d “, i, test_case_list[i], my_atoi(test_case_list[i])); printf(“test-%d : [%s] “, i, atoi(test_case_list[i]) == my_atoi(test_case_list[i]) ? ” OK “ : “FAIL”); } ​ return 0; }

简单写了build.sh脚本做编译测试:

复制#! /bin/bash -e ​ CFLAGS=“-Wall -Werror” cmd=“gcc *.c $CFLAGS -o test”if [ $1 = “clean” ]; then rm -rf test echo “Clean build done !” exit 0 fiecho $cmd && $cmd

执行编译后,运行小程序的结果:

复制my_atoi$ ./test gcc main.c -Wall -save-temps=obj -o test atoi-0 : [1234] => 1234 my-atoi-0: [1234] => 1234 test-0 : [ OK ] atoi-1 : [-1234] => -1234 my-atoi-1: [-1234] => -1234 test-1 : [ OK ] atoi-2 : [1234a5678] => 1234 my-atoi-2: [1234a5678] => 1234 test-2 : [ OK ] atoi-3 : [-1234a5678] => -1234 my-atoi-3: [-1234a5678] => -1234 test-3 : [ OK ] atoi-4 : [ 1234] => 1234 my-atoi-4: [ 1234] => 1234 test-4 : [ OK ] atoi-5 : [ -1234] => -1234 my-atoi-5: [ -1234] => -1234 test-5 : [ OK ] atoi-6 : [ 1234a5678] => 1234 my-atoi-6: [ 1234a5678] => 1234 test-6 : [ OK ] atoi-7 : [ -1234a5678] => -1234 my-atoi-7: [ -1234a5678] => -1234 test-7 : [ OK ] atoi-8 : [abcd] => 0 my-atoi-8: [abcd] => 0 test-8 : [ OK ] atoi-9 : [] => 0 my-atoi-9: [] => 0 test-9 : [ OK ] atoi-10 : [01234] => 1234 my-atoi-10: [01234] => 1234 test-10 : [ OK ] atoi-11 : [-01234] => -1234 my-atoi-11: [-01234] => -1234 test-11 : [ OK ]

从运行结果上看,基本满足了题目要求,有心的读者可以进一步测试其他测试用例。

4 小小总结

aoti的源码实现,核心就是找出那些无效字符,并成功剔除它们,然后再想办法把字符串转换成10进制的整数,你都get到了吗?

审核编辑:汤梓红

猜你喜欢