字符串转整数程序优化。
来源:互联网 发布:潍坊行知学校宿舍图片 编辑:程序博客网 时间:2024/06/05 11:22
题目:字符串转整数
注意事项:
/*
1.考虑第一位有正负号
2.考虑字符串是否为空
3.考虑非数字字符
4.考虑变化出来的数大小,超过最大最小int怎么办?
5.每一位怎么变换成数字
*/
写了一个练习程序,请各位大牛帮我优化一下:
WIN7+VS
程序:
#define SINT_MIN (-32768)
#define SINT_MAX 32767
bool t_stoi(const char* src, int* des)
{
const char* src_t = NULL;
int ret = -1;
bool flag = false;
src_t = src;
*des = 0;
while(NULL == src)
{
return false;
}
/*if there is a sign?*/
if( '-' == *src_t || '+' == *src_t)
{
*src_t++;
flag = true;
}
while(NULL != src_t && ('0' <= *src_t) && ('9' >= *src_t))
{
/*small than 32767*/
if( ((*des)*10 + (*src_t-'0')) <= SINT_MAX)
{
*des = (*des)*10 + (*src_t-'0');
src_t++;
}
/* if it is minus, check if it is larger than -32768*/
else if('-' == *src)
{
if( ((*des)*10 + (*src_t-'0'))*ret >= SINT_MIN)
{
*des = (*des)*10 + (*src_t-'0');
src_t++;
}
else
{
*des =0;
printf("Overflow.\n");
return false;
}
}
else
{
*des =0;
printf("Overflow.\n");
return false;
}
}
if(flag)
{
if('-' == *src)
{
*des = (*des) * ret;
}
}
return true;
}
void main()
{
char * a = " 32767";
int b=0;
if(t_stoi(a,&b))
{
printf("b = %d\n",b);
}
printf("end of the main.");
}
- 字符串转整数程序优化。
- 字符串处理------字符串转整数
- 数字字符串转整数
- 整数转字符串
- 字符串转整数
- 字符串转整数
- 字符串转整数
- 字符串转整数
- 字符串转整数
- 20. 字符串转整数
- 字符串转整数
- 字符串转整数
- CSDN-字符串转整数
- 整数转字符串
- c字符串转整数
- 字符串转整数问题
- 字符串转整数(C#)
- c 整数转字符串
- C/C++中Sqlite使用简介
- Debug手机出现Android Unable to open sync connection!
- VS工程属性配置 /MDd /MLd /MTd
- 《Effective C++》之请记住
- java xml
- 字符串转整数程序优化。
- 跟踪手指在屏幕上的位置生成音频样本并播放
- 【转】Android设计模式的运用。。。
- .net 刷新页面,弹出“重试”框
- 备份控制文件
- 如何使用QFrame
- 使用之前确定初始化
- ant android自动编译生成签名对齐脚本
- centos基础上的精简和优化