Walker19900515的专栏

对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;//declareextern 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 intint stringToInt(const string& );//convert string to doubledouble stringToDouble(const string& );//convert string to boolbool stringToBool(const string& );//print current time of this systembool printSystemTime(ostream& output=cout);#endif //FSTRING//源文件#include "fstring.h"#include <time.h>#include <math.h>//definitionbool 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;elsereturn 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;}

,愚者用肉体监视心灵,智者用心灵监视肉体

Walker19900515的专栏

相关文章:

你感兴趣的文章:

标签云: