实验3:作业魔改版(plugless)

main
lensfrex 3 years ago
parent 847ec0fb15
commit f01a0d08e6
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 2
      .gitignore
  2. 8
      work20220407/.idea/.gitignore
  3. 14
      work20220407/.idea/deployment.xml
  4. 8
      work20220407/.idea/encodings.xml
  5. 4
      work20220407/.idea/misc.xml
  6. 8
      work20220407/.idea/modules.xml
  7. 6
      work20220407/.idea/vcs.xml
  8. 2
      work20220407/.idea/work20220401.iml
  9. 8
      work20220407/main.cpp
  10. 40
      work20220407/string/String.cpp
  11. 6
      work20220407/string/String.h

2
.gitignore vendored

@ -24,3 +24,5 @@
cmake-build-*/ cmake-build-*/
out/ out/
.vs/

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="remote_linux (563cc454-127a-4c53-a847-5bf281087bb1)">
<serverdata>
<mappings>
<mapping local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
</serverData>
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="file://$PROJECT_DIR$/string/String.cpp" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/string/String.h" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/work20220401.iml" filepath="$PROJECT_DIR$/.idea/work20220401.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

@ -1,9 +1,9 @@
#include <iostream> #include <iostream>
#include "string/String.h" #include "string/String.h"
using namespace std; using namespace std;
// 测试示例来自题目
int main() { int main() {
String s1("Help!"),s2("Good!"),s3(s2),s4,s5; String s1("Help!"),s2("Good!"),s3(s2),s4,s5;
cout<<"s1="<<s1<<endl; cout<<"s1="<<s1<<endl;
@ -15,12 +15,10 @@ int main() {
cout<<"s3="<<s3<<endl; cout<<"s3="<<s3<<endl;
cin>>s4; cin>>s4;
cout<<"s4="<<s4<<endl; cout<<"s4="<<s4<<endl;
cout<<"s4length="<<s4.Length()<<endl; s5=s3+s4;
/*s5=s3+s4;
cout<<"s5="<<s5<<endl; cout<<"s5="<<s5<<endl;
s5[0]='g'; s5[0]='g';
cout<<"s5="<<s5<<endl; cout<<"s5="<<s5<<endl;
cout<<"strlen(s5)="<<s5.Length()<<endl;*/ cout<<"strlen(s5)="<<s5.Length()<<endl;
cout<<boolalpha<<(s3 == s1)<<endl; cout<<boolalpha<<(s3 == s1)<<endl;
} }

@ -77,14 +77,7 @@ bool String::equals(const char *source, int sourceLength) const {
} }
} }
// 以上情况都不符合时才进行逐字比较 return strcmp(this->data, source) == 0;
// 能执行到此处两者长度肯定相等,因此访问数组不会越界
for (int i = 0; i < length; ++i) {
if (data[i] != source[i]) {
return false;
}
}
return true;
} }
String &String::operator=(const String &source) { String &String::operator=(const String &source) {
@ -98,6 +91,15 @@ String &String::operator=(const String &source) {
} }
String &String::operator=(const char *source) { String &String::operator=(const char *source) {
if (source == nullptr) {
delete[] data;
this->data = nullptr;
length = 0;
allocSize = 0;
return *this;
}
copyData(source, (int) strlen(source)); copyData(source, (int) strlen(source));
return *this; return *this;
@ -144,7 +146,7 @@ void String::addString(const char *source, int sourceLength) {
// 本对象nullptr的话就直接新分配 // 本对象nullptr的话就直接新分配
if (data == nullptr) { if (data == nullptr) {
allocSize = sourceLength + 2 + 1; allocSize = sourceLength + 1;
this->data = new char[allocSize](); this->data = new char[allocSize]();
length = sourceLength; length = sourceLength;
@ -153,13 +155,13 @@ void String::addString(const char *source, int sourceLength) {
} }
// 如果拼接后字符长度超过已分配的内存大小,则重新分配data内存大小 // 如果拼接后字符长度超过已分配的内存大小,则重新分配data内存大小
if (allocSize < (length + sourceLength + 2)) { if (allocSize < (length + sourceLength + 1)) {
// 保存拼接前的原数据,以便重新分配足够的内存 // 保存拼接前的原数据,以便重新分配足够的内存
char *old = new char[length + 1](); char *old = new char[length + 1]();
strcpy(old, data); strcpy(old, data);
// 重新分配内存,防止拼接后溢出,2为两个横杠大小 // 重新分配内存,防止拼接后溢出,2为两个横杠大小
length += sourceLength + 2; length += sourceLength;
allocSize = length + 1; allocSize = length + 1;
delete[] data; delete[] data;
@ -169,6 +171,7 @@ void String::addString(const char *source, int sourceLength) {
} }
strcat(data, source); strcat(data, source);
length += sourceLength;
} }
/** /**
@ -202,26 +205,25 @@ std::istream &operator>>(std::istream &inputStream, String &source) {
return inputStream; return inputStream;
} }
char String::operator[](int i) { char &String::operator[](int i) {
return (i < length && i >= 0) ? data[i] : '\0'; return (i < length && i >= 0) ? data[i] : data[0];
} }
/** /**
* *
*
*/ */
String &String::operator+(const char *source) { String String::operator+(const char *source) {
int sourceLength = (int) strlen(source); int sourceLength = (int) strlen(source);
String target(this->length + sourceLength + 1); String target(this->data, this->length + sourceLength + 1);
target = this->data;
target += source; target += source;
return target; return target;
} }
String &String::operator+(const String &source) { String String::operator+(const String &source) {
String target(this->length + source.length); String target(this->data, this->length + source.length);
target = this->data;
target += source; target += source;
return target; return target;

@ -54,15 +54,15 @@ public:
String &operator=(const char *source); String &operator=(const char *source);
String &operator+(const char *source); String operator+(const char *source);
String &operator+(const String &source); String operator+(const String &source);
friend std::ostream &operator<<(std::ostream &outputStream, const String &source); friend std::ostream &operator<<(std::ostream &outputStream, const String &source);
friend std::istream &operator>>(std::istream &outputStream, String &source); friend std::istream &operator>>(std::istream &outputStream, String &source);
char operator[](int i); char &operator[](int i);
int Length() const; int Length() const;

Loading…
Cancel
Save