网易面试题:写一段程序,实现atoi(const char* s)方法
2012-04-11 18:58
267 查看
描述:
写一段程序,实现atoi(const char* s)方法。
示例代码:
写一段程序,实现atoi(const char* s)方法。
示例代码:
#include <iostream> using namespace std; // 写一段程序,实现atoi(const char* s)方法。 // atoi用于将字符串转换成为整数。 // 比如 “123” => 123, “-246” => -246。 int Aatoi(const char *s) { // 主要的问题是负号的处理,不考虑其他字符的处理 int len = strlen(s); if(0 == len) return -1; bool is_plus = true; if(s[0] != '-') { is_plus = true; } else { is_plus = false; s++; } int result = 0; while(*s!='\0') { result = result*10 + *s - '0'; s++; } if(is_plus == false) return -result; else return result; } void main() { char s[] = "123"; cout << Aatoi(s) << endl; }
相关文章推荐
- 写一段程序,实现atoi(const char* s)方法
- 库函数的实现atoi (const char *str)
- C程序:cont char *p 和 char* const p 区别及记忆方法
- 编程实现函数 int my_atoi(const char *str)
- 不调用库函数,实现字符串复制函数char * strcpy(char * dst, const char * src) C/C++软件开发求职面试题 必备考点(二)
- Python通过解析网页实现看报程序的方法
- 【C语言】【面试题】使用main函数的参数,实现一个整数计算机,程序可以接受三个参数
- C#实现软件监控外部程序运行状态的方法
- java设置一段代码执行的超时时间的简单方法 间接实现获取Connection超时问题
- PHP面试题:50,40,30,20,1这五张优惠卷,订单金额为X元,用方法实现return多少张优惠卷
- C++抽奖程序实现方法
- error C2664: 'atoi' : cannot convert parameter 1 from 'CString' to 'const char *'
- Winform程序多语言国际化实现的简单方法
- 实现自定义对话框程序快捷键的两种方法
- webview实现支付后的业务逻辑处理,小程序webview完成微信支付后的处理方法——微信web-view高级用法12
- c#实现为程序绑定电脑简单实现方法
- 微信小程序之滚动视图容器的实现方法
- iphone程序中实现截屏的一种方法
- MFC单文档(SDI)全屏程序实现方法
- 网易面试题 对String和java调用方法的考察