当前位置: 首页 > article >正文

EX_25/2/22

找到第一天mystring练习,实现以下功能

mystring str = "hello"

mystring ptr = "world"

str = str + ptr;

str += ptr

str[0] = 'H'

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Data {
private:
    char* p;
    int len;

public:
    Data();
    Data(const char* str);
    Data(const Data& other);
    ~Data();
    void copy(const Data& str);
    void append(const Data& str);
    void show() const;
    bool compare(const Data& n) const;
    void swap(Data& n);

    Data operator+(const Data& other) const;
    Data& operator+=(const Data& other);
    char& operator[](int n);
    Data& operator=(const Data& other);
};

Data::Data() {
    p = NULL;
    len = 0;
}

Data::Data(const char* str) {
    len = strlen(str);
    p = (char*)malloc(len + 1);
    strcpy(p, str);
}

Data::Data(const Data& other) {
    len = other.len;
    p = (char*)malloc(len + 1);
    strcpy(p, other.p);
}

Data::~Data() {
    if (p != NULL) { free(p); }
}

void Data::copy(const Data& str) {
    if (p != NULL) { free(p); }
    len = str.len;
    p = (char*)malloc(len + 1);
    strcpy(p, str.p);
}

void Data::append(const Data& str) {
    len = len + str.len;
    char* backup = p;
    p = (char*)calloc(1, len + 1);
    strcpy(p, backup);
    strcat(p, str.p);
    free(backup);
}

void Data::show() const {
    cout << p << endl;
}

bool Data::compare(const Data& n) const {
    return strcmp(p, n.p) == 0;
}

void Data::swap(Data& n) {
    char* temp = p;
    p = n.p;
    n.p = temp;
}

Data Data::operator+(const Data& other) const {
    Data result;
    result.len = len + other.len;
    result.p = (char*)malloc(result.len + 1);
    strcpy(result.p, p);
    strcat(result.p, other.p);
    return result;
}

Data& Data::operator+=(const Data& other) {
    len += other.len;
    char* backup = p;
    p = (char*)calloc(1, len + 1);
    strcpy(p, backup);
    strcat(p, other.p);
    free(backup);
    return *this;
}

char& Data::operator[](int n) {
    return p[n];
}

Data& Data::operator=(const Data& other) {
    if (this == &other) return *this;
    if (p != NULL) { free(p); }
    len = other.len;
    p = (char*)malloc(len + 1);
    strcpy(p, other.p);
    return *this;
}

int main(int argc, const char** argv) {
    Data str = "hello";
    Data ptr = "world";

    Data combined = str + ptr;
    combined.show();

    str += ptr;
    str.show();

    str[0] = 'H';
    str.show();

    if (str.compare(ptr)) {
        cout << "str 和 ptr 一样" << endl;
    } else {
        cout << "str 和 ptr 不一样" << endl;
    }

    str.swap(ptr);
    str.show();
    ptr.show();

    return 0;
}

封装消息队列

class Msg{

key_t key

int id;

int channel }

实现以下功能

Msg m("文件名")

m[1].send("数据"),

将数据发送到1号频道中 string str = m[1].read(int size) 从1号频道中读取消息,并且返回

#include <sys/types.h>
#include <iostream>
#include <string>
#include <cstring>
#include <sys/ipc.h>
#include <sys/msg.h>


using namespace std;

class Msg {
public:
    Msg(const string& filename) {
        key = ftok(filename.data(), 'A');
        id = msgget(key, 0666 | IPC_CREAT);
    }

    void send(int n, const string& text) {
        msgbuf buf{n, {}};
        strncpy(buf.text, text.data(), sizeof(buf.text));
        msgsnd(id, &buf, strlen(buf.text) + 1, 0);
    }

    string read(int n) {
        msgbuf buf{};
        msgrcv(id, &buf, sizeof(buf.text), n, 0);
        return string(buf.text);
    }

private:
    key_t key;
    int id;

    struct msgbuf {
        long n;
        char text[1024];
    };
};

int main() {
    Msg m("msgfile");
    m.send(1, "hello, channel 1!");
    string msg = m.read(1);
    cout << msg << endl;
    return 0;
}


http://www.kler.cn/a/560987.html

相关文章:

  • 115 道 MySQL 面试题,从简单到深入!
  • 《一起打怪兽吧》——自制一款Python小游戏
  • 基于Spring Boot的健康医院门诊在线挂号系统设与实现(LW+源码+讲解)
  • 超详细:数据库的基本架构
  • HandBrake for Mac v1.9.2 视频压缩及格式转换 汉化版 支持M、Intel芯片
  • TLS与自签名证书的创建、作用、用到的工具等知识的介绍
  • 反向代理模块kfj
  • 实操解决Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错的问题
  • escape SQL中用法
  • 力扣-贪心-135 分发糖果
  • 如何加固织梦CMS安全,防webshell、防篡改、防劫持,提升DedeCMS漏洞防护能力
  • 将Ubuntu操作系统的安装源设置为阿里云
  • java23种设计模式-原型模式
  • 【网络编程】UDP协议
  • Spring Boot 项目中,JDK 动态代理和 CGLIB 动态代理的使用
  • 【无标题】PHP-get_definde_vars
  • Unity汽车笔记
  • 三七互娱游戏策划岗内推
  • angular新闻列表分页
  • nodejs npm install、npm run dev运行的坎坷之路