//本来想模拟java实现String类的 后来发现确实比较多
//故 选取一部分进行实现(比较简单)
SString.h 头文件
#pragma once
class SString
{
private:
char *str;
int len;
public:
SString();
SString(const SString& src);
SString(char* const pStr);
~SString()
{
if(str)
delete []str;
}
int getLength() const{return len;}; // 获取到字符串的大小
void clear(){str[0]='\0';len=0;}; //进行清除
//故 选取一部分进行实现(比较简单)
SString.h 头文件
#pragma once
class SString
{
private:
char *str;
int len;
public:
SString();
SString(const SString& src);
SString(char* const pStr);
~SString()
{
if(str)
delete []str;
}
int getLength() const{return len;}; // 获取到字符串的大小
void clear(){str[0]='\0';len=0;}; //进行清除
//首先进行一些运算符的重载 大于 小于 等于等等
char &operator[] (int i); //重载[]运算符 能够获取到它的某一特定位置的
SString &operator= (const SString& refstr); //进行数据的复制
int operator> (const SString &refstr);
int operator== (const SString &refstr);
int operator!= (const SString &refstr);
int operator< (const SString &refstr);
SString operator+ (const SString &refstr);
SString& operator+= (const SString &refstr);
int _strcmp( char *srcstr,const char *refstr);
void _strcpy( char *srcstr,const char *refstr);
void _strcat( char *srcstr,const char *refstr);
char &operator[] (int i); //重载[]运算符 能够获取到它的某一特定位置的
SString &operator= (const SString& refstr); //进行数据的复制
int operator> (const SString &refstr);
int operator== (const SString &refstr);
int operator!= (const SString &refstr);
int operator< (const SString &refstr);
SString operator+ (const SString &refstr);
SString& operator+= (const SString &refstr);
int _strcmp( char *srcstr,const char *refstr);
void _strcpy( char *srcstr,const char *refstr);
void _strcat( char *srcstr,const char *refstr);
//其他功能的实现
//1 charAt(int i) //查找i处的字符数据
char charAt(int i);
bool isEmpty()
{
return (len==0);
}
//1 charAt(int i) //查找i处的字符数据
char charAt(int i);
bool isEmpty()
{
return (len==0);
}
int indexOf(char ch); //返回第一次出现的索引
int find(const SString &srcstr, int startPos); //下次实现KMP算法在使用吧 呵呵
SString subString(int pos, int size); //求子字符串
SString& insertString(const SString &srcstr, int pos); //在pos位置插入字符串
SString &deleteString(int pos, int length); //删除特定位置的字符串
int find(const SString &srcstr, int startPos); //下次实现KMP算法在使用吧 呵呵
SString subString(int pos, int size); //求子字符串
SString& insertString(const SString &srcstr, int pos); //在pos位置插入字符串
SString &deleteString(int pos, int length); //删除特定位置的字符串
//下面利用流写一些字符串和数字转换吧
double stringToDouble();
int stringToInt();
float stringToFloat();
};
SString.cpp
#include "stdafx.h"
#include "SString.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
double stringToDouble();
int stringToInt();
float stringToFloat();
};
SString.cpp
#include "stdafx.h"
#include "SString.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
SString::SString()
{
str = new char[1];
str[0] = '\0';
len = 0;
}
{
str = new char[1];
str[0] = '\0';
len = 0;
}
SString::SString(const SString& refstr)
{
len = refstr.len;
str = new char[len+1];
_strcpy(str,refstr.str);
}
{
len = refstr.len;
str = new char[len+1];
_strcpy(str,refstr.str);
}
SString::SString(char* const pStr)
{
int size=0;
char *q=pStr;
while(*q++ != '\0') size++;
len = size;
str = new char[len+1];
_strcpy(str,pStr);
}
{
int size=0;
char *q=pStr;
while(*q++ != '\0') size++;
len = size;
str = new char[len+1];
_strcpy(str,pStr);
}
char &SString::operator[] (int i) //重载[]运算符 能够获取到它的某一特定位置的
{
if(i<0 || i>len)
throw out_of_range("越界错误!");
return str[i];
}
{
if(i<0 || i>len)
throw out_of_range("越界错误!");
return str[i];
}
SString &SString::operator= (const SString& refstr) //进行数据的复制
{
int size = refstr.len;
if(this != &refstr)
{
if(len < size)
{
delete []str;
str = new char[size+1];
len = size;
}
_strcpy(str,refstr.str);
}
return *this;
}
{
int size = refstr.len;
if(this != &refstr)
{
if(len < size)
{
delete []str;
str = new char[size+1];
len = size;
}
_strcpy(str,refstr.str);
}
return *this;
}
int SString::operator> (const SString &refstr)
{
return (strcmp(str,refstr.str) > 0);
}
{
return (strcmp(str,refstr.str) > 0);
}
int SString::operator== (const SString &refstr)
{
return (strcmp(str,refstr.str));
}
{
return (strcmp(str,refstr.str));
}
int SString::operator!= (const SString &refstr)
{
return (strcmp(str,refstr.str));
}
{
return (strcmp(str,refstr.str));
}
int SString::operator< (const SString &refstr)
{
return (strcmp(str,refstr.str));
}
{
return (strcmp(str,refstr.str));
}
SString& SString::operator+= (const SString &refstr)
{
int newLen = len + refstr.len;
char *newStr = new char[newLen+1];
_strcpy(newStr,str);
_strcat(newStr,refstr.str);
str = newStr;
len = newLen;
return *this;
}
{
int newLen = len + refstr.len;
char *newStr = new char[newLen+1];
_strcpy(newStr,str);
_strcat(newStr,refstr.str);
str = newStr;
len = newLen;
return *this;
}
SString SString::operator+ (const SString &refstr)
{
SString newStr(*this);
newStr += refstr.str;
return newStr;
}
{
SString newStr(*this);
newStr += refstr.str;
return newStr;
}
int SString::_strcmp( char *srcstr,const char *refstr)
{
// 首先利用数组实现其功能
int i;
for(i=0; srcstr[i]==refstr[i]; i++)
if(srcstr[i] == '\0')
return 0;
return (srcstr[i] - refstr[i]);
//for(; *srcstr==*refstr; *srcstr++,*refstr++) //不一定能够实现 作为写法放着
// if(*srcstr == '\0')
// return 0;
//return (*srcstr-*refstr);
}
{
// 首先利用数组实现其功能
int i;
for(i=0; srcstr[i]==refstr[i]; i++)
if(srcstr[i] == '\0')
return 0;
return (srcstr[i] - refstr[i]);
//for(; *srcstr==*refstr; *srcstr++,*refstr++) //不一定能够实现 作为写法放着
// if(*srcstr == '\0')
// return 0;
//return (*srcstr-*refstr);
}
void SString::_strcpy( char *srcstr,const char *refstr)
{
//首先利用数组实现
cout<<refstr<<endl;
for(int i=0; i<len; i++)
srcstr[i] = refstr[i];
srcstr[len] = '\0';
/*while(*srcstr != '\0') //不一定能够成功 作为写法放着
*srcstr++ = *refstr++;
*srcstr = '\0';*/
}
{
//首先利用数组实现
cout<<refstr<<endl;
for(int i=0; i<len; i++)
srcstr[i] = refstr[i];
srcstr[len] = '\0';
/*while(*srcstr != '\0') //不一定能够成功 作为写法放着
*srcstr++ = *refstr++;
*srcstr = '\0';*/
}
void SString::_strcat( char *srcstr,const char *refstr)
{
//首先利用数组实现
int i=0,j=0;
while(srcstr[i++]!='\0'); //首先找到位置
while(srcstr[i++]=refstr[j++]!='0');
//while(*srcstr++ != '\0') ;
//while(*srcstr++ = *refstr++ != '\0') ;
}
{
//首先利用数组实现
int i=0,j=0;
while(srcstr[i++]!='\0'); //首先找到位置
while(srcstr[i++]=refstr[j++]!='0');
//while(*srcstr++ != '\0') ;
//while(*srcstr++ = *refstr++ != '\0') ;
}
char SString::charAt(int i)
{
if(i<0 || i>len)
throw out_of_range("数组越界!");
return str[i];
}
{
if(i<0 || i>len)
throw out_of_range("数组越界!");
return str[i];
}
SString SString::subString(int pos, int size)
{
SString temp;
int i,j;
if(pos <0 || size<0)
throw out_of_range("数组越界!");
if((pos+size-1) > len-1) size = len-pos; //截取字符串长度最多到len-1
temp.str = new char[size+1];
temp.len = size; //注意函数返回的时候释放内存
for(i=0,j=pos; i<size; i++,j++)
temp.str[i] = str[j];
temp.str[size] = '\0';
return temp;
}
{
SString temp;
int i,j;
if(pos <0 || size<0)
throw out_of_range("数组越界!");
if((pos+size-1) > len-1) size = len-pos; //截取字符串长度最多到len-1
temp.str = new char[size+1];
temp.len = size; //注意函数返回的时候释放内存
for(i=0,j=pos; i<size; i++,j++)
temp.str[i] = str[j];
temp.str[size] = '\0';
return temp;
}
SString& SString::insertString(const SString &srcstr, int pos)
{
int size,j,i;
if(pos <0 || pos>len)
throw out_of_range("数组越界!");
size = len + srcstr.len;
char *newStr = new char[size+1];
{
for(i=0; i<pos;i++) newStr[i] = str[i]; //先复制str的一部分
for(j=pos,i=0; i<srcstr.len; j++,i++)
newStr[j] = srcstr.str[i]; //复制srcstr的全部
for(j=pos+srcstr.len,i=pos; i<len; i++,j++)
newStr[j] = str[i];
newStr[j] = '\0';
delete []str;
str = newStr;
len = size;
}
return *this;
}
{
int size,j,i;
if(pos <0 || pos>len)
throw out_of_range("数组越界!");
size = len + srcstr.len;
char *newStr = new char[size+1];
{
for(i=0; i<pos;i++) newStr[i] = str[i]; //先复制str的一部分
for(j=pos,i=0; i<srcstr.len; j++,i++)
newStr[j] = srcstr.str[i]; //复制srcstr的全部
for(j=pos+srcstr.len,i=pos; i<len; i++,j++)
newStr[j] = str[i];
newStr[j] = '\0';
delete []str;
str = newStr;
len = size;
}
return *this;
}
SString &SString::deleteString(int pos, int length)
{
int i,size = length;
if(pos<0 || pos>len)
throw out_of_range("数组越界!");
if(length != 0)
{
if(pos+length-1 > len-1)
size = len-pos;
for(i=pos; i+size<=len; i++) str[i] = str[i+size];
}
len -= size;
return *this;
}
{
int i,size = length;
if(pos<0 || pos>len)
throw out_of_range("数组越界!");
if(length != 0)
{
if(pos+length-1 > len-1)
size = len-pos;
for(i=pos; i+size<=len; i++) str[i] = str[i+size];
}
len -= size;
return *this;
}
int SString::indexOf(char ch)
{
for(int i=0; i<len; i++)
if(ch==str[i])
return i;
return -1;
}
{
for(int i=0; i<len; i++)
if(ch==str[i])
return i;
return -1;
}
double SString::stringToDouble()
{
double temp;
string strTest(str);
istringstream istr(strTest);
istr>>temp;
if(istr.fail() || !istr.eof())
return 0;
return temp;
}
int SString::stringToInt()
{
int temp;
string strTest(str);
istringstream istr(strTest);
istr>>temp;
if(istr.fail() || !istr.eof())
return 0;
return temp;
}
{
int temp;
string strTest(str);
istringstream istr(strTest);
istr>>temp;
if(istr.fail() || !istr.eof())
return 0;
return temp;
}
float SString::stringToFloat()
{
float temp;
string strTest(str);
istringstream istr(strTest);
istr>>temp;
if(istr.fail() || !istr.eof())
return 0;
return temp;
}
测试文件
#include "stdafx.h"
#include "SString.h"
#include <iostream>
#include <string>
using namespace std;
{
float temp;
string strTest(str);
istringstream istr(strTest);
istr>>temp;
if(istr.fail() || !istr.eof())
return 0;
return temp;
}
测试文件
#include "stdafx.h"
#include "SString.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
SString ss("1.222"); //只是简单测试下 转换功能
float p = ss.stringToDouble();
cout<<p<<endl;
getchar();
return 0;
}
{
SString ss("1.222"); //只是简单测试下 转换功能
float p = ss.stringToDouble();
cout<<p<<endl;
getchar();
return 0;
}