关于
我的项目
相关阅读
热度排行
- [转] 宫崎骏用动漫教给我们的人生哲理,每一句都能说到心里! - (日期:[八月 24, 2013] 点击:[53,609])
- Google 网页爬虫报告无法连接站点解决办法 - (日期:[七月 20, 2014] 点击:[38,665])
- 架设Tiny Tiny RSS(TTRSS)阅读器,找回Google Reader! - (日期:[九月 27, 2013] 点击:[27,807])
- SkyDrive、DropBox和Google Drive三大公有云存储服务对比 - (日期:[六月 25, 2013] 点击:[25,673])
- 升级到至强E5440后,与i5 CPU笔记本性能对比 - (日期:[二月 18, 2014] 点击:[23,841])
- 公钥私钥加密解密数字证书数字签名详解 - (日期:[四月 19, 2014] 点击:[22,976])
- 本站建站技术合集 - (日期:[九月 20, 2013] 点击:[22,557])
- 使用OpenerDNS解决无法访问Google的问题 - (日期:[七月 5, 2014] 点击:[21,857])
- WordPress博客添加“返回顶部”按钮 - (日期:[七月 14, 2013] 点击:[21,278])
- Linux文件系统基础之inode和dentry - (日期:[三月 13, 2015] 点击:[20,221])
- 云存储中的HTTP鉴权算法分析 - (日期:[二月 7, 2014] 点击:[18,655])
- 存储基础知识之——磁盘阵列原理及操作实战 - (日期:[二月 9, 2014] 点击:[17,544])
- 精选37条强大的常用linux shell命令组合 - (日期:[九月 4, 2013] 点击:[17,469])
- DNS原理、架构和配置详解 - (日期:[九月 6, 2013] 点击:[16,876])
- Netty和Jetty的Java NIO 网络框架模型分析 - (日期:[七月 13, 2013] 点击:[16,351])
- CoreOS 初识之安装 - (日期:[十一月 16, 2014] 点击:[16,221])
- Windows与Linux文件系统互访的几种方法 - (日期:[八月 21, 2014] 点击:[15,739])
- Dijkstra算法求解最短路径分析 - (日期:[七月 12, 2014] 点击:[14,942])
- NAS解决方案实现多媒体文件共享播放 - (日期:[十二月 21, 2014] 点击:[13,967])
- 简介 - (日期:[九月 1, 2012] 点击:[13,791])
- 如何编程实现 2 + 2 = 5? - (日期:[六月 2, 2014] 点击:[13,278])
- 搭建了一个iNews程序 - (日期:[十月 15, 2013] 点击:[13,252])
- 2014年9月曝出的Bash ShellShock漏洞简析 - (日期:[九月 26, 2014] 点击:[13,170])
- 彻底解决WordPress博客垃圾评论的问题 - (日期:[八月 5, 2013] 点击:[13,165])
- 如何使用1M的内存排序100万个8位数 - (日期:[三月 27, 2014] 点击:[12,570])
- 全部日志列表 - (日期:[十一月 11, 2012] 点击:[12,428])
- 关于回调函数和this指针探讨 - (日期:[八月 24, 2014] 点击:[12,246])
- 开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南 - (日期:[四月 23, 2022] 点击:[11,867])
- 给定一个long型常量,其值为x,给定long型变量a,要求a & x 的取值集合 - (日期:[九月 8, 2012] 点击:[11,742])
- WordPress建站必备实用插件 - (日期:[八月 7, 2014] 点击:[11,389])
-
近期文章
- Golang PGO技术介绍 四月 13, 2025
- 3FS Usrbio 简介 四月 6, 2025
- NVIDIA 为 CUDA 添加原生 Python 支持:开启 GPU 计算新篇章 四月 5, 2025
- pico.sh服务简介 四月 4, 2025
- cloudflare的妙用 一月 11, 2025
- 介绍一个生产力工具:ntfy 十二月 24, 2024
- 困扰了快1个月的家用宽带网络卡顿问题-Linux病毒实战手记 四月 21, 2024
- HomeServer 2024升级计划 二月 11, 2024
- HomeServer直播、监控方案实践 九月 28, 2023
- 我的2022 一月 7, 2023
近期评论
- 匿名发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- 1发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- xxgl发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- 童燕群发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- xxgl发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- 童燕群发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- xxgl发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- 童燕群发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- xxgl发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
- xxgl发表在《开源好用的电子书管理服务Talebook(Calibre网络版)安装使用指南》
分类目录
文章归档
- 2025年四月 (4)
- 2025年一月 (1)
- 2024年十二月 (1)
- 2024年四月 (1)
- 2024年二月 (1)
- 2023年九月 (1)
- 2023年一月 (1)
- 2022年十月 (1)
- 2022年八月 (2)
- 2022年四月 (1)
- 2022年三月 (1)
- 2021年十二月 (2)
- 2021年十月 (2)
- 2021年九月 (1)
- 2021年八月 (1)
- 2021年五月 (1)
- 2021年三月 (2)
- 2021年一月 (2)
- 2020年十二月 (5)
- 2020年十一月 (2)
- 2020年十月 (2)
- 2020年九月 (1)
- 2020年八月 (5)
- 2020年七月 (2)
- 2019年九月 (1)
- 2018年八月 (1)
- 2018年七月 (1)
- 2018年六月 (1)
- 2018年五月 (1)
- 2018年三月 (1)
- 2018年二月 (1)
- 2018年一月 (2)
- 2017年十二月 (3)
- 2017年十月 (4)
- 2017年九月 (1)
- 2017年七月 (1)
- 2017年六月 (1)
- 2016年十二月 (1)
- 2016年十月 (1)
- 2016年九月 (1)
- 2016年七月 (2)
- 2016年六月 (1)
- 2016年二月 (3)
- 2015年十二月 (3)
- 2015年十一月 (2)
- 2015年十月 (1)
- 2015年八月 (2)
- 2015年七月 (4)
- 2015年六月 (1)
- 2015年三月 (2)
- 2015年二月 (1)
- 2015年一月 (4)
- 2014年十二月 (2)
- 2014年十一月 (2)
- 2014年十月 (5)
- 2014年九月 (8)
- 2014年八月 (11)
- 2014年七月 (17)
- 2014年六月 (7)
- 2014年五月 (15)
- 2014年四月 (16)
- 2014年三月 (14)
- 2014年二月 (5)
- 2013年十二月 (5)
- 2013年十一月 (3)
- 2013年十月 (13)
- 2013年九月 (13)
- 2013年八月 (13)
- 2013年七月 (9)
- 2013年六月 (8)
- 2013年五月 (1)
- 2013年三月 (3)
- 2013年一月 (1)
- 2012年十一月 (1)
- 2012年九月 (12)
- 2012年八月 (3)
- 2011年二月 (1)
- 2009年三月 (1)
- 2009年二月 (1)
- 2008年十一月 (1)
- 2008年六月 (1)
- 2008年四月 (1)
- 2008年三月 (1)
试写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、用宏来定义字符串长度是完全错误的思想。