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

C++ day4 练习

一、练习1

        找到第一天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();
	mystring(const char* str);
	~mystring();
	void copy(const mystring& r);
	void show();
	void append(const mystring& r);
	bool compare(const mystring& r);
	void swap(mystring& r);
	// 再写一个函数,要求实现将 mystrwing 转换成 const char*
	const char* data();
	friend mystring operator+(const mystring& l, const mystring& r);
	friend mystring& operator+= (const mystring& l, const mystring& r);
	friend char& operator[](const mystring& l, int index);
};

char& operator[](const mystring& l, int index){
	return l.p[index];
}

mystring operator+(const mystring& l, const mystring& r){
	mystring temp = l;
	temp.append(r);
	return temp;
}

mystring& operator+=(const mystring& l, const mystring& r){
	l = l + r;
	return l;
}

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

mystring::mystring(const char* str){
	// 计算str实际长度
	len = strlen(str);
	// 根据str实际长度,申请对应大小的堆空间
	p = (char*)calloc(1,len+1);
	// 将str拷贝到堆空间里面去
	strcpy(p,str);
}

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

// 其实就是 p 的 set 接口
void mystring::copy(const mystring& r){
	if(p != NULL){
		free(p);
	}
	len = r.len;
	p = (char*)calloc(1,len+1);
	strcpy(p,r.p);
}

// 其实就是 p 的 get 接口
const char* mystring::data(){
	return p;
}

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

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

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

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

int main(int argc, const char** argv){
	mystring str = "hello";
	printf("str = %s\n", str.data());

	mystring ptr;
	ptr.copy("你好");
	ptr.show();

	ptr.append("世界");
	ptr.show();

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


二、练习2

        封装消息队列

        class Msg{

                key_t key;

                int id;

                int channel

        }

        实现以下功能:

        Msg m("文件名");

        m[1].send("数据");  // 将数据发送到1号频道中

        string str = m[1].recv(int size);  // 从1号频道中读取消息,并且返回;

        编写程序测试

【代码】:

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

using namespace std;

class Msg{
private:
	key_t key;
	int id;
	int channel;
	struct msgbuf{
		long channel;
		char text[512];
	};
public:
	Msg(const string& filename = ""){
		key = ftok(filename.data(),1);
		id = msgget(key,IPC_CREAT | 0666);
	}

	~Msg(){
		msgctl(id,IPC_RMID,0);
	}

	void send(const string& str){
		msgbuf buf = {0};	
		strcpy(buf.text,str.data());
		buf.channel = channel;
		msgsnd(id,&buf,str.length,0);
	}

	string recv(int size=512){
		msgbuf buf = {0};
		msgrecv(id,&buf,size,channel,0);
		string str = buf.text;
		return str;
	}

	friend Msg operator[](const Msg& l,int channel);
};

// m[1].send(str);
Msg& operator[](const Msg& l,int channel){
	l.channel = channel;
	return l;
}


int main(int argc,const char** argv){

}


三、练习3

        封装信号灯集

        class Sem{

                key_t key;

                int id;

                int index;

        }

        实现以下功能:

        Sem s(参数x,参数y);  // 创建信号灯集,信号灯集中存在 x 个信号量,并且将所有信号量初始化为 y;

        s[1].init(10);  // 手动初始化信号灯集中的第1个信号量,初始化成 10;

        s[1] + 1;  // 让信号灯集中的第1个信号量的值 +1;

        s[1].operator+(1);

        s[1] - 1;  // 让信号灯集中的第1个信号量的值 -1;

        编写程序测试。

【代码】:

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

using namespace std;

class Sem{
private:
	key_t key;
	int id;
	int index;
public:
	Sem(const string& filename = "",int n,int val){
		key = ftok(filename.data());
		id = semget(key,n,IPC_CREAT | 0666);
		for(int i=0;i<n;i++){
			semctl(id,i,SETVAL,val);
		}
	}

	~Sem(){
		semctl(id,0,IPC_RMID);
	}

	friend Sem& operator+(const Sem& l,int val);
	friend Sem& operator-(const Sem& l,int val);
	friend Sem operator[](const Sem& l,int index);
};


// Sem s
// s + 1解锁
// s - 1 上锁
// s + 1 + 1 + 1 - 2 - 3
// int(4) + 3
Sem& operator+(const Sem& l,int val){
	sembuf buf = {0};
	buf.sem_num = l.index;
	buf.sem_op = abs(val);
	buf.sem_flg = SEM_UNDO;
	semop(id,&buf,1);
	return l;
}


/*
	Sem s;
	s[0] - 1  s.index = 0确定好了
*/

Sem& operator-(const Sem& l,int val){
    sembuf buf = {0};
    buf.sem_num = l.index;
    buf.sem_op = -abs(val);
    buf.sem_flg = SEM_UNDO;
    semop(id,&buf,1); 
	return l;                    
}


Sem& operator[](const Sem& l,int index){
	l.index = index;
	return l;
}

int main(int argc,const char** argv){

}



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

相关文章:

  • 抖音视频如何下载保存去水印
  • 本地大模型编程实战(23)用智能体(Agent)实现基于SQL数据构建问答系统(2)
  • kubernetes中pod spec设置的limits request会转换为哪些参数进行限制
  • [杂学笔记]OSI七层模型作用、HTTP协议中的各种方法、HTTP的头部字段、TLS握手、指针与引用的使用场景、零拷贝技术
  • 形式化数学编程在AI医疗中的探索路径分析
  • 爬虫运行后如何保存数据?
  • 【redis】数据类型之hyperloglog
  • 利用Java爬虫获取VIP商品详情实战案例指南
  • 如何使用深度学习进行手写数字识别(MNIST)
  • 基于Prometheus与Grafana构建实时监控与告警体系,保障微服务稳定性!
  • python如何去除列表末尾的None
  • Nmap网络安全审计
  • 超多目标优化:基于导航变量的多目标粒子群优化算法(NMOPSO)的无人机三维路径规划,MATLAB代码
  • https和http有什么区别
  • kotlin 知识点 七 泛型的高级特性
  • 【gitlab】认识 持续集成与部署
  • Java 值传递
  • python实现基于文心一言大模型的sql小工具
  • Unity Shader Graph 2D - Procedural程序化图形循环加载进度效果
  • 从零开始:使用PyTorch构建DeepSeek R1模型及其训练详解