问题:

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. STM32f103------ADC(DMA)

    STM32F10x  ADC 技术指标: 分辨率: 12位分辨率  LSB=Vref+  / 2^(12) 转换时间: 采样一次至少14个ADC时钟周期 ,而ADC最高时钟周期为14MHz  选用采样 ...

  2. 不用预计算切向空间的Normal mapping

    先贴出shader 吧 等有时间了 来阐述原理 // vertex shader //varying vec3 ViewPosition; //varying vec3 Normal; varying ...

  3. 霍布森选择效应(Hobson choice Effect)

    1631年,英国剑桥商人霍布森从事马匹生意,他说,你们买我的马.租我的马,随你的便,价格都便宜.霍布森的马圈大大的.马匹多多的,然而马圈只有一个小门,高头大马出不去,能出来的都是瘦马.赖马.小马,来买 ...

  4. DSP开发资源总结,经典书籍,论坛

    OMAP4开发资源总结: 一.TI OMAP4官网介绍: http://www.ti.com.cn/general/cn/docs/wtbu/wtbuproductcontent.tsp?templa ...

  5. Spark RDD Union

    示例   Spark多个RDD(数据格式相同)“组合”为一个RDD   代码   from pyspark import SparkConf, SparkContext conf = SparkCon ...

  6. Minimum Depth of Binary Tree ——LeetCode

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. iOS面试贴士

    iOS面试小贴士 ———————————————回答好下面的足够了------------------------------------- 多线程.特别是NSOperation 和 GCD 的内部原 ...

  8. Asp.Net Mvc+Angular.Js自测小Demo

    参考:http://www.cnblogs.com/eedc/p/6082052.html 一.引用anguler: 1.angular.js 2.angular-route.js 3.app.js ...

  9. ganglia单播配置

    背景:    有时,由于当前网络不支持组播等种种原因,使用gmond默认的配置gmetad不能获取到各个客户端的全部数据,http://x.x.x.x/ganglia页面一个cluster组只能展示一 ...

  10. java JMS消息队列

    http://blog.csdn.net/shirdrn/article/details/6362792 http://haohaoxuexi.iteye.com/blog/1893038 http: ...