模拟String


//本来想模拟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;};  //进行清除
 //首先进行一些运算符的重载  大于 小于 等于等等
 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);
 }  
 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);  //删除特定位置的字符串
 //下面利用流写一些字符串和数字转换吧
 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;
}
SString::SString(const SString& refstr)
{
 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);
}
char &SString::operator[] (int 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 SString::operator> (const SString &refstr)
{
 return (strcmp(str,refstr.str) > 0);
}
int SString::operator== (const SString &refstr)
{
 return (strcmp(str,refstr.str));
}
int SString::operator!= (const SString &refstr)
{
 return (strcmp(str,refstr.str));
}
int SString::operator< (const SString &refstr)
{
 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;
}
SString SString::operator+ (const SString &refstr)
{
 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);
}
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';*/
}
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') ;
}
char SString::charAt(int 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& 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;
}
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 SString::indexOf(char ch)
{
 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;
 }
 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;
int _tmain(int argc, _TCHAR* argv[])
{
 SString ss("1.222");  //只是简单测试下 转换功能
 float p = ss.stringToDouble();
 cout<<p<<endl;
 getchar();
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值