对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. RISC-V首度被我国列入扶持对象,上海已成RISC-V重要“据点”

    时间:2018年7月24日 16:33 摘要:近期,上海市经济信息委发布了<上海市经济信息化委关于开展2018年度第二批上海市软件和集成电路产业发展专项资金(集成电路和电子信息制造领域)项目申报 ...

  2. sass03 变量、样式导入

    demo1.scss @import "css.css"; //导入css文件 @import "http://ss/xx"; //导入css文件 @impor ...

  3. POJ 3672 水题......

    5分钟写完 水水更开心 //By SiriusRen #include <cstdio> #include <iostream> #include <algorithm& ...

  4. Android 自定义的开关按钮——SwitchButton

    本文转自:http://blog.csdn.net/swust_chenpeng/article/details/19967501 我将原文的控件进行了一些修改,去掉了原来控件的外边框,只留下重要的遮 ...

  5. 用fcntl锁一个文件来保护操作

    int testfd; /* fd for test*/ if((testfd = open("/usr/local/pgsql/bin/test_fd",O_RDWR|O_CRE ...

  6. sapui5 One or more constraints have not been satisfied.

    Getting error in creating a new project for UI5 One or more constraints have not been satisfied. slo ...

  7. Goldengate参数规范

    1.    文档综述 1.1.  文档说明 本文档规定了在实施Goldengate时,各个进程需要配置的参数. 该参数模板适合于Goldengate11.2.1.0版本: **注:本文档为Golden ...

  8. 学习Go语言之单例模式

    单例模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建.这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象 // 单例模式 package main ...

  9. 守护、互斥锁、IPC和生产者消费者模型

    守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic processes are ...

  10. whatis---查询一个命令执行什么功能

    whatis命令是用于查询一个命令执行什么功能,并将查询结果打印到终端上. whatis命令在用catman -w命令创建的数据库中查找command参数指定的命令.系统调用.库函数或特殊文件名.wh ...