对string作了一些扩展,包含string转化为int、string转化为double、string转化为bool、打印系统当前时间。但没有解决数据溢出的问题,请大神帮忙解决!

//头文件
/*part of interface about string;
*it follow the function simply ,no complex situation exist;
*so it should be modify before you use it;
*overflow problem is need to be solved;
*/
#ifndef FSTRING
#define FSTRING #include <string>
#include <vector>
#include <iostream> using namespace std; //declare
extern bool stringToBoolValid;
extern bool stringToIntValid;
extern bool stringToDoubleValid; //split the string with delim,default delim is ' '
bool split(vector<string>& ,const string str,const char delim=' ');
//convert string to int
int stringToInt(const string& );
//convert string to double
double stringToDouble(const string& );
//convert string to bool
bool stringToBool(const string& );
//print current time of this system
bool printSystemTime(ostream& output=cout);
//delete space
void deleteSpace(string& str);
#endif //FSTRING
//源文件
#include "fstring.h"
#include <time.h>
#include <math.h> //definition
bool stringToBoolValid=true;
bool stringToIntValid=true;
bool stringToDoubleValid=true; //切割字符串函数
bool split(vector<string>& vecStr,const string str,const char delim)
{//假设所须要要转化的字符串为空,则直接返回
if(str.empty())
return false;
size_t i=0;
string subStr;
subStr.clear();
while(i<str.size())
{
if(str[i]==delim)
{
if(!subStr.empty())
vecStr.push_back(subStr);
subStr.clear();
}
else
{
subStr+=str[i];
}
++i;
}
vecStr.push_back(subStr);
return true;
} int stringToInt(const string& str)//转化成整数
{
if(str.empty())
{
stringToIntValid=false;
return 0;
}
int returnInt(0);
bool flagPlusMinus=0;
size_t i=0;
if(str[0]=='+')
{
i++;
}
else if(str[0]=='-')
{
i++;
flagPlusMinus=1;
}
for(;i<str.size();++i)
{
if(str[i]<'0' || str[i]>'9')
{
stringToIntValid=false;
returnInt=0;
return returnInt;
}
returnInt=returnInt*10+str[i]-'0';
}//假设仅仅有一个正号或负号,输出也是零 if(flagPlusMinus)
returnInt*=(-1);
return returnInt;
} double stringToDouble(const string& str)//转化成浮点数
{
if(str.empty())
{
stringToDoubleValid=false;
return 0;
}
double returnDouble(0);
size_t i=0;
size_t flag=2000;
int NumPoint=0;//小数点出现次数
int decimalNum(0);
bool flagPlusMinus=0;
if(str[0]=='+')
{
i++;
}
else if(str[0]=='-')
{
i++;
flagPlusMinus=1;
}
for(;i<str.size();++i)
{
if(str[i]=='.')
{
if(NumPoint>1)
{
stringToDoubleValid=true;
returnDouble=0;
return returnDouble;
}
flag=i;
continue;
}
else if(str[i]<'0' || str[i]>'9')
{
stringToDoubleValid=true;
returnDouble=0;
return returnDouble;
} if(i>flag)
{
decimalNum++;
}
returnDouble=returnDouble*10+str[i]-'0';
}
for(int t=0;t<decimalNum;++t)
returnDouble/=10; if(flagPlusMinus)
returnDouble*=(-1);
return returnDouble;
} bool stringToBool(const string& str)//String to Bool
{
if(str.size()>1 || str.empty())
{
stringToBoolValid=false;
return 0;
}
if(str=="1")
return 1;
else
return 0;
} bool printSystemTime(ostream& output)
{
time_t currentTime=time(0);
struct tm* currentTimeStruct=localtime(¤tTime);
output<<"系统当前时间:"<<1900+currentTimeStruct->tm_year<<"."<<
currentTimeStruct->tm_mon+1<<"."<<currentTimeStruct->tm_mday<<" "<<
currentTimeStruct->tm_hour<<":"<<currentTimeStruct->tm_min<<":"<<
currentTimeStruct->tm_sec<<endl;
return true;
}

void deleteSpace(string& str)
{//删除表达式中的空格
<span style="white-space:pre"> </span>string::iterator iter = str.begin();
<span style="white-space:pre"> </span>while (iter != str.end())
<span style="white-space:pre"> </span>{//注意删除后,迭代器指向被删除元素的下一个元素
<span style="white-space:pre"> </span>if (*iter == ' ')
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>iter = str.erase(iter);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>iter++;
<span style="white-space:pre"> </span>}
}

对string的一些扩展函数的更多相关文章

  1. 从零开始学Kotlin-扩展函数(10)

    从零开始学Kotlin基础篇系列文章 什么是扩展函数 扩展函数数是指在一个类上增加一种新的行为,我们甚至没有这个类代码的访问权限: Kotlin 可以对一个类的属性和方法进行扩展,且不需要继承或使用 ...

  2. 逆波兰法求解数学表达示(C++)

    主要是栈的应用,里面有两个函数deleteSpace(),stringToDouble()在我还有一篇博客其中:对string的一些扩展函数. 本程序仅仅是主要的功能实现,没有差错控制. #inclu ...

  3. golang struct扩展函数参数命名警告

    今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...

  4. 前端学PHP之mysql扩展函数

    × 目录 [1]连接数据库 [2]使用数据库 [3]执行SQL查询[4]操作结果集[5]关闭连接 前面的话 mysql由于其体积小.速度快.总体拥有成本低,尤其是具有开放源码这一特点,许多中小型网站为 ...

  5. 用Kotlin开发Android应用(III):扩展函数和默认值

    这是关于Kotlin的第三篇. 原文标题:Kotlin for Android (III): Extension functions and default values 原文链接:http://an ...

  6. C#ASP.NET 通用扩展函数之 LogicSugar 简单好用

    说明一下性能方面 还可以接受 循环1000次普通Switch是用了0.001秒 ,扩展函数为0.002秒  , 如果是大项目在有负载均衡的情况下完全可以无视掉,小项目也不会计较这点性能了. 注意需要引 ...

  7. C#ASP.NET 通用扩展函数之 IsWhat 简单好用

    好东西都需要人去整理.分类 注意:需要引用命名空间 SyntacticSugar 用法: /***扩展函数名细***/ //[IsInRange] int num = 100; //以前写法 if ( ...

  8. Swift新手教程3-字符串String

    原创blog,转载请注明出处 String 在swfit中,String兼容Unicode的方式.用法和C语言类似. 注意   在Cocoa和Cocoa touch中,Swift的String,和Fo ...

  9. Kotlin的扩展函数:扩展Android框架(KAD 08)

    作者:Antonio Leiva 时间:Jan 11, 2017 原文链接:https://antonioleiva.com/extension-functions-kotlin/ 扩展函数是Kotl ...

随机推荐

  1. [BZOJ4289] [PA2012] Tax 解题报告 (最短路+差分建图)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 4289: PA2012 Tax Time Limit: 10 Sec  Memo ...

  2. [JZOJ4272] [NOIP2015模拟10.28B组] 序章-弗兰德的秘密 解题报告(树形DP)

    Description 背景介绍弗兰德,我不知道这个地方对我意味着什么.这里是一切开始的地方.3年前,还是个什么都没见过的少年,来到弗兰德的树下,走进了封闭的密室,扭动的封尘已久机关,在石板上知道了这 ...

  3. es6 --- 功能

    标记的模板文字 1.模板文字!确实很棒.我们不再会这样做…. const concatenatedString = "I am the " + number + "per ...

  4. Pycharm在创建py文件时,如何自动添加默认文件头注释?

    PyCharm是一款很好用的编写Python工程的IDE,用PyCharm创建一个Python文件或者向工程添加一个.py文件时,为了更好的使所编写的代码在各操作环境更好的运行,我们往往需要在.py文 ...

  5. 再谈Ubuntu和CentOS安装好之后的联网问题(桥接和NAT、静态和动态ip)(博主推荐)

    不多说,直接上干货! 首先,普及概念. hostonly.桥接和NAT的联网方式 对于CentOS系统,用的最多的就是,NAT和桥接模式 CentOS 6.5静态IP的设置(NAT和桥接联网方式都适用 ...

  6. leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  7. SFTP的使用

    SFTP的使用: 1.项目中需要引入jar包,下载地址:https://sourceforge.net/projects/jsch/files/jsch.jar/ 2.需要下载SFTP服务器,下载地址 ...

  8. 双列集合Map的嵌套遍历

    双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...

  9. objc_clear_deallocating 与弱引用

    void *objc_destructInstance(id obj){ if (obj) { Class isa_gen = _object_getClass(obj); class_t *isa ...

  10. caffe(5) 其他常用层及参数

    本文讲解一些其它的常用层,包括:softmax_loss层,Inner Product层,accuracy层,reshape层和dropout层及其它们的参数配置. 1.softmax-loss so ...