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 ...
随机推荐
- SCALA常规练习C
package com.hengheng.scala abstract class Animal { def walk(speed : Int) def breathe() = { println(& ...
- 手工走一次OPENSTACK安装,掉一层皮啊
掉皮也是值得的,对OS的了解慢慢加深. 最近加入CS的Q群也学到不少.
- 各加密模式的演示(ECB,CBC)
对于较长的明文进行加密需要进行分块加密,但是直接加密(ecb)不容易隐藏模式,用OpenCV写了个程序论证了一下 ECB 优点就是简单,可以并行计算,不会迭代误差 缺点就是隐藏不了模式 CBC 需要初 ...
- 重新定义malloc和free 防止内存泄漏
1, 定义供应用程序使用的头文件//libmem.h#ifndef _LIBMEM_H_#define _LIBMEM_H_ //声明自定义malloc及free函数extern void *my_m ...
- redis 基本使用
Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...
- OpenGl学习总结
http://wenku.baidu.com/view/5305fe4f866fb84ae45c8dcd.html
- vmware压缩磁盘空间的方法
家里笔记本40G,可用空间怎么挤都只剩7G,从单位copy回来的linux虚拟机要10G,经检查实际使用空间5.7,也就是其他都是空余空间,可以释放掉.(只适合independent.Persiste ...
- JDK 高性能编程之容器
高性能编程在对不同场景下对于容器的选择有着非常苛刻的条件,这里记录下前人总结的经验,并对源码进行调试 JDK高性能编程之容器 读书笔记内容部分来源书籍深入理解JVM.互联网等 先放一个类图util,点 ...
- 修改mysql数据存储的地址
修改mysql数据存储的地址 修改步骤如下 1,修改前为默认路径/var/lib/mysql/,计划修改为/data/mysql/data mysql> show variables like ...
- 用js实现的刷新页面
一.先来看一个简单的例子: 下面以三个页面分别命名为frame.html.top.html.bottom.html为例来具体说明如何做. frame.html 由上(top.html)下(bottom ...