问题:

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的更多相关文章

  1. 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 ...

  2. 【转载】解决 Subversion 的 “svn: Can't convert string from 'UTF-8' to native encoding” 错误

    转载自:http://blog.csdn.net/shaohui/article/details/3996274 在google code 上创建了一个新的项目, 用Windows 下面的tortoi ...

  3. 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 ...

  4. 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 ...

  5. How to convert string to wstring?

    How to convert string to wstring? - Codejie's C++ Space - C++博客     How to convert string to wstring ...

  6. svn: Can't convert string from 'UTF-8' to native

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt227 svn 版本库中有文件是以中文字符命名的,在 Linux 下 chec ...

  7. 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 ...

  8. 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 ...

  9. svn错误:Can't convert string from 'UTF-8' to native encoding

    如果文件名包含了中文,当执行"svn up ."遇到如下错误时: svn: Can't convert string from 'UTF-8' to native encoding ...

随机推荐

  1. 编写C# Windows服务,用于杀死Zsd.exe进程

    最近经常在我的xp系统进程中出现Zsd.exe进程.刚开始他占用内存不是很大.但是过了一段时间就会变成几百M 机器就会变得很卡,网上说Zsd可能是病毒.所以我就想要不写一个Windows服务,让他每隔 ...

  2. 从Profile中窥探Unity的内存管理

    刨根问底U3D---从Profile中窥探Unity的内存管理 这篇文章包含哪些内容 这篇文章从Unity的Profile组件入手,来探讨一下Unity在开发环境和正式环境中的内存使用发面的一些区别, ...

  3. C51 延时程序

    一.相关换算 1.1s=10^3ms(毫秒)=10^6μs(微秒)=10^9ns(纳秒)=10^12ps(皮秒)=10^15fs(飞秒)=10^18as(阿秒)=10^21zm(仄秒)=10^24ym ...

  4. -_-#【乱码】URL中文参数

    JavaScript利用URL向后台传入中文参数乱码问题解决之道! encodeURIComponent(encodeURIComponent('ya呀')) http://wap.baomihua. ...

  5. 【转】Unable to execute dex: Java heap space 解决方案(如何为eclipse.int 添加内存)

    原文网址:http://blog.csdn.net/zengyangtech/article/details/7003379 欢迎转载,转载请注明 http://blog.csdn.net/zengy ...

  6. 【转】掌握java枚举类型(enum type)

    原文网址:http://iaiai.iteye.com/blog/1843553 1   背景 在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量.之前我们通常利用 ...

  7. Delphi TdxBarmanager

    在 Form 上放一个 BarManager鼠标右键它,可以建立一个 MainToolBar,鼠标右键MainToolBar,建什么随便你选.只不过,最后设置一下 BarManager 的 Image ...

  8. 01背包之求第K优解——Bone Collector II

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...

  9. LinGo:装货问题——线性规划,整数规划,1988年美国数模B题

    7种规格的包装箱要装有两辆铁路平板车上去,包装箱的宽和高相同,但厚度(t,以cm计)和重量(以kg计)不同, 表A-1给出了每包装箱的厚度.重量和数量,每辆车有10.2m长的地方用来装包装箱(像面包片 ...

  10. yum nfs

    linux下3大文件共享方法 1.NFS NFS服务器配置 编辑/etc/exports,在文件中列出,要共享的目录.书写规则是:共享目录主机(参数).并且每条规则占据一行.例如: /mnt/mp3 ...