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

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;
}

相关文章:

  • 第5章 软件工程(二)
  • Crack SmartGit
  • vue3中Watch和WatchEffect的用法和区别
  • 调用DeepSeek API 增强版纯前端实现方案,支持文件上传和内容解析功能
  • sam2 windows 编译安装
  • springboot单机支持1w并发,需要做哪些优化
  • 国产编辑器EverEdit - 如何在EverEdit中创建工程?
  • 使用Uni-app实现语音视频聊天(Android、iOS)
  • 漏洞文字版表述一句话版本(漏洞危害以及修复建议),通常用于漏洞通报中简洁干练【持续更新中】
  • JavaWeb-在idea中配置Servlet项目
  • Linux下网络运维命令总结
  • Linux设备驱动开发-SPI驱动开发详解(包含设备树处理详细过程)
  • AR技术下的电商:虚拟试穿/试用/试戴成新风尚
  • Linux系统需要学习的内容和学习计划
  • 网络运维学习笔记(DeepSeek优化版)001网工初级(HCIA-Datacom与CCNA-EI)网络架构与通信原理
  • touchgfx的工作机制
  • Redis基础认知
  • 第四篇:微信小程序网络请求与API调用:实现数据交互
  • Spring Boot 中为什么 需要限流、降级和熔断?
  • 用js表述注意力机制
  • 长春建站模板源码/腾讯企点官网下载
  • 河南龙王建设集团网站/海洋seo
  • html做网站收藏按钮/seo推广灰色词
  • 东莞网站开发建设/seo是什么意思中文
  • 上海高端网站建设公司/长沙seo步骤
  • 乐从建网站/西安关键词排名推广