C++ day4 练习

news/2025/2/26 3:32:44

一、练习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.niftyadmin.cn/n/5867129.html

相关文章

PHP入门基础学习九(PHP使用手册)

web交互 当表单的method属性提交方式为POST时,浏览器发送POST请求 当表单的method属性提交方式为GET时,浏览器发送GET请求 一、web表单交互 当PHP收到来自浏览器提交的数据后,会自动保存到超全局变量中。 超全局变量是PHP预定义好的变量,可以在PHP脚本的任何位置使用。…

华为昇腾服务器(固件版本查询、驱动版本查询、CANN版本查询)

文章目录 1. **查看固件和驱动版本**2. **查看CANN版本**3. **其他辅助方法**注意事项 在华为昇腾服务器上查看固件、驱动和CANN版本的常用方法如下&#xff1a; 1. 查看固件和驱动版本 通过命令行工具 npu-smi 执行以下命令查看当前设备的固件&#xff08;Firmware&#xff0…

Pytorch深度学习教程_6_激活函数

欢迎来到《pytorch深度学习教程》系列的第六篇&#xff01;在前面的五篇中&#xff0c;我们已经介绍了Python、numpy及pytorch的基本使用&#xff0c;进行了梯度及神经网络的实践。今天&#xff0c;我们将深入理解激活函数并进行简单的实践学习 欢迎订阅专栏进行系统学习&…

算法-数据结构-图的构建(邻接矩阵表示)

数据定义 //邻接矩阵表示图 //1.无向图是对称的 //2.有权的把a,到b 对应的位置换成权的值/*** 无向图* A B* A 0 1* B 1 0*/ /*** 有向图* A B* A 0 1* B 0 0*/import java.util.ArrayList; import java.util.List;/*** 带权图* A B* A 0 1* B 0 0*/ p…

Android NDK基本开发流程

Android NDK&#xff08;Native Development Kit&#xff09;开发流程允许开发者使用C/C代码来开发Android应用的部分功能&#xff0c;通常用于性能敏感的场景&#xff0c;如游戏、图像处理等。以下是Android NDK开发的基本流程&#xff1a; 1. 环境准备 安装Android Studio&a…

LabVIEW不规则正弦波波峰波谷检测

在处理不规则正弦波信号时&#xff0c;准确检测波峰和波谷是分析和处理信号的关键任务。特别是在实验数据、传感器信号或其他非理想波形中&#xff0c;波峰和波谷的位置可以提供有价值的信息。然而&#xff0c;由于噪声干扰、信号畸变以及不规则性&#xff0c;波峰波谷的检测变…

Docker 搭建 Redis 数据库

Docker 搭建 Redis 数据库 前言一、准备工作二、创建 Redis 容器的目录结构三、启动 Redis 容器1. 通过 redis.conf 配置文件设置密码2. 通过 Docker 命令中的 requirepass 参数设置密码 四、Host 网络模式与 Port 映射模式五、检查 Redis 容器状态六、访问 Redis 服务总结 前言…

使用 AndroidNativeEmu 调用 JNI 函数

版权归作者所有&#xff0c;如有转发&#xff0c;请注明文章出处&#xff1a;https://cyrus-studio.github.io/blog/ AndroidNativeEmu AndroidNativeEmu 专为 Android 原生代码调试和模拟设计&#xff0c;特别关注 JNI 调用和 Android 环境。相比之下&#xff0c;Unicorn 是通…