作业day5

news/2025/2/25 18:17:30

封装一个mystring类 拥有私有成员: char* p int len 需要让以下代码编译通过,并实现对应功能 mystring str = "hello" mystring ptr; ptr.copy(str) ptr.append(str) ptr.show() 输出ptr代表的字符串 ptr.compare(str) 比较ptr和str是否一样 ptr.swap(str) 交换ptr 和 str的内容 实现以下功能 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 mystring {
private:
    char* p;
    int len;

public:
    mystring(const char* str) {
        len = strlen(str);
        p = new char[len + 1];
        strcpy(p, str);
    }

    mystring() {
        p = NULL;
        len = 0;
    }

    ~mystring() {
        if (p != NULL) {
            delete[] p;
        }
    }

    void copy(const char* r) {
        if (p != NULL) {
            delete[] p;
        }
        len = strlen(r);
        p = new char[len + 1];
        strcpy(p, r);
    }

    const char* data() {
        return p;
    }

    void show() {
        cout << p << endl;
    }

    void append(const mystring& r) {
        len = len + r.len;
        char* back = p;
        p = new char[len + 1];
        strcpy(p, back);
        strcat(p, r.p);
        delete[] back;
    }

    bool compare(const mystring& r) {
        return strcmp(p, r.p) == 0;
    }

    void swap(mystring& r) {
        char* temp = p;
        p = r.p;
        r.p = temp;
    }
};

int main(int argc, const char** argv) {
    mystring str = "hello"; // 单参数构造函数
    mystring ptr;           // 默认构造函数

    ptr.copy(str.data());   // 复制 str 到 ptr
    ptr.append(str);        // 将 str 追加到 ptr
    ptr.show();             // 输出 ptr 的内容

    if (ptr.compare(str)) {
        cout << "ptr and str are the same." << endl;
    } else {
        cout << "ptr and str are different." << endl;
    }

    ptr.swap(str);          // 交换 ptr 和 str 的内容
    ptr.show();             // 输出 ptr 的内容
    str.show();             // 输出 str 的内容

    return 0;
}
#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

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

public:
    // 构造函数
    mystring(const char* str = nullptr) {
        if (str) {
            len = strlen(str);
            p = new char[len + 1];
            strcpy(p, str);
        } else {
            p = nullptr;
            len = 0;
        }
    }

    // 析构函数
    ~mystring() {
        if (p) {
            delete[] p;
        }
    }

    // 赋值运算符重载
    mystring& operator=(const mystring& other) {
        if (this != &other) {
            if (p) {
                delete[] p;
            }
            len = other.len;
            p = new char[len + 1];
            strcpy(p, other.p);
        }
        return *this;
    }

    // + 运算符重载
    mystring operator+(const mystring& other) const {
        mystring result;
        result.len = len + other.len;
        result.p = new char[result.len + 1];
        strcpy(result.p, p);
        strcat(result.p, other.p);
        return result;
    }

    // += 运算符重载
    mystring& operator+=(const mystring& other) {
        char* temp = p;
        len += other.len;
        p = new char[len + 1];
        strcpy(p, temp);
        strcat(p, other.p);
        delete[] temp;
        return *this;
    }

    // [] 运算符重载
    char& operator[](size_t index) {
        if (index >= len) {
            throw out_of_range("Index out of range");
        }
        return p[index];
    }

   
    const char& operator[](size_t index) const {
        if (index >= len) {
            throw out_of_range("Index out of range");
        }
        return p[index];
    }

    const char* data() const {
        return p;
    }

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

int main() {
    mystring str = "hello";
    mystring ptr = "world";

    // 测试 + 运算符
    str = str + ptr;
    str.show();  // 输出 "helloworld"

    // 测试 += 运算符
    str += ptr;
    str.show();  // 输出 "helloworldworld"

    // 测试 [] 运算符
    str[0] = 'H';
    str.show();  // 输出 "Helloworldworld"

    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 <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <stdexcept>

using namespace std;

// 消息结构体
struct Message {
    long mtype;       // 消息类型(频道号)
    char mtext[512];  // 消息内容
};

// Msg 类封装
class Msg {
private:
    key_t key;   // 消息队列的键值
    int msgid;   // 消息队列的 ID

    // 创建或获取消息队列
    void createQueue(const string& filename) {
        key = ftok(filename.c_str(), 'a');
        if (key == -1) {
            throw runtime_error("Failed to generate key");
        }

        msgid = msgget(key, 0666 | IPC_CREAT);
        if (msgid == -1) {
            throw runtime_error("Failed to create/get message queue");
        }
    }

public:
    // 构造函数
    Msg(const string& filename) {
        createQueue(filename);
    }

    // 析构函数
    ~Msg() {
        // 删除消息队列(可选,也可以手动删除)
        msgctl(msgid, IPC_RMID, nullptr);
    }

    // 发送消息到指定频道
    void send(int channel, const string& data) {
        Message msg;
        msg.mtype = channel;
        strncpy(msg.mtext, data.c_str(), sizeof(msg.mtext) - 1);
        msg.mtext[sizeof(msg.mtext) - 1] = '\0';

        if (msgsnd(msgid, &msg, sizeof(msg.mtext), 0) == -1) {
            throw runtime_error("Failed to send message");
        }
    }

    // 从指定频道读取消息
    string read(int channel, int size) {
        Message msg;
        if (msgrcv(msgid, &msg, size, channel, 0) == -1) {
            throw runtime_error("Failed to receive message");
        }
        return string(msg.mtext);
    }

    // 重载 [] 运算符,返回一个代理对象
    struct ChannelProxy {
        Msg& msg;
        int channel;

        ChannelProxy(Msg& msg, int channel) : msg(msg), channel(channel) {}

        void send(const string& data) {
            msg.send(channel, data);
        }

        string read(int size) {
            return msg.read(channel, size);
        }
    };

    ChannelProxy operator[](int channel) {
        return ChannelProxy(*this, channel);
    }
};

// 测试程序
int main() {
    try {
        // 创建消息队列
        Msg m("testfile");

        // 发送消息到1号频道
        m[1].send("Hello from channel 1");

        // 从1号频道读取消息
        string received = m[1].read(512);
        cout << "Received: " << received << endl;

        // 发送消息到2号频道
        m[2].send("Hello from channel 2");

        // 从2号频道读取消息
        received = m[2].read(512);
        cout << "Received: " << received << endl;
    } catch (const exception& e) {
        cerr << "Error: " << e.what() << endl;
        return 1;
    }

    return 0;
}

封装信号灯集 class Sem{ key_t key int id; int index } 实现以下功能 Sem s(参数x,参数y):创建信号灯集,信号灯集中存在 x 个信号量,并且将所有信号量初始化为 y s.init[1](10):手动初始化信号灯集中的第1个信号量,初始化成 10 s[1] + 1 让信号灯集中的第1个信号量的值 +1 s[1] - 1 让信号灯集中的第1个信号量的值 -1 编写程序测试

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
#include <stdexcept>
#include <vector>

using namespace std;
class Sem {
private:
    key_t key;
    int id;
    int index;


// 创建信号量集
    void create()
 semun sem_union; // 初始化所有信号量
};


http://www.niftyadmin.cn/n/5865819.html

相关文章

【关于seisimic unix中使用suedit指令无法保存问题】

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、如何修改头文件二、出现的问题尝试解决使用ls显示文件属性使用chmod修改文件属性 总结 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff…

【行业解决方案篇二】【当图神经网络成为“金融侦探”:DeepSeek反洗钱系统技术全解】

一、为什么传统反洗钱系统像“拿着渔网捞针”? 金融犯罪每年造成全球8万亿美元损失,而传统规则引擎存在三大致命伤: 规则滞后:依赖人工设定的固定阈值(如单日转账>50万触发警报),黑产通过“化整为零”轻松绕过关联缺失:仅分析单笔交易,无法识别多层嵌套的“资金迷…

20250223下载并制作RTX2080Ti显卡的显存的测试工具mats

20250223下载并制作RTX2080Ti显卡的显存的测试工具mats 2025/2/23 23:23 缘起&#xff1a;我使用X99的主板&#xff0c;使用二手的RTX2080Ti显卡【显存22GB版本&#xff0c;准备学习AI的】 但是半年后发现看大码率的视频容易花屏&#xff0c;最初以为是WIN10经常更换显卡/来回更…

19、《Springboot+MongoDB整合:玩转文档型数据库》

SpringbootMongoDB整合&#xff1a;玩转文档型数据库 摘要&#xff1a;本文全面讲解Spring Boot与MongoDB的整合实践&#xff0c;涵盖环境搭建、CRUD操作、聚合查询、事务管理、性能优化等核心内容。通过15个典型代码示例&#xff0c;演示如何高效操作文档数据库&#xff0c;深…

计算机毕业设计程序,定制开发服务

我们擅长的开发语言包括Python、C、Golang、Java&#xff0c;以及前端技术如HTML、CSS、JS和Vue。我们的服务内容丰富&#xff0c;能够满足您各种需求&#xff0c;无论是简单的功能开发还是复杂的定制项目&#xff0c;我们都能为您提供专业支持。若有具体需求可联系作者。 链接…

SQL:DQL数据查询语言以及系统函数(oracle)

SQL Structured Query Language&#xff0c;结构化查询语言, 是一种用于管理和操作关系数据库的标准编程语言。 sql的分类 DQL&#xff08;Data Query Language&#xff09;&#xff1a;数据查询语言 DDL&#xff08;Data Definition Language&#xff09;&#xff1a;数据…

12. 三昧真火焚环劫 - 环形链表检测(快慢指针)

哪吒在数据修仙界中继续他的修炼之旅。这一次,他来到了一片神秘的环形山脉,山脉中有一条蜿蜒的火龙,它象征着环形链表。山脉的入口处有一块巨大的石碑,上面刻着一行文字:“欲破此山,需以三昧真火之力,焚环劫之链,快慢指针定环踪。” 哪吒定睛一看,石碑上还有一行小字…

解决idea一个非常坑的问题

dea中经常会遇到这样问题&#xff0c;明明maven的pom中已经添加了依赖&#xff0c;总是提示jar包找不到&#xff0c; 于是双击clean &#xff0c;或者 点击 reload maven &#xff0c;都是不好使。如 javax.crypto.spec.IvParameterSpec;这个包&#xff0c;竟然飘红了。我clean…