我正在使用多种方法编写自定义字符串类。问题是比较方法不能像我想的那样工作。当两个char数组不同时,if条件仍然在我的main函数中继续进行,而不是什么都不做。
使用g ++编译时没有给出错误。代码在语法上是正确的,但逻辑上是错误的。我知道这是因为我可以给比较方法两个内容不同的char数组,并且它们是否以这种方式不同并不重要,因为main函数将运行if条件为" s8.compare(s7)= = 1"无论比较方法中的结果是否为真。
我将在下面发布完整的代码。任何帮助是极大的赞赏。
string.h中
sessionFactory.setAnnotatedClasses(new Class[] { Book.class });
的main.cpp
class Str {
private:
char *value;
int length;
int capacity;
//Doubles the size of the string when called.
void growArray();
//If the two strings are uneven, get absolute value of difference in length.
int difference(int a, int b);
//Calculates the size of a character array, passed in as an argument
int getCharArrSize(const char *v);
public:
Str();
explicit Str(const char *STR);
void copy(Str s);
void concatenate(Str s);
bool compare(Str s);
void print();
};
//Str constructor
Str::Str() {
//Assign value, capacity, and length to any new Str object
value = new char[100];
capacity = 100;
length = 0;
}
//Pass STR object as a pointer to string object constructor
Str::Str(const char *STR) {
length = getCharArrSize(STR);
capacity = 100;
value = new char[capacity];
//Copy contents from STR to string object
for (int i = 0; i < length; i++)
value[i] = STR[i];
}
//Doubles the size of the string when called.
void Str::growArray() {
const char *tmp = value;
capacity *= 2;
value = new char[capacity];
for (int i = 0; i < length; i++)
value[i] = tmp[i];
}
//If the two strings are uneven, get absolute value of difference in length.
int Str::difference(int a, int b) {
int d = 0;
if (a > b) d = a - b;
else if (b > a) d = b - a;
return d;
}
//Calculates the size of a character array, passed in as an argument
int Str::getCharArrSize(const char *v) {
int c = 0;
while (v[c] != '\0') {
c++;
}
return c;
}
//Overwrites the data of the string array with the data contained in s
void Str::copy(Str s) {
//Check ability for empty string object to hold Str s contents
if (capacity > s.length) {
//Copy over each element until s length is reached
for (int i = 0; i < s.length ; i++)
value[i] = s.value[i];
//Set string object length to copy's size
length = getCharArrSize(value);
} else { growArray(); }
}
//Concatenate Str s onto string object
void Str::concatenate(Str s) {
//Check ability for string object to hold itself and concatenated chars
if (capacity > length + s.length) {
//Fill string object with s object until end of combined lengths if necessary
for (int i = 0; i < length + s.length; i++)
value[length + i] = s.value[i];
//Set length based on chars in concatenated string object
length = getCharArrSize(value);
} else { growArray(); }
}
//Compare each element in Str s against string for similarities
bool Str::compare(Str s) {
if (length == s.length) {
if (*value == *s.value) {
while ((*value != value[length]) && (*s.value != s.value[s.length])) {
value++;
s.value++;
}
return true;
} else return false;
} else {
difference(length, s.length);
}
}
//Print function
void Str::print() {
std::cout << value << std::endl;
}
上面的代码返回一个看似正确的结果,但是将(s8.compare(s7)== 1)更改为类似(s8.compare(s5)== 1)返回&#39;它们匹配!&# 39;当我试图检查char数组中的每个单独元素时,如果它们的长度相同并且每个字符在数组中匹配,则只返回true。
答案 0 :(得分:0)
您的程序有未定义的行为,因为Str::compare
在其中一个分支中没有return
语句。
bool Str::compare(Str s) {
if (length == s.length) {
...
} else {
// Missing return statement.
difference(length, s.length);
}
}
也许您想将该行更改为:
return (difference(length, s.length) == 0);
答案 1 :(得分:0)
您的循环在没有比较的情况下运行。您比较char数组中的初始值,然后循环遍历其余部分而不进行比较。所以每次初始值相等时你都会返回true。
在确定相同长度后循环运行之后,比较每个字符。如果它们不相等,则函数将返回false。否则该函数将返回true。
bool Str::compare(Str s) {
if (length == s.length) {
while ((*value != value[length]) && (*s.value != s.value[s.length])) {
if (*value == *s.value) {
value++;
s.value++;
} else {
return false;//will return false as soon as a comparison is false
}
}
return true;
} else {
difference(length, s.length);
}
}
您还需要从差异函数返回一个布尔值。如果要将该函数开关的int返回到compare函数的int返回,并使用0和1s作为它们的布尔对应项。