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

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){

}


相关文章:

  • SQL: DDL,DML,DCL,DTL,TCL,
  • 2.24力扣每日一题--设计有序流
  • 【Microsoft® PowerPoint for Mac】MAC一键导出PPT备注
  • 能不能用Ai来开发出一款APP?很早就想过能不能用Ai来开发出一款APP?
  • 89.迷人子序列计数问题|Marscode AI刷题
  • vue2中,打包报错ERROR in /node_modlules/@types/lodash/common/common.d.ts 26
  • python全栈-并发和网络通信
  • GO 快速升级Go版本
  • 【Qt之QQuickWidget】QML嵌入QWidget中
  • c++day4
  • 【嵌入式Linux应用开发基础】网络编程(1):TCP/IP协议栈
  • WIN10 本地部署 BGE Embedding 向量化模型
  • unxi-进程间通信
  • 使用PHP接入纯真IP库:实现IP地址地理位置查询
  • akka现有的分布式定时任务框架总结
  • 条件渲染
  • .Net 9下使用Tensorflow.net---DNN_Keras
  • AI时代前端开发技能变革与ScriptEcho:拥抱AI,提升效率
  • MongoDB 复制(副本集)
  • Uncaught TypeError: Module._malloc is not a function
  • 广西落马官员家中发现大量金砖?官方辟谣
  • 日本农林水产大臣因不当“大米言论”引咎辞职
  • 国家发改委:进一步完善促进民营经济发展的制度机制
  • 山西资深公益人士孙超因突发急病离世,终年37岁
  • 陕西籍青年作家卜文哲爬山时发生意外离世,终年28岁
  • 国家发改委谈整治“内卷式”竞争:加力破除地方保护和市场分割,遏制落后产能无序扩张