先写这么多,收工

目前完成:
=, +=, 对比判断, 各种构造方法以及析构
待完成:
<<, >>, []等
main
lensfrex 3 years ago
parent 0f10a88a6b
commit 12cd67a899
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 8
      work20220401/.idea/.gitignore
  2. 14
      work20220401/.idea/deployment.xml
  3. 4
      work20220401/.idea/misc.xml
  4. 8
      work20220401/.idea/modules.xml
  5. 6
      work20220401/.idea/vcs.xml
  6. 2
      work20220401/.idea/work20220401.iml
  7. 6
      work20220401/CMakeLists.txt
  8. 29
      work20220401/main.cpp
  9. 127
      work20220401/neoString/CString.cpp
  10. 38
      work20220401/neoString/CString.h

@ -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,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" />

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.21)
project(work20220401)
set(CMAKE_CXX_STANDARD 14)
add_executable(work20220401 main.cpp neoString/CString.cpp neoString/CString.h)

@ -0,0 +1,29 @@
#include "iostream"
#include "neoString/CString.h"
using namespace std;
int main() {
int n, i, j;
while (cin >> n) {
CString *c = new CString[n + 2];
for (i = 0; i < n; i++) {
cin >> c[i];
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (c[j] > c[j + 1]) {
c[n] = c[j];
c[j] = c[j + 1];
c[j + 1] = c[n];
}
}
}
for (i = 0; i < n; i++) {
c[n + 1] += c[i];
}
cout << c[n + 1] << endl;
delete[] c;
}
return 0;
}

@ -0,0 +1,127 @@
//
// Created by lenfrex on 2022/4/4.
//
#include <cstring>
#include "CString.h"
/*
*
*/
CString::CString(int length) {
data = new char[length]();
CString::length = length;
}
CString::~CString() {
delete[] data;
}
/*
* 512
*/
CString::CString() : CString(512) {}
/*
* Java中String类的equal()
* falsedata为nullptrfalse
* true
*
*/
bool CString::operator==(const CString &source) const {
// 判断两CString长度以及本对象data成员是否为空指针
if (source.length != length || data == nullptr) {
return false;
} else {
// 判断两对象data成员是否指向同一内存块
if (source.data == data) {
return true;
}
}
// 以上情况都不符合时才进行逐字比较
int compareLength = source.length < length ? source.length : length;
for (int i = 0; i < compareLength; ++i) {
if (data[i] != source.data[i]) {
return false;
}
}
return true;
}
/*
*
*/
CString::CString(char *source) : CString((int)strlen(source)) {
strncpy(data, source, length);
}
/*
*
*/
CString::CString(char *source, int length) : CString(length) {
strncpy(data, source, length);
}
CString &CString::operator=(const CString &source) {
// 处理自己给自己赋值的情况,那叫啥,自赋值
if (this == &source) {
return *this;
}
if (data != nullptr && length != 0) {
delete[] data;
}
this->length = source.length;
this->data = new char[length]();
strncpy(data, source.data, length);
return *this;
}
CString &CString::operator+=(const CString &source) {
if (data == nullptr && length == 0) {
this->length = source.length;
this->data = new char[length]();
}
char *tmp = new char[length]();
strncpy(tmp, data, length);
// 重新分配内存,防止拼接后溢出
delete[] data;
this->length += source.length;
this->data = new char[this->length];
strcpy(tmp, data);
strncat(data, source.data, source.length);
return *this;
}
CString::CString(const CString &source) : CString(source.data, source.length) {}
/*
*
*/
bool CString::operator<(const CString &source) const {
// 这部分类似于前边==重载的前置判断
if (source.length != length || data == nullptr) {
return false;
} else {
if (source.data == data) {
return true;
}
}
// 以上情况都不符合时才进行逐字比较
int compareLength = source.length < length ? source.length : length;
for (int i = 0; i < compareLength; ++i) {
if (data[i] < source.data[i]) {
return true;
}
}
return false;
}

@ -0,0 +1,38 @@
//
// Created by lenfrex on 2022/4/4.
//
#ifndef WORK20220401_CSTRING_H
#define WORK20220401_CSTRING_H
#include "utility"
using namespace std::rel_ops;
class CString {
private:
char *data = nullptr;
int length = 0;
public:
explicit CString(int length);
CString(char *data, int length);
explicit CString(char *source);
CString(const CString &source);
CString();
~CString();
bool operator==(const CString &source) const;
bool operator<(const CString &source) const;
CString &operator+=(const CString &source);
CString &operator=(const CString &source);
};
#endif //WORK20220401_CSTRING_H
Loading…
Cancel
Save