请问一下各位兄弟 如图所示 查找字符串"999"找不到是怎么回事?
int转字符串超出int范围如何限制一下?
代码如下:
#include <iostream>
using namespace std;
class HSTRING
{
public:
HSTRING();
HSTRING(const char* str);
~HSTRING();
HSTRING(const HSTRING& str);
HSTRING& operator+(const HSTRING& str);
HSTRING& operator-(const HSTRING& str);
HSTRING& operator=(const HSTRING& str);
unsigned int neicunlen;
unsigned int zifuchuanlen;
unsigned int GETLENTH(const char* str) const;//内存,字符串,获取的长度;
char* SSTR;
char* getStr() { return SSTR; }
void fuzhistr(const char* yuantou);
void xiugai(const HSTRING& xg, const HSTRING& xd);
// 修改函数,xg为修改位置,xd为修改后的字符串;
int xunzhao(const HSTRING& s) const;
int strsr(const HSTRING& s) const;
HSTRING& operator=(int dnf);
};
HSTRING::HSTRING()
{
neicunlen = 0x64;
zifuchuanlen = 0;
SSTR = new char[neicunlen];
}
HSTRING::~HSTRING()
{
if (SSTR != nullptr)
{
delete[] SSTR;
SSTR = nullptr;
}
}
void HSTRING::fuzhistr(const char* yuantou)
{
unsigned int len = GETLENTH(yuantou);
if (len > neicunlen)
{
char* xstr = SSTR;
SSTR = new char[len];//重新分配;
neicunlen = len;//修正长度;
delete[] xstr;
}
memcpy(SSTR, yuantou, len);
zifuchuanlen = len;
}
unsigned int HSTRING::GETLENTH(const char* str) const
{
unsigned int len = 0;
while (str[len++]);
return len;
}
HSTRING::HSTRING(const char* str) :HSTRING()
{
fuzhistr(str);
}
HSTRING::HSTRING(const HSTRING& str) :HSTRING()
{
fuzhistr(str.SSTR);
}
HSTRING& HSTRING::operator=(const HSTRING& str)
{
if (this == &str)return *this;
if (SSTR != nullptr)
{
delete[] SSTR;
SSTR = nullptr;
}
zifuchuanlen = str.zifuchuanlen;
neicunlen = str.neicunlen;
SSTR = new char[neicunlen];
fuzhistr(str.SSTR);
return *this;
}
HSTRING& HSTRING::operator+(const HSTRING& str)
{
unsigned int SLEN = zifuchuanlen + str.zifuchuanlen - 1;
if (SLEN > neicunlen)// 检查内存空间;
{
char* new_SSTR = new char[SLEN];
memcpy(new_SSTR, SSTR, zifuchuanlen);
delete[] SSTR;
SSTR = new_SSTR;
neicunlen = SLEN;// 更新内存长度;
}
memcpy(SSTR + zifuchuanlen - 1, str.SSTR, str.zifuchuanlen);
zifuchuanlen = SLEN; // 更新字符串的长度;
return *this;
}
HSTRING& HSTRING::operator-(const HSTRING& str)
{
bool wan = true; int h = 0;
for (int z = 0; z < zifuchuanlen; z++)
{
while (h < str.zifuchuanlen - 1 && z + h < zifuchuanlen && SSTR[z + h] == str.SSTR[h])++h;
if (h == str.zifuchuanlen - 1)
{
wan = false;
memcpy(SSTR + z, SSTR + z + str.zifuchuanlen - 1, zifuchuanlen - (z + str.zifuchuanlen - 1));
zifuchuanlen = zifuchuanlen - str.zifuchuanlen + 1; // 减删除的;
break;
}
z += h;
}
if (wan) cout << "对不起!没有找到要删除的数据" << endl;
return *this;
}
HSTRING& HSTRING::operator=(int dnf)
{
char str[0x20]{ 0 }; char len{ 0x1F }; bool zh = dnf >= 0;
dnf = dnf * (zh * 2 - 1);
do str[--len] = dnf % 10 + 48;
while (dnf = dnf / 10);
str[len = len - 1 * (1 - zh)] = '-' * (zh + 1) * (1 - zh) + str[len] * zh;
unsigned int SLEN = 0x20 - len;
if (SLEN > neicunlen)
{
delete[] SSTR;
SSTR = new char[SLEN] {};
neicunlen = SLEN;
}
if ( (int)str >= 2147483647)
{
cout << "对不起,超出int范围,无法转换!";
}
zifuchuanlen = neicunlen;
memcpy(SSTR, str + len, zifuchuanlen);
return *this;
}
void HSTRING::xiugai(const HSTRING& xgd, const HSTRING& xd)
{
int start = strsr(xgd);//判断修改的字符串;
unsigned int len_1 = GETLENTH(xgd.SSTR) - 1;
unsigned int len_2 = GETLENTH(xd.SSTR) - 1;
if (start != -1)
{
unsigned int SLEN = zifuchuanlen + len_2 - len_1;
if (len_2 > len_1)
{
char* xstr = SSTR;
SSTR = new char[SLEN];//重新分配;
memcpy(SSTR, xstr, zifuchuanlen);
delete[] xstr;
}
memcpy(SSTR + start + len_2, SSTR + start + len_1, zifuchuanlen - len_1 - start);
memcpy(SSTR + start, xd.SSTR, len_2);
neicunlen = SLEN; //修正内存长度;
zifuchuanlen = SLEN;//修正字符串长度;
}
}
int HSTRING::xunzhao(const HSTRING& xd) const
{
unsigned int hanzi = 0;
if (zifuchuanlen >= xd.zifuchuanlen)
{
for (int i = 0; i < zifuchuanlen - xd.zifuchuanlen + 1; i++)
{
int j = 0;
// 如果匹配,继续匹配下一个;
while (j < xd.zifuchuanlen && SSTR[i + j] == xd.SSTR[j])
{
j++;
}
if (j == xd.zifuchuanlen - 1 && SSTR[i] == xd.SSTR[0])
{
for (int z = 0; z < i; z++)
{
if (SSTR[z] < 0) { z++; hanzi++; }
}
return i - hanzi;
}
}
}
cout << "对不起!没有找到你要的数据!";
return -1;
}
int HSTRING::strsr(const HSTRING& xd) const
{
unsigned int len_1 = zifuchuanlen;
unsigned int len_2 = xd.zifuchuanlen - 1;
if (len_1 >= len_2)
{
for (int i = 0; i < len_1; i++)
{
int n = 0;
while (n < len_2 && SSTR[n + i] == xd.SSTR[n])n++;
if (n == len_2) return i;
}
}
cout << " 对不起!无法完成修改!\n";
return -1;
}
int main()
{
HSTRING str("12345678你好999");
HSTRING str2 = "abc";
//增加数据 例:123456789 + abc 得到 123456789abc ;
//str + str2;
//cout << "结果:" << str.getStr() << endl;
// 删除数据 例:123456789 - 456 得到 123789 ;
//str - "456";
//cout << "结果:" << str.getStr() << endl;
//改数据 例:123456789中的34修改为 abc 得到 12abc56789 ;
//HSTRING str3 = str;
//str.xiugai("34", "abc");
//cout << "结果:" << str.getStr() << endl;
//查数据 例:123456 查 34 得到 34的位置 2 ;
cout << "结果:" << str.xunzhao("999") << endl;
//cout << "查不到试验:" << str.xunzhao("中文66666") << endl;
//转换数据 实现 int 转 HSTRING 字符串;
//str = "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈";
str=98765432155555555;
// str=-66666666;
cout << "结果:" << str.getStr() << endl;
}
1.j==xd.zifuchuanlen-1改为j==xd.zifuchuanlen,否则末尾的无法匹配
2.只考虑被匹配的字符串会有汉字,没有考虑要找的内容里可能有汉字
如果有用麻烦点个采纳,谢谢~
int HSTRING::xunzhao(const HSTRING& xd) const
{
unsigned int hanzi = 0;
if (zifuchuanlen >= xd.zifuchuanlen)
{
for (int i = 0; i < zifuchuanlen - xd.zifuchuanlen + 1; i++)
{
int j = 0;
// 如果匹配,继续匹配下一个;
while (j < xd.zifuchuanlen && SSTR[i + j] == xd.SSTR[j])
{
j++;
}
if (j == xd.zifuchuanlen && SSTR[i] == xd.SSTR[0]) // 修改 -1
{
/*for (int z = 0; z < i; z++)
{
if (SSTR[z] < 0) { z++; hanzi++; }
}
return i - hanzi;*/ // 去掉
return i;
}
}
}
cout << "对不起!没有找到你要的数据!";
return -1;