C++ String简易实现

这是之前学习C++时,书中要求自己实现一份简易String,然后就实现了一份....下面是部分功能代码,提供给需要的人参考下,如果有什么问题也可以下方评论~

“kString.h”

#ifndef KSTRING_H_
#define KSTRING_H_
#include <iostream>

class kString{
private:
    char* str;
    int Len;
public:
    //构造函数和析构函数
    kString();
    kString(const char* str_t);
    kString(const kString& t);
    kString& operator=(const kString& t);
    ~kString();

    int kSize();
    char& operator[](int n);
    char operator[](int n)const;

    //关系运算符重载
    friend bool operator>(const kString& s1,const kString& s2);
    friend bool operator<(const kString& s1,const kString& s2);
    friend bool operator==(const kString& s1,const kString& s2);
    friend bool operator>=(const kString& s1,const kString& s2);
    friend bool operator<=(const kString& s1,const kString& s2);
    friend std::ostream& operator<<(std::ostream& out_t, const kString& t);
};
#endif
“kString.cpp”

#include "kString.h"
#include <iostream>

//默认构造函数 创建空String
kString::kString(){
    str = NULL;
    Len = 0;
}

kString::kString(const char* str_t){
    Len = strlen(str_t);
    str = new char[Len+1];
    strcpy(str,str_t);
}

kString::kString(const kString& t){
    Len = t.Len;
    str = new char[Len+1];
    strcpy(str,t.str);
}

//虽然加入证同测试的赋值操作 但是该函数并不具备异常安全性
kString& kString::operator=(const kString& t){
    if(this==&t){       //证同测试
        return *this;
    }else{
        delete []str;
        Len = t.Len;
        str = new char[Len+1];    //这里可能会引发异常(std::bad_alloc)导致类数据败坏
        strcpy(str,t.str);
        return *this;
    }
}

/*
//既有证同测试也增加了异常安全性
kString& kString::operator=(const kString& t){
    if(this==&t){
        return *this;
    }else{
        char* str_t = str;      //用作临时存储 当new正常执行没发生异常 再将旧指针所指内存释放掉
        Len = t.Len;
        str = new char[Len+1];
        strcpy(str,t.str);
        delete str_t;
        return *this;
    }
}
*/

//析构函数 用于释放指向的字符串所在内存
kString::~kString(){
    delete[]str;
}

//返回当前String字符数
int kString::kSize(){
    return Len;
}

//重载<< 以便可以直接对kString对象进行输出操作
std::ostream& operator<<(std::ostream& out_t, const kString& t){
    out_t << t.str;
    return out_t;
}

//non-const版
char& kString::operator[](int n){
    return str[n];
}

//
//const char& kString::operator[](int n)const{
//    return str[n];
//}

//const版 用于const对象时调用
char kString::operator[](int n)const{
    return str[n];
}


//以下是对关系运算符重载 以便可以直接对该kString对象进行比较
bool operator>(const kString& s1,const kString& s2){
    if(strcmp(s1.str,s2.str)>0){
        return true;
    }else{
        return false;
    }
}

bool operator<(const kString& s1,const kString& s2){
    if(strcmp(s1.str,s2.str)<0){
        return true;
    }else{
        return false;
    }
}

bool operator==(const kString& s1,const kString& s2){
    if(strcmp(s1.str,s2.str)==0){
        return true;
    }else{
        return false;
    }
}

bool operator>=(const kString& s1,const kString& s2){
    if(strcmp(s1.str,s2.str)>0 || strcmp(s1.str,s2.str)==0){
        return true;
    }else{
        return false;
    }
}

bool operator<=(const kString& s1,const kString& s2){
    if(strcmp(s1.str,s2.str)<0 || strcmp(s1.str,s2.str)==0){
        return true;
    }else{
        return false;
    }
}
测试代码就不帖上来了,测试功能都正常~因为是简易版与C++String相比还是有很多功能没有去实现,有兴趣的可以加以完善 添加其他相应功能~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是C++双栈实现简易计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> numStack; // 存储数字的栈 stack<char> opStack; // 存储运算符的栈 string expr; // 表达式字符串 cin >> expr; int len = expr.length(); for (int i = 0; i < len; i++) { char ch = expr[i]; if (ch >= '0' && ch <= '9') { // 如果是数字,直接入栈 numStack.push(ch - '0'); } else { // 如果是运算符 if (opStack.empty() || ch == '(') { // 如果运算符栈为空或者当前运算符是左括号,直接入栈 opStack.push(ch); } else if (ch == ')') { // 如果当前运算符是右括号,弹出运算符栈中的元素直到遇到左括号 while (opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int res; if (op == '+') { res = num1 + num2; } else if (op == '-') { res = num1 - num2; } else if (op == '*') { res = num1 * num2; } else { res = num1 / num2; } numStack.push(res); } opStack.pop(); // 弹出左括号 } else { // 如果当前运算符的优先级小于等于栈顶运算符的优先级,弹出栈顶元素进行运算 while (!opStack.empty() && opStack.top() != '(' && ((ch == '*' || ch == '/') || (opStack.top() == '+' || opStack.top() == '-'))) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int res; if (op == '+') { res = num1 + num2; } else if (op == '-') { res = num1 - num2; } else if (op == '*') { res = num1 * num2; } else { res = num1 / num2; } numStack.push(res); } opStack.push(ch); // 将当前运算符入栈 } } } while (!opStack.empty()) { // 处理剩余的运算符 int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int res; if (op == '+') { res = num1 + num2; } else if (op == '-') { res = num1 - num2; } else if (op == '*') { res = num1 * num2; } else { res = num1 / num2; } numStack.push(res); } cout << numStack.top() << endl; // 输出最终结果 return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值