Convert String to Long
问题:
Given a string, write a routine that converts the string to a long, without using the built in functions that would do this. Describe what (if any) limitations the code has.
代码:
/*
* Author: Min Li
* Discussion:
* 1. Return 0 when input is not valid (Empty or wrong format)
* 2. Return LONG_MAX when input string overflows
* 3. Return LONG_MIN when input string underflows
* 4. Input String is allowed to have additional character after a valid substring that can form a long number. (e.g. +123+)
* 5. Input can have many whitespaces before the first non-whitespace character. (e.g. " 123")
*
*/ // Class: Solution class Solution {
public:
// Method: Convert String to Long
long stringToLong(string s) {
// Special Case: Empty
if(s.size()==)
return ;
long sign; // Record the sign of the long number
int index=;
int strLen = s.size(); // The length of input
long result=; // The final result
// Discard the whitespaces before the first non-whitespace.
while(index<strLen && s[index]==' ') index++; // The input only contains whitespaces
if(index==strLen)
return ; // Determine the sign
if(s[index]=='-') { // Input starts with "-"
sign = -;
++index;
}
else if(s[index]=='+') { // Input starts with "+"
sign = ;
++index;
}
else if(s[index] < '' || s[index] > '') // Invalid input
return ;
else // Unsigned input
sign = ; // Retrieve the digit after first non-whitespace character
while(index<strLen) {
if(s[index]>='' && s[index]<='') { // New character is 0-9
int digit = s[index]-'';
if(result>LONG_MAX/ || (result==LONG_MAX/ && digit>LONG_MAX%)) { // Overflow or underflow
result = sign==-?LONG_MIN:LONG_MAX;
}
else
result = result*+digit;
}
else // New character is not 0-9
break;
index++;
} return sign*result; } // Method: Test
void test() {
string testString;
// testString = ""; // Test Case 1: Empty
testString = ""; // Test Case 2: Valid unsigned input
// testString = "+123"; // Test Case 3: Valid signed input
// testString = " 123"; // Test Case : Valid input with whitespaces
// testString = "abc123"; // Test Case : Invalid input
// testString = "++123"; // Test Case 4: Invalid signed input
// testString = "+123+"; // Test Case 5: Valid input format with additional characters
// testString = "3924x8"; // Test Case 6: Invalid input format
// testString = "1000000000000000000000"; // Test Case 7: Overflow
// testString = "-10000000000000000000"; // Test Case 8: Underflow
cout << stringToLong(testString) << endl;
}
};
Convert String to Long的更多相关文章
- svn: Can't convert string from 'UTF-8' to native encoding 的解决办法(转)
http://www.cnblogs.com/xuxm2007/archive/2010/10/26/1861223.html svn 版本库中有文件是以中文字符命名的,在 Linux 下 check ...
- 【转载】解决 Subversion 的 “svn: Can't convert string from 'UTF-8' to native encoding” 错误
转载自:http://blog.csdn.net/shaohui/article/details/3996274 在google code 上创建了一个新的项目, 用Windows 下面的tortoi ...
- svn: Can't convert string from 'UTF-8' to native encoding 的解决办法
http://www.leakon.com/archives/610 http://www.toplee.com/blog/566.html http://svnbook.red-bean.com/e ...
- SVN遇到Can't convert string from 'UTF-8' to native encoding
刚配好mysql,svn co代码的时候遇到问题 svn: Can't convert string from 'UTF-8' to native encoding: svn: platform/co ...
- How to convert string to wstring?
How to convert string to wstring? - Codejie's C++ Space - C++博客 How to convert string to wstring ...
- svn: Can't convert string from 'UTF-8' to native
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt227 svn 版本库中有文件是以中文字符命名的,在 Linux 下 chec ...
- How to convert String to Date – Java
In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners ar ...
- Java – How to convert String to Char Array
Java – How to convert String to Char ArrayIn Java, you can use String.toCharArray() to convert a Str ...
- svn错误:Can't convert string from 'UTF-8' to native encoding
如果文件名包含了中文,当执行"svn up ."遇到如下错误时: svn: Can't convert string from 'UTF-8' to native encoding ...
随机推荐
- poj 2513Colored Sticks
http://poj.org/problem?id=2513 #include<cstdio> #include<cstdlib> #include<cstring> ...
- linux crontab 计划任务 atd和windows下的计划任务
crontab 命令 如果发现您的系统里没有这个命令,请安装下面两个软件包. vixie-cron crontabs crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类 ...
- Putty工具包简单使用
Putty工具包简单使用 一.Putty简介 Putty是一款远程登录工具,用它可以非常方便的登录到Linux服务器上进行各种操作(命令行方式).Putty完全免费,而且无需安装(双击即可运行),支持 ...
- REST服务中的日志可视化(关键技术实现)
引言 在系统构建完成之后,我们通常会使用REST API对外提供服务,在REST API的处理过程中经常会出现一些异想不到的问题(用户权限不足.参数不全.数据库访问异常等),导致请求失败,很多时候用户 ...
- 安装loadrunner11 ,出现如下错误如何解决?
出现的问题是: 安装LoadRunner 11时弹窗提示"Micosoft Visual C++ 2005 SP1 可再发行组件包(X86):'命令行选项语法错误.键入命令 / ? 可获得帮 ...
- 进程间通讯aidl
进程间通讯(aidl) 1.首先定义一个接口 2.把这个接口的文件扩展名改为xxx.aidl 3.写一个MyService类继承自Service类重新里面的方法, 4.在MyService类定义一个内 ...
- [Java] 类的初始化步骤
前言 类的初始化过程,可在多线程环境下进行的,为了简化,本文介绍的单线程情况下的类初始化步骤. 此外,继承情况下的顺序不是本文焦点,本文重点在于一个类的内部字段的初始化顺序.想了解继承情况下的初始化情 ...
- FSharp.Data 程序集之 Http
FSharp.Data 程序集之 Http (** # F# Data: HTTP Utilities .NET 库提供了强大的 API,产生和发送 HTTP WEB 请求,有两个类型,一个简单,`W ...
- icinga 被动模式 nsca 安装
本文假设读者已安装好icinga,此外nsca本身nagios插件,icinga/nagios都适用 一.编译安装nsca1.编译,拷贝文件tar -vxzf nsca-2.7.2.tar.gz./c ...
- 真正菜鸟用教程之WQSG Scrip Export WQSG (脚本导出导入工具,PSP、NDS汉化必备 )
先扫盲WQSG是干什么用的 一些掌机类游戏汉化比方PSP NDS 汉化必备之物 它能够依据字典转换文本 假设你不知道这是啥玩意,快去充电染成茜色的坂道 文本提取(导出)方法 (下文称导出文章) 在导出 ...