一、练习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){
}