其他未完成
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
# 定义路径变量
|
||||
SRC_DIR = ./
|
||||
OBJ_DIR = ./
|
||||
BIN_DIR = ./
|
||||
|
||||
# 定义编译器
|
||||
CC = g++
|
||||
STD = -std=c++11
|
||||
# 定义目标文件
|
||||
TARGET = $(BIN_DIR)/a.out
|
||||
# 定义源文件
|
||||
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
|
||||
# OBJS = $(OBJ_DIR)/lrc.o $(OBJ_DIR)/console.o $(OBJ_DIR)/start_mplayer.o $(OBJ_DIR)/time_delay.o $(OBJ_DIR)/main.o
|
||||
OBJS = $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
|
||||
|
||||
# 编译规则
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
|
||||
$(CC) -c $< -o $@ $(STD)
|
||||
|
||||
# 链接规则
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $^ -o $@ $(STD)
|
||||
|
||||
# 默认构建目标
|
||||
all: $(TARGET)
|
||||
|
||||
# 创建目录
|
||||
$(shell mkdir -p $(OBJ_DIR) $(BIN_DIR))
|
||||
|
||||
# 运行测试
|
||||
run: $(TARGET)
|
||||
$(TARGET)
|
||||
|
||||
# 清理规则
|
||||
clean:
|
||||
rm -rf $(OBJ_DIR)/*.o $(TARGET)
|
||||
|
||||
# 伪目标
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "mystring.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
MyString s1("disen");
|
||||
cout << s1 + ",lucy" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "mystring.h"
|
||||
using namespace std;
|
||||
|
||||
ostream &operator<<(ostream &cout, const MyString &str)
|
||||
{
|
||||
cout << str.mStr; // 输出字符串 mStr
|
||||
return cout; // 返回 cout 输出流对象
|
||||
}
|
||||
istream &operator<<(istream &cin, MyString &str)
|
||||
{
|
||||
cin >> str.mStr; // 输入字符串 mStr
|
||||
return cin; // 返回 cin 输入流对象
|
||||
}
|
||||
|
||||
MyString::MyString(const char *str)
|
||||
{
|
||||
mSize = strlen(str); // 计算字符串长度
|
||||
mStr = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(mStr, str); // 拷贝字符串
|
||||
}
|
||||
|
||||
MyString::MyString(const MyString &str)
|
||||
{
|
||||
mSize = str.mSize; // 拷贝字符串长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, str.mStr); // 拷贝字符串
|
||||
delete[] this->mStr; // 删除之前的空间
|
||||
this->mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
MyString::~MyString()
|
||||
{
|
||||
delete[] mStr; // 释放空间
|
||||
}
|
||||
|
||||
// 字符串拼接
|
||||
MyString &MyString::operator+(MyString &other)
|
||||
{
|
||||
mSize += other.mSize; // 计算新的字符串长度, 用原有长度加上 other 的长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, mStr); // 拷贝 mStr
|
||||
strcat(p, other.mStr); // 拼接 other.mStr
|
||||
delete[] this->mStr; // 删除之前的空间
|
||||
this->mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
MyString &MyString::operator+(const char *other)
|
||||
{
|
||||
mSize += strlen(other); // 计算新的字符串长度, 用原有长度加上 other 的长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, mStr); // 拷贝 mStr
|
||||
strcat(p, other); // 拼接 other
|
||||
delete[] this->mStr; // 删除之前的空间
|
||||
this->mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
// 字符串赋值
|
||||
MyString &MyString::operator=(MyString &other)
|
||||
{
|
||||
mSize = other.mSize; // 拷贝字符串长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, other.mStr); // 拷贝字符串
|
||||
delete[] mStr; // 删除之前的空间
|
||||
mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
MyString &MyString::operator=(const char *other)
|
||||
{
|
||||
mSize = strlen(other); // 拷贝字符串长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, other); // 拷贝字符串
|
||||
delete[] mStr; // 删除之前的空间
|
||||
mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
// 字符串比较
|
||||
MyString &operator==(MyString &other)
|
||||
{
|
||||
return strcmp(mStr, other.mStr) == 0; // 比较字符串
|
||||
}
|
||||
|
||||
MyString &operator==(const char *other)
|
||||
{
|
||||
return strcmp(mStr, other) == 0; // 比较字符串
|
||||
}
|
||||
|
||||
// 读取字符串中 index 位置的字符
|
||||
char MyString::operator[](int index)
|
||||
{
|
||||
cout << "长度: " << mSize << endl;
|
||||
// index 支持负数, -1 表示倒数第一个字符
|
||||
// index 是负数,计算机存储是补码,如果直接位运算,以补码的方式 & 位运算符
|
||||
// 需要手动取反补码(~), 然后 +1
|
||||
int absIndex = (~index + 1) & 0x7fffffff; // 0x7fffffff 是有符号正整数 int 的最大值
|
||||
// 0x7fffffff = 0111 1111 1111 1111 1111 1111 1111 1111
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef __MYSTRING_H__
|
||||
#define __MYSTRING_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
// 重载: 实现自定义字符串类
|
||||
class MyString
|
||||
{
|
||||
friend ostream &operator<<(ostream &cout, const MyString &str) {}
|
||||
friend istream &operator<<(istream &cout, MyString &str) {}
|
||||
|
||||
public:
|
||||
MyString(const char *str); // 有参构造函数
|
||||
MyString(const MyString &str); // 拷贝构造函数
|
||||
~MyString(); // 析构函数
|
||||
|
||||
public:
|
||||
// 字符串拼接
|
||||
MyString &operator+(MyString &other); // 重载 + 运算符
|
||||
MyString &operator+(const char *other); // 重载 + 运算符
|
||||
|
||||
// 字符串赋值
|
||||
MyString &operator=(MyString &other); // 重载 = 运算符
|
||||
MyString &operator=(const char *other); // 重载 = 运算符
|
||||
|
||||
// 字符串比较
|
||||
MyString &operator==(MyString &other); // 重载 == 运算符
|
||||
MyString &operator==(const char *other); // 重载 == 运算符
|
||||
|
||||
// 读取字符串中 index 位置的字符
|
||||
char operator[](int index); // 重载 [] 运算符
|
||||
|
||||
private:
|
||||
char *mStr; // 字符串
|
||||
int mSize; // 字符串长度
|
||||
};
|
||||
|
||||
ostream &operator<<(ostream &cout, const MyString &str);
|
||||
istream &operator<<(istream &cout, MyString &str);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user