Write a program to convert string to number without using library function。
来源:互联网 发布:mac开机密码忘了四种 编辑:程序博客网 时间:2024/04/29 19:00
1、问题
/*
Write a program to convert string to number without using library function。
*/
2、算法
#define MAX_LONG 0X7FFFFFFFlong foo(const char* str)
{
int sign = 1 ;
long num = 0 ;
const char* p = str ;
//假设输入的字符串是合法的
if ( *p == '-' )
{
sign = -1 ;
p++ ;
}else if ( *p == '+' )
{
sign = 1 ;
p++ ;
}
while(*p)
{
// long 取值范围-2147483648~ 2147483647
if ( (num > LONG_MAX/10) ||( (num == LONG_MAX/10) &&
( (-1 == sign && *p > '8') || (1 == sign && *p > '7')) ))
{
printf("out of bound") ;
return 0 ;
}
if ( *p >='0' && *p <='9' )
{
num = num*10 + (*p - '0') ;
}
*p++ ;
}
printf("%d", num * sign) ;
return num * sign ;
}
0 0
- Write a program to convert string to number without using library function。
- Convert a String to a Number
- SQL CONVERT FUNCTION CONVERT DATETIME TO STRING
- JavaScript:to write a function which increments a string, to create a new string
- Convert a Number to Hexadecimal
- Convert a Number to Hexadecimal
- Convert a Number to Hexadecimal
- Convert a Number to Hexadecimal
- Convert a Number to Hexadecimal
- Write a program to print the fibonacci series sum upto a particular no. using recursion.
- Write a program to pratice ATA command
- How to write a simple shell program
- convert a string to XmlElement
- vim E213: Cannot convert (add ! to write without conversion)
- Write a C program to print all permutations of a given string
- Write a program to print all permutations of a given string
- Write an Efficient C Program to Reverse Bits of a Number
- How to convert a String to DateTime
- Spring MVC学习笔记(一)———从注册开始
- Android仿IOS UIAlertView对话框
- The Berkeley algorithm
- unix/linux编程实践教程:服务器与socket
- 2014.09.03 周三-sql:count
- Write a program to convert string to number without using library function。
- C/C++ 堆内存分配 浅拷贝 深拷贝
- 第十二章 12.1.3节练习
- 如何引用另外一个文件中的串, 顺便说说void print();和(void)print();的区别
- 在自定义控件中读取XML配置属性的值
- vsftpd使用方法
- 100盏灯泡的开关问题
- 【linux kernel】虚拟内存分配
- 优秀开源项目