字符串转整数 C++编程
来源:互联网 发布:分层网络模型优点 编辑:程序博客网 时间:2024/06/07 09:05
//首先考虑空的字符串指针
//正负号
//非数字字符串
//考虑溢出
#include<string>
#include<iostream>
using namespace std;
int sting_to_int(const string &str)throw(bad_cast)
{
int value = 0;
int sign = -1;
if (str.empty())
{
cout << "str is empty"<<endl;
return 0;
}
int i = 0;
if (str[0] == '-')
{
if (str.length() > 1)
{
sign = -1;
i = 1;
}
else
{
cout << "no number"<<endl;
return 0;
}
}
if (str[0] == '+')
{
if (str.length() > 1)
{
sign = 1;
i = 1;
}
else
{
cout << "no number" <<endl;
return 0;
}
}
size_t length = str.length();
for (; i < length; i++)
{
if ('0' <= str[i] && str[i] <= '9')
{
value = value * 10 + str[i] - '0';
if (((sign==1)&& value > 0x7FFFFFFF)
|| ((sign==-1) && value < 0x80000000))
{
cout << "str is out of range" <<endl;
return 0;
}
}
else
{
cout << "str has nonnumeric character" << endl;
return 0;
}
}
return value*sign;
}
int main()
{
/*sting_to_int(NULL);
sting_to_int("");*/
/*cout << sting_to_int("123")<<endl;
cout << sting_to_int("+123")<<endl;
cout << sting_to_int("-123")<<endl;
sting_to_int("1a33");
sting_to_int("+0");
sting_to_int("-0");*/
//有效的最大正整数, 0x7FFFFFFF
cout << sting_to_int("+2147483647") << endl;
cout << sting_to_int("-2147483647") << endl;
cout << sting_to_int("+2147483648") << endl;
//有效的最小负整数, 0x80000000
cout << sting_to_int("-2147483648") << endl;
cout << sting_to_int("+2147483649") << endl;
cout << sting_to_int("-2147483649") << endl;
/*sting_to_int("+");
sting_to_int("-");*/
system("pause");
return 0;
}
//正负号
//非数字字符串
//考虑溢出
#include<string>
#include<iostream>
using namespace std;
int sting_to_int(const string &str)throw(bad_cast)
{
int value = 0;
int sign = -1;
if (str.empty())
{
cout << "str is empty"<<endl;
return 0;
}
int i = 0;
if (str[0] == '-')
{
if (str.length() > 1)
{
sign = -1;
i = 1;
}
else
{
cout << "no number"<<endl;
return 0;
}
}
if (str[0] == '+')
{
if (str.length() > 1)
{
sign = 1;
i = 1;
}
else
{
cout << "no number" <<endl;
return 0;
}
}
size_t length = str.length();
for (; i < length; i++)
{
if ('0' <= str[i] && str[i] <= '9')
{
value = value * 10 + str[i] - '0';
if (((sign==1)&& value > 0x7FFFFFFF)
|| ((sign==-1) && value < 0x80000000))
{
cout << "str is out of range" <<endl;
return 0;
}
}
else
{
cout << "str has nonnumeric character" << endl;
return 0;
}
}
return value*sign;
}
int main()
{
/*sting_to_int(NULL);
sting_to_int("");*/
/*cout << sting_to_int("123")<<endl;
cout << sting_to_int("+123")<<endl;
cout << sting_to_int("-123")<<endl;
sting_to_int("1a33");
sting_to_int("+0");
sting_to_int("-0");*/
//有效的最大正整数, 0x7FFFFFFF
cout << sting_to_int("+2147483647") << endl;
cout << sting_to_int("-2147483647") << endl;
cout << sting_to_int("+2147483648") << endl;
//有效的最小负整数, 0x80000000
cout << sting_to_int("-2147483648") << endl;
cout << sting_to_int("+2147483649") << endl;
cout << sting_to_int("-2147483649") << endl;
/*sting_to_int("+");
sting_to_int("-");*/
system("pause");
return 0;
}
阅读全文
0 0
- C/C++编程:整数转字符串输出
- c字符串转整数
- 字符串转整数(C#)
- c 整数转字符串
- 字符串转整数 C++编程
- 整数转Base64字符串C
- 编程算法 - 把字符串转换为整数 代码(C)
- C/C++整数与字符串互转
- C语言整数转字符串itoa函数
- C语言字符串转整数atoi函数
- c 字符串 整数转换
- C语言的字符串转整数 整数转字符串的函数
- C语言中字符串的逆序、字符串转整数、整数转字符串、自定义字符串操作(strcpy、strlen、strcat、strcmp)
- C 将整数变成字符串
- c语言字符串整数转换
- C语言整数转字符串(itoa()函数)小练习
- 《c程序设计语言》读书笔记-4.12-递归整数转字符串
- C语言字符串转int整数的实现源代码
- 说说css中pt、px、em、rem都扮演了什么角色
- Excel制作柱形图
- Js关于原型以及原型链的详细介绍
- JAVA环境+eclipse+tomcat+maven配置
- python第一感受
- 字符串转整数 C++编程
- applicationContext-dao.xml
- COUNT()函数详解
- 数据结构--初识图
- HBase体系结构剖析--下
- 卡尔曼滤波
- 过滤器通过HttpServletResponseWrapper包装HttpServletResponse实现获取response中的返回数据,以及对数据进行gzip压缩
- Android Studio查找快捷键总结
- C++之获取GPU(显卡)信息