廢了很大力氣,終於把C++的串基本操作給整明白了。
開始時最大的問題在於main函數呼叫strAssign時傳入一個字串參數時總是報錯。經過不懈努力終於給弄明白並且解決了
部分函數詳解:
1.strAssign:串的賦值操作不能直接用 = 實現。因爲串是一個數組,而 = 不能直接將一個數組賦值給另一個數組,這就要求我們必須對陣列中的元素進行逐個賦值。
2.strCompare:串的比較是串排序應用中的核心操作。設兩串待比較的字元分別爲a、b,若a的碼值小於b的碼值,返回a < b;大於返回a > b。若a = b則繼續比較下一對字元。如果上述步驟完成後沒有比較出結果,那麼先結束的就是較小串,同時結束則相等。
#include <iostream>
using namespace std;
struct String {
char *ch;
int length;
};
bool strAssign(String &str, const char *ch);
int getLength(String str);
int strCompare(String string1, String string2);
bool concat(String &str, String string1, String string2);
bool subString(String &str, String string, int length, int position);
bool clearString(String &string);
int main() {
String str{nullptr, 0};
String p{nullptr, 0};
String sum{};
const char *ch = "adfaf";
const char *s = "adafadsfdfa";
strAssign(str, ch);
strAssign(p, s);
if (strCompare(str, p) != 0) {
if (strCompare(str, p) < 0)
cout << "result: str < p" << endl;
else
cout << "result: str > p" << endl;
} else
cout << "str = p" << endl;
if (concat(sum, str, p)) {
cout << "concat success!" << endl;
cout << "sum: " << sum.ch << endl;
} else {
cout << "concat failed!" << endl;
}
cout << "sum' length: " << getLength(sum) << endl;
if (subString(str, sum, 4, 2)) {
cout << "sub success!" << endl;
cout << "subString:" << str.ch << endl;
} else
cout << "sub failed" << endl;
if (clearString(sum) && clearString(str) && clearString(p)) {
cout << "All string have been empty!" << endl;
} else{
cout << "clear failed!" << endl;
}
}
bool strAssign(String &str, const char *ch) {
delete str.ch;
int length = 0;
const char *c = ch;
while (*c != '\0') {
++length;
++c;
}
if (length == 0) {
str.length = 0;
str.ch = nullptr;
return true;
} else {
str.ch = new char[length + 1];
if (str.ch == nullptr)
return false;
else {
c = ch;
for (int i = 0; i <= length; ++i, ++c) {
str.ch[i] = *c;
}
str.length = length;
return true;
}
}
}
int getLength(String str) {
return str.length;
}
int strCompare(String string1, String string2) {
for (int i = 0; i < string1.length && i < string2.length; ++i) {
if (string1.ch[i] != string2.ch[i]) {
return string1.ch[i] - string2.ch[i];
}
}
return string1.length - string2.length;
}
bool concat(String &str, String string1, String string2) {
delete str.ch;
str.ch = nullptr;
str.ch = new char[string1.length + string2.length + 1];
if (str.ch == nullptr)
return false;
int i;
for (i = 0; i < string1.length; ++i) {
str.ch[i] = string1.ch[i];
}
for (int j = 0; j <= string2.length; ++j) {
str.ch[i + j] = string2.ch[j];
}
str.length = string1.length + string2.length;
return true;
}
bool subString(String &str, String string, int length, int position) {
if (position < 0 || position >= string.length
|| length > string.length - position)
return false;
delete str.ch;
str.ch = nullptr;
if (length == 0) {
str.ch = nullptr;
str.length = 0;
return true;
} else {
str.ch = new char[length + 1];
int j = 0;
for (int i = position; i < position + length; ++i, ++j) {
str.ch[j] = string.ch[j];
}
str.ch[j] = '\0';
str.length = length;
return true;
}
}
bool clearString(String &string){
delete string.ch;
string.ch = nullptr;
string.length = 0;
return true;
}