同步代码

main
lensfrex 2 years ago
parent 3ac0fd4479
commit 2e90624a1e
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 0
      pure-c-test/20220407/3-7.c
  2. 0
      pure-c-test/20220407/3-8.c
  3. 8
      work20220401/.idea/encodings.xml
  4. 2
      work20220401/neoString/CString.cpp
  5. 2
      work20220401/neoString/CString.h
  6. 13
      work20220407/.idea/deployment.xml
  7. 10
      work20220407/.idea/other.xml
  8. 36
      work20220407/main.cpp
  9. 45
      work20220407/string/String.cpp

@ -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$/neoString/CString.cpp" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/neoString/CString.h" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

@ -22,7 +22,7 @@ CString::CString() : CString(512) {}
/**
*
*/
CString::CString(char *source) : CString(source, (int) strlen(source)) {}
CString::CString(const char *source) : CString(source, (int) strlen(source)) {}
/**
*

@ -24,7 +24,7 @@ public:
CString(char *data, int length);
explicit CString(char *source);
explicit CString(const char *source);
CString(const CString &source);

@ -1,12 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
<component name="PublishConfigData" serverName="Remote Development" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="remote_linux (563cc454-127a-4c53-a847-5bf281087bb1)">
<serverdata>
<mappings>
<mapping local="$PROJECT_DIR$" web="/" />
<mapping deploy="/tmp/tmp.DKsvCRfOsP" local="$PROJECT_DIR$" web="/" />
</mappings>
<excludedPaths>
<excludedPath local="true" path="$PROJECT_DIR$/cmake-build-debug-msvc" />
<excludedPath local="true" path="$PROJECT_DIR$/cmake-build-debug-clang" />
<excludedPath local="true" path="$PROJECT_DIR$/cmake-build-debug-remotelinux" />
<excludedPath local="true" path="$PROJECT_DIR$/cmake-build-debug-mingw-112" />
<excludedPath local="true" path="$PROJECT_DIR$/cmake-build-debug-mingw-81" />
<excludedPath local="true" path="$PROJECT_DIR$/cmake-build-debug-remotelinux" />
<excludedPath local="true" path="$PROJECT_DIR$/cmake-build-release-mingw-112" />
</excludedPaths>
</serverdata>
</paths>
</serverData>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoUploadManager">
<option name="hosts">
<list>
<option value="563cc454-127a-4c53-a847-5bf281087bb1" />
</list>
</option>
</component>
</project>

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

@ -4,8 +4,12 @@
#include <cstring>
#include <algorithm>
#include <cassert>
#include "String.h"
#include "utility"
using namespace std::rel_ops;
/**
*
@ -15,7 +19,7 @@ String::String(int size) : length(0), allocSize(size) {
}
/**
* 128
* 128
*/
String::String() : String(128) {}
@ -34,17 +38,15 @@ String::String(const char *source, int length) : String(length + 1) {
this->length = length;
}
String::String(const String &source) : String(source.data, source.length) {}
String::~String() {
delete[] data;
data = nullptr;
}
String::String(const String &source) : String(source.data, source.length) {}
/**
* equals函数
* @param source
* @return
*/
bool String::operator==(const String &source) const {
return equals(source.data, source.length);
@ -55,7 +57,7 @@ bool String::operator==(const char *source) const {
}
/**
* Java中String类的equal()
*
* data是否为nullptrnull则truefalse
* false
* true
@ -80,6 +82,9 @@ bool String::equals(const char *source, int sourceLength) const {
return strcmp(this->data, source) == 0;
}
/*
* copyData()
*/
String &String::operator=(const String &source) {
if (this == &source) {
return *this;
@ -161,9 +166,9 @@ void String::addString(const char *source, int sourceLength) {
strcpy(old, data);
// 重新分配内存,防止拼接后溢出
// 分配两倍内存空间来减少重新分配次数
delete[] data;
// 分配两倍内存空间来减少重新分配次数(空间换时间?)
allocSize = (length + sourceLength)*2 + 1;
delete[] data;
this->data = new char[allocSize]();
strcpy(data, old);
@ -196,20 +201,30 @@ std::ostream &operator<<(std::ostream &outputStream, const String &source) {
}
std::istream &operator>>(std::istream &inputStream, String &source) {
// 使用istream.get(char*, int)可以限制读入的长度,防止输入超长的字符串导致溢出,但是空格也会被读入
// 手动分割太麻烦,因此只好先用这种方法了
inputStream >> source.data;
source.length = (int) strlen(source.data);
// 其实这里有个问题,如果用户的输入超过了4028个字符,会导致溢出,不安全
// 虽然可以像std::string那样分批读入,动态分配,这样过长就不会被截掉,
// 但是为了简单起见,在这里就直接>>了
char* input = new char[4028]();
if (std::cin >> input) {
strncpy(source.data, input, source.allocSize - 1);
int inputLength = (int) strlen(input);
source.length = (int) std::min(inputLength, source.allocSize - 1);
source.data[source.length] = '\0';
}
return inputStream;
}
char &String::operator[](int i) {
return (i < length && i >= 0) ? data[i] : data[0];
// 用断言处理下标越界,一般应该用异常处理,但是简单起见还是断言吧
assert(i < length);
return data[i];
}
/**
*
*
*
*/
String String::operator+(const char *source) {
@ -232,4 +247,4 @@ String String::operator+(const String &source) {
int String::Length() const {
return length;
}
}
Loading…
Cancel
Save