微信扫描,分享到朋友圈和群
试写atoi函数
int atoi(const char* strNum);
思路:首先对输入指针判空,然后取出每一位的权值a,用类似下面的算法:Math.pow(10, i) * a 这样的算法实现。用到了乘方算法,似乎不是特别好。另外用一个类似递归的思想:sum = sum * 10 + digitnum; ,下面就后者给出实现代码。
在接口中用到了const char*,想了半天,到底是指针是const还是指向const char的指针呢,在The C++ Programming Language里面给出过一个助记的方法:把一个声明从右向左读。因此const char*是指后者。
atoi函数库文件编写:
[root@Shentar ~/myprogs/cpp/atoi]# cat atoi.cpp
#include <string.h>
#include <stdio.h>
#define MAX_NUM_LEN 10
int atoi(const char* strNum)
{
int sum = 0;
bool bnegative = false;
size_t len = 0;
if (NULL == strNum)
{
throw "argument error!";
}
if ('-' == *strNum)
{
bnegative = true;
strNum++;
}
else if ('+' == *strNum)
{
strNum++;
}
else
{
/*do nothing;*/
}
len = strnlen(strNum, MAX_NUM_LEN);
if (len <= 0)
{
throw "the input str is empty!\n";
}
unsigned char digitnum;
for (int i = 0; i != len; i++)
{
digitnum = *strNum - 0x30;
if (digitnum > 9 || digitnum < 0)
{
printf("char %c is not a digit.\n", *strNum);
if (i == 0)
{
throw "digit error!";
}
else
{
break;
}
}
sum = sum * 10 + digitnum;
strNum++;
}
if (bnegative)
{
sum = -sum;
}
return sum;
}
测试程序:
[root@Shentar ~/myprogs/cpp/atoi]# cat testatoi.cpp
#include <stdio.h>
int atoi(const char* str);
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("usage: testmain numberchar\n");
return -1;
}
try
{
printf("the input str [%s] was converted to int [%d].\n", argv[1], atoi(argv[1]));
}
catch(const char* errstr)
{
printf("%s\n", errstr);
return -1;
}
return 0;
}
Makefile
[root@Shentar ~/myprogs/cpp/atoi]# cat Makefile
lib:
g++ -o libatoi.so -shared atoi.cpp
test:lib
g++ -o testatoi testatoi.cpp -L. -latoi
all:test
clean:
rm *.o *.so *.out -rf
测试结果:
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi f
char f is not a digit.
digit error!
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi 1
the input str [1] was converted to int [1].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi -11
the input str [-11] was converted to int [-11].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi -21
the input str [-21] was converted to int [-21].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi -20981
the input str [-20981] was converted to int [-20981].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi 20981
the input str [20981] was converted to int [20981].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi a20981
char a is not a digit.
digit error!
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi 2a0981
char a is not a digit.
the input str [2a0981] was converted to int [2].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi 2ab0981
char a is not a digit.
the input str [2ab0981] was converted to int [2].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi 2ab-0981
char a is not a digit.
the input str [2ab-0981] was converted to int [2].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi -2ab-0981
char a is not a digit.
the input str [-2ab-0981] was converted to int [-2].
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi -ab-0981
char a is not a digit.
digit error!
[root@Shentar ~/myprogs/cpp/atoi]# ./testatoi
usage: testmain numberchar
1条评论
问题:
1、没有考虑整数溢出。
2、用宏来定义字符串长度是完全错误的思想。