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

0221作业

找到第一天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 mystring{
private:
 	char* p;
	int len;
public:
	mystring(const char* s="")
	{
		len=strlen(s);
		if(len!=0){
			p=(char*)calloc(1,len+1);
			strcpy(p,s);
		}else{
			p=NULL;
		}
			cout<<"构造函数"<<endl;
		
	}

	~mystring()
	{
		free(p);
	}
	
	void show()
	{
		cout<<p<<endl;
	}

friend mystring operator+(const mystring& l,const mystring& r);
friend mystring& operator=(mystring& l,mystring& r);
friend mystring& operator+=(mystring& l,mystring& r);
friend char& operator[](mystring& l,int insert);

};

mystring operator+(mystring& l,mystring& r){
		mystring temp;
		temp.len=l.len+r.len;
		temp.p=(char*)calloc(1,temp.len+1);
		strcpy(temp.p,l.p);
		strcat(temp.p,r.p);
		return temp;
	}

mystring& operator=(mystring& l,mystring& r){
	free(l.p);
	l.p=(char*)calloc(1,r.len+1);
	l.len=r.len;
	strcpy(l.p,r.p);
	return l;
}

mystring& operator+=(mystring& l,mystring& r){
	char *temp=l.p;
	l.len=l.len+r.len;
	l.p=(char*)calloc(1,l.len+1);
	strcpy(l.p,temp);
	strcat(l.p,r.p);
	free(temp);
	return l;
}

char& operator[](mystring& l,int insert){
	return l.p[insert];
}
		
int main(int argc,const char** argv){
	
	mystring str="hello";
	mystring ptr="world";
	str=str+ptr;
	str+=ptr;
	str[0]='H';
	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 <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

using namespace std;

class Msg {
private:
    key_t key;
    int id;
    vector<queue<string>> channels;  // 每个频道的消息队列

public:
    Msg(const string& filename) {
        // 创建消息队列的键值
        key = ftok(filename.c_str(), 'a');
        if (key == -1) {
            perror("ftok");
            exit(1);
        }

        // 创建消息队列
        id = msgget(key, 0666 | IPC_CREAT);
        if (id == -1) {
            perror("msgget");
            exit(1);
        }

        // 初始化频道
        channels.resize(10);  // 假设最多支持10个频道
    }

    ~Msg() {
        // 删除消息队列
        msgctl(id, IPC_RMID, nullptr);
    }

    // 下标运算符,返回特定频道
    class Channel& operator[](int index) {
        return channels[index];
    }

    class Channel {
    private:
        Msg& msg;
        int channel_id;

    public:
        Channel(Msg& m, int id) : msg(m), channel_id(id) {}

        void send(const string& data) {
            // 将消息发送到指定频道
            channels[channel_id].push(data);
        }

        string read(int size) {
            // 从指定频道读取消息
            if (channels[channel_id].empty()) {
                return "";  // 如果队列为空,返回空字符串
            }
            string data = channels[channel_id].front();
            channels[channel_id].pop();
            return data.substr(0, size);  // 返回指定大小的消息
        }
    };

    // 返回特定频道的代理对象
    Channel& getChannel(int index) {
        return channels[index];
    }
};

int main() {
    Msg m("testfile");

    // 发送消息到1号频道
    m.getChannel(1).send("Hello, World!");

    // 从1号频道读取消息
    string str = m.getChannel(1).read(100);
    cout << "Received: " << str << endl;

    return 0;
}

相关文章:

  • leetcode 题目解析 第3题 无重复字符的最长子串
  • go 环境准备
  • cadence报错解决1
  • 光明谷推出AT指令版本的蓝牙音箱SOC 开启便捷智能音频开发新体验
  • at32f403a rt thread led基础bsp工程测试
  • 黑神话悟空火焰山攻略来了
  • 041集——封装之:新建图层(CAD—C#二次开发入门)
  • 动态订阅kafka mq实现(消费者组动态上下线)
  • 代码随想录-训练营-day35
  • 基于ffmpeg+openGL ES实现的视频编辑工具-添加转场(九)
  • C语言进阶习题【3】(7预处理)——写一个宏计算结构体变量相对于首地址的偏移
  • 先进制造aps专题三十 用免费生产排程软件isuperaps进行长期生产计划制定
  • 计算机图形学:实验环境配置
  • 基于Matlab实现串口实时显示波形GUI界面(源码)
  • Linux 驱动入门(6)—— IRDA(红外遥控模块)驱动
  • 代码随想录算法训练营day40(补0208)
  • “死”循环(查漏补缺)
  • 055 SpringCache
  • cs106x-lecture14(Autumn 2017)-SPL实现
  • 【Java进阶学习 第五篇】JDK8、9中的接口新特性
  • 上海国际碳中和博览会下月举办,首次打造民营经济专区
  • 中国物流集团等10家央企11名领导人员职务任免
  • “AD365特应性皮炎疾病教育项目”启动,助力提升认知与规范诊疗
  • 车建兴被留置:跌落的前常州首富和红星系重整迷路
  • 商务部回应美方加严限制中国芯片:敦促美方立即纠正错误做法
  • 丹麦外交大臣拉斯穆森将访华