2.24作业

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
 
using namespace std;                 
 
class AB{
private:
	int a;
	int b;
public:
	AB(int a=0,int b=0){}
	void seta(int l){a=l;}
	void setb(int l){b=l;}
	int geta(){return a;}
	int getb(){return b;}
};
 
class AA:public AB{
	
	public:
		AA(int ab=0,int bc=0)
		:AB(ab,bc){}
		void setA(int a)
		{
			seta(a);
			setb(a);
		}
		void setB(int a)
		{
			seta(a);
			setb(a);
		}
 
		void show()
		{
			if(geta()==getb())
			{
				cout<<"是正方形"<<endl;
			}else
			{
				cout<<"是长方形"<<endl;
			}
		}
};
 
int main(int argc,const char** argv){
	
	AA aa;
	aa.setA(4);
	aa.setB(5);
	aa.show();
	return 0;
}
 
cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
 
using namespace std;                 
 
class ABC{
private:
	int a;
	int b;
	int c;
public:
	ABC(int a=0,int b=0,int c=0){}
	void seta(int l){a=l;}
	void setb(int l){b=l;}
	void setc(int l){c=l;}
	int geta(){return a;}
	int getb(){return b;}
	int getc(){return c;}
};
 
 
class AAB:public ABC{
	public:
		AAB(int ab=0,int bc=0,int cd=0)
		:ABC(ab,bc,cd){}
		void setA(int a)
		{
			seta(a);
			setb(a);
		}
		void setB(int a)
		{
			seta(a);
			setb(a);
		}
		void setC(int a)
		{
			seta(a);
			setb(a);
		}
	};
 
 
class AAA:public AAB{
	public:
		AAA(int ab=0,int bc=0,int cd=0)
		:AAB(ab,bc,cd){}
		void setA(int a)
		{
			seta(a);
			setb(a);
			setc(a);
		}
		void setB(int a)
		{
			seta(a);
			setb(a);
			setc(a);
		}
		void setC(int a)
		{
			seta(a);
			setb(a);
			setc(a);
		}
 
		void show()
		{
			cout<<geta()<<endl<<getb()<<endl<<getc()<<endl;
 
			if(geta()==getb()&&geta()==getc())
			{
				cout<<"是等边三角形"<<endl;
			}else
			{
				cout<<"不是等边三角形"<<endl;
			}
		}
};
 
int main(int argc,const char** argv){
	
	AAA aaa;
	aaa.AAA::setA(3);
	aaa.AAA::setB(4);
	aaa.AAA::setC(5);
	aaa.AAA::show();
	return 0;
}
 

3.queue

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <sys/ipc.h>
#include <sys/msg.h>
 
using namespace std; 
 
struct MessageBuf {
    long mtype;     
    char mtext[128];
};
 
class Msg{
private:
	long channel;
	MessageBuf buf;
	key_t key;
	int id;
public:
	Msg(const char* filename);
	Msg& operator[](int c);
//	void send(const char* txt);
	friend void operator<<(Msg& buf,const char* txt);
};
 
 
Msg::Msg(const char* filename)
{
	key=ftok(filename,1);
	id=msgget(key,IPC_CREAT|0666);
	memset(&buf,0,128);
}
 
 
Msg& Msg::operator[](int c)
{
	channel=c;	
	return *this;
}
 
void operator<<( Msg& m,const char* txt)
{
	m.buf.mtype=m.channel;
	strcpy(m.buf.mtext,txt);
	msgsnd(m.id,&m.buf,sizeof(m.buf.mtext),IPC_NOWAIT);
}
 
 
int main(int argc,const char** argv){
 
while(1)
{
	Msg m("ipc");
	long channel;
	cout<<"请选择频道号: ";
	cin>>channel;
	m[channel]<<"hello world";
}
	return 0;
}

queue.read

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <sys/ipc.h>
#include <sys/msg.h>
 
using namespace std;                
 
struct MessageBuf {
    long mtype;     
    char mtext[128];
};
 
class Msg{
private:
	long channel;
	MessageBuf buf;
	key_t key;
	int id;
public:
	Msg(const char* filename);
	Msg& operator[](int c);
	friend Msg& operator>>(Msg& m,string& s);
};
 
 
Msg::Msg(const char* filename)
{
	key=ftok(filename,1);
	id=msgget(key,IPC_CREAT|0666);
	memset(&buf,0,128);
}
 
 
Msg& Msg::operator[](int c)
{
	channel=c;	
	return *this;
}
 
 
Msg& operator>>(Msg& m,string& s)
{
	
	m.buf.mtype=m.channel;
	msgrcv(m.id,&m.buf,sizeof(m.buf.mtext),m.channel,IPC_NOWAIT);
	s=m.buf.mtext;
	return m;
}
 
int main(int argc,const char** argv){
while(1)
{
	Msg m("ipc");
	long channel;
	cout<<"请选择频道号: ";
	cin>>channel;
	string str;
	m[channel]>>str;
	cout<<str<<endl;
}
	
}
cpp 复制代码
lude <sys/ipc.h>
#include <semaphore.h>
#include <sys/sem.h>
 
using namespace std;
 
class Sem{
private:
	key_t key;
	int id;
	int index;
public:
	Sem(const char* filename,int n,int val){
		key = ftok(filename,1);
		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+(Sem& l,int val);
	friend Sem& operator-(Sem& l,int val);
	Sem& operator[](int index);
 
	Sem& operator++(int);
	Sem& operator--(int);
};
 
 
// Sem s
// s + 1解锁
// s - 1 上锁
// s + 1 + 1 + 1 - 2 - 3
// int(4) + 3
Sem& operator+(Sem& l,int val){
	sembuf buf = {0};
	buf.sem_num = l.index;
	buf.sem_op = abs(val);
	buf.sem_flg = SEM_UNDO;
	semop(l.id,&buf,1);
	return l;
}
 
Sem& Sem::operator++(int)
{
	sembuf buf = {0};
	buf.sem_num =this->index;
	buf.sem_op = 1;
	buf.sem_flg = SEM_UNDO;
	semop(this->id,&buf,1);
	return *this;
}
 
 
 
/*
	Sem s;
	s[0] - 1  s.index = 0确定好了
*/
 
Sem& operator-( Sem& l,int val){
    sembuf buf = {0};
    buf.sem_num = l.index;
    buf.sem_op = -abs(val);
    buf.sem_flg = SEM_UNDO;
    semop(l.id,&buf,1); 
	return l;                    
}
 
 
Sem& Sem::operator--(int)
{
	sembuf buf = {0};
    buf.sem_num = this->index;
    buf.sem_op = -1;
    buf.sem_flg = SEM_UNDO;
    semop(this->id,&buf,1); 
	return *this; 
}
 
 
Sem& Sem::operator[](int index){
	this->index = index;
	return *this;
}
相关推荐
tainshuai2 小时前
用 KNN 算法解锁分类的奥秘:从电影类型到鸢尾花开
算法·分类·数据挖掘
Coovally AI模型快速验证8 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
pusue_the_sun8 小时前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
RaymondZhao348 小时前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
zhangfeng11339 小时前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类
啊阿狸不会拉杆9 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路10 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
你知道网上冲浪吗11 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
地平线开发者12 小时前
征程 6 | PTQ 精度调优辅助代码,总有你用得上的
算法·自动驾驶
Tisfy12 小时前
LeetCode 837.新 21 点:动态规划+滑动窗口
数学·算法·leetcode·动态规划·dp·滑动窗口·概率