gpt4 book ai didi

c++ - 在较大字符串中查找字符串的函数

转载 作者:搜寻专家 更新时间:2023-10-31 01:14:40 26 4
gpt4 key购买 nike

这是我用来实现这个程序的完整代码。一切似乎都可以编译和运行,但是一旦它运行我的 find 方法,程序似乎停止并且不执行 main.cpp 文件中说明匹配子字符串的最后一行。非常感谢任何帮助!

.h文件:

#include <iostream>
using namespace std;

class MyString
{
public:
MyString();
MyString(const char *message);
MyString(const MyString &source);
~MyString();
const void Print() const;
const int Length() const;
MyString& operator()(const int index, const char b);
char& operator()(const int i);

MyString& operator=(const MyString& rhs);
bool operator==(const MyString& other) const;
bool operator!=(const MyString& other) const;
const MyString operator+(const MyString& rhs) const;
MyString& operator+=(const MyString& rhs);
friend ostream& operator<<(ostream& output, const MyString& rhs);
const int Find(const MyString& other);
MyString Substring(int start, int length);

private:
char *String;
int Size;


};

istream& operator>>(istream& input, MyString& rhs);

.cpp 文件:

   #include <iostream>
#include <cstdlib>
#include "MyString.h"

using namespace std;

//default constructor that sets the initial string to the value "Hello World"
MyString::MyString()
{
char temp[] = "Hello World";

int counter(0);
while(temp[counter] != '\0')
{
counter++;
}
Size = counter;
String = new char [Size];
for(int i=0; i < Size; i++)
String[i] = temp[i];

}

//alternate constructor that allows for setting of the inital value of the string
MyString::MyString(const char *message)
{
int counter(0);
while(message[counter] != '\0')
{
counter++;
}
Size = counter;
String = new char [Size];
for(int i=0; i < Size; i++)
String[i] = message[i];
}

//copy constructor
MyString::MyString(const MyString &source)
{

int counter(0);
while(source.String[counter] != '\0')
{
counter++;
}
Size = counter;
String = new char[Size];
for(int i = 0; i < Size; i++)
String[i] = source.String[i];
}

//Deconstructor
MyString::~MyString()
{
delete [] String;
}

//Length() method that reports the length of the string
const int MyString::Length() const
{
int counter(0);

while(String[counter] != '\0')
{
counter ++;
}
return (counter);
}

/*Parenthesis operator should be overloaded to replace the Set and Get functions of your previous assignment. Note that both instances should issue exit(1) upon violation of the string array bounaries.
*/
MyString& MyString::operator()(const int index, const char b)
{
if(String[index] == '\0')
{
exit(1);
}
else
{
String[index] = b;
}
}

char& MyString::operator()(const int i)
{
if(String[i] == '\0')
{
exit(1);
}
else
{
return String[i];
}
}
/*Assignment operator (=) which will copy the source string into the destination string. Note that size of the destination needs to be adjusted to be the same as the source.
*/
MyString& MyString::operator=(const MyString& rhs)
{
if(this != &rhs)
{
delete [] String;
String = new char[rhs.Size];
Size = rhs.Size;

for(int i = 0; i < rhs.Size+1 ; i++)
{
String[i] = rhs.String[i];
}
}

return *this;
}
/*Logical comparison operator (==) that returns true iff the two strings are identical in size and contents.
*/
bool MyString::operator==(const MyString& other)const
{
if(other.Size == this->Size)
{
for(int i = 0; i < this->Size+1; i++)
{
if(&other == this)

return true;
}
}
else
return false;
}

//Negated logical comparison operator (!=) that returns boolean negation of 2
bool MyString::operator!=(const MyString& other) const
{
return !(*this == other);
}

//Addition operator (+) that concatenates two strings
const MyString MyString::operator+(const MyString& rhs) const
{
char* tmp = new char[Size + rhs.Size +1];

for(int i = 0; i < Size; i++)
{
tmp[i] = String[i];
}
for(int i = 0; i < rhs.Size+1; i++)
{
tmp[i+Size] = rhs.String[i];
}
MyString result;

delete [] result.String;
result.String = tmp;
result.Size = Size+rhs.Size;

return result;
}
/*Addition/Assigment operator (+=) used in the following fashion: String1 += String2 to operate as String1 = String1 + String2
*/
MyString& MyString::operator+=(const MyString& rhs)
{
char* tmp = new char[Size + rhs.Size + 1];

for(int i = 0; i < Size; i++)
{
tmp[i] = String[i];
} for(int i = 0; i < rhs.Size+1; i++)
{
tmp[i+Size] = rhs.String[i];
}

delete [] String;
String = tmp;
Size += rhs.Size;

return *this;
}
istream& operator>>(istream& input, MyString& rhs)
{
char* t;
int size(256);
t = new char[size];
input.getline(t,size);

rhs = MyString(t);
delete [] t;

return input;
}

ostream& operator<<(ostream& output, const MyString& rhs)
{
if(rhs.String != '\0')
{
output << rhs.String;
}
else
{
output<<"No String to output\n";
}

return output;
}

const int MyString::Find(const MyString& other)
{
int nfound = -1;

if(other.Size > Size)
{
return nfound;
}
int i = 0, j = 0;
for(i = 0; i < Size; i++)
{
for(j = 0; j < other.Size; j++)
{
if( ((i+j) >= Size) || (String[i+j] != other.String[j]) )
{
break;
}

}

if(j == other.Size)
{
return i;
}

}

return nfound;
}
/*MyString::Substring(start, length). This method returns a substring of the original string that contains the same characters as the original string starting at location start and is as long as length.
*/

MyString MyString::Substring(int start, int length)
{
char* sub;
sub = new char[length+1];


while(start != '\0')
{
for(int i = start; i < length+1; i++)
{
sub[i] = String[i];
}
}
return MyString(sub);
}

//Print() method that prints the string
const void MyString::Print() const
{

for(int i=0; i < Size; i++)
{
cout<<String[i];
}
cout<<endl;
}

main.cpp 文件:

  #include <cstdlib>
#include <iostream>
#include "MyString.h"


using namespace std;

/*
*
*/
int main (int argc, char **argv)
{

MyString String1; // String1 must be defined within the scope

const MyString ConstString("Target string"); //Test of alternate constructor

MyString SearchString; //Test of default constructor that should set "Hello World". W/o ()
MyString TargetString (String1); //Test of copy constructor


cout << "Please enter two strings. ";
cout << "Each string needs to be shorter than 256 characters or terminated by /\n." << endl;
cout << "The first string will be searched to see whether it contains exactly the second string. " << endl;

cin >> SearchString >> TargetString; // Test of cascaded string-extraction operator


if(SearchString.Find(TargetString) == -1) {
cout << TargetString << " is not in " << SearchString << endl;
}
else {
cout << TargetString << " is in " << SearchString << endl;
cout << "Details of the hit: " << endl;
cout << "Starting poisition of the hit: " << SearchString.Find(TargetString) << endl;
cout << "The matching substring is: " << SearchString.Substring(SearchString.Find(TargetString), TargetString.Length());
}
return 0;
}

最佳答案

看来内层循环的不变量是 j 在 0 和 end-2 之间(含 0 和 end-2)。因此 j 永远不会等于 end(“匹配”条件)。

关于c++ - 在较大字符串中查找字符串的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com