封装一个mystring类 拥有私有成员: char* p int len 需要让以下代码编译通过,并实现对应功能 mystring str = "hello" mystring ptr; ptr.copy(str) ptr.append(str) ptr.show() 输出ptr代表的字符串 ptr.compare(str) 比较ptr和str是否一样 ptr.swap(str) 交换ptr 和 str的内容 实现以下功能 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(const char* str) {len = strlen(str);p = new char[len + 1];strcpy(p, str);}mystring() {p = NULL;len = 0;}~mystring() {if (p != NULL) {delete[] p;}}void copy(const char* r) {if (p != NULL) {delete[] p;}len = strlen(r);p = new char[len + 1];strcpy(p, r);}const char* data() {return p;}void show() {cout << p << endl;}void append(const mystring& r) {len = len + r.len;char* back = p;p = new char[len + 1];strcpy(p, back);strcat(p, r.p);delete[] back;}bool compare(const mystring& r) {return strcmp(p, r.p) == 0;}void swap(mystring& r) {char* temp = p;p = r.p;r.p = temp;}
};int main(int argc, const char** argv) {mystring str = "hello"; // 单参数构造函数mystring ptr; // 默认构造函数ptr.copy(str.data()); // 复制 str 到 ptrptr.append(str); // 将 str 追加到 ptrptr.show(); // 输出 ptr 的内容if (ptr.compare(str)) {cout << "ptr and str are the same." << endl;} else {cout << "ptr and str are different." << endl;}ptr.swap(str); // 交换 ptr 和 str 的内容ptr.show(); // 输出 ptr 的内容str.show(); // 输出 str 的内容return 0;
}
#include <iostream>
#include <cstring>
#include <cstdlib>using namespace std;class mystring {
private:char* p;int len;public:// 构造函数mystring(const char* str = nullptr) {if (str) {len = strlen(str);p = new char[len + 1];strcpy(p, str);} else {p = nullptr;len = 0;}}// 析构函数~mystring() {if (p) {delete[] p;}}// 赋值运算符重载mystring& operator=(const mystring& other) {if (this != &other) {if (p) {delete[] p;}len = other.len;p = new char[len + 1];strcpy(p, other.p);}return *this;}// + 运算符重载mystring operator+(const mystring& other) const {mystring result;result.len = len + other.len;result.p = new char[result.len + 1];strcpy(result.p, p);strcat(result.p, other.p);return result;}// += 运算符重载mystring& operator+=(const mystring& other) {char* temp = p;len += other.len;p = new char[len + 1];strcpy(p, temp);strcat(p, other.p);delete[] temp;return *this;}// [] 运算符重载char& operator[](size_t index) {if (index >= len) {throw out_of_range("Index out of range");}return p[index];}// const [] 运算符重载const char& operator[](size_t index) const {if (index >= len) {throw out_of_range("Index out of range");}return p[index];}// 获取字符串数据const char* data() const {return p;}void show() const {cout << p << endl;}
};int main() {mystring str = "hello";mystring ptr = "world";// 测试 + 运算符str = str + ptr;str.show(); // 输出 "helloworld"// 测试 += 运算符str += ptr;str.show(); // 输出 "helloworldworld"// 测试 [] 运算符str[0] = 'H';str.show(); // 输出 "Helloworldworld"return 0;
}
封装消息队列 class Msg{ key_t key int id; int channel } 实现以下功能 Msg m("文件名") m[1].send("数据"),将数据发送到1号频道中 string str = m[1].read(int size) 从1号频道中读取消息,并且返回 编写程序测试
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <stdexcept>using namespace std;// 消息结构体
struct Message {long mtype; // 消息类型(频道号)char mtext[512]; // 消息内容
};// Msg 类封装
class Msg {
private:key_t key; // 消息队列的键值int msgid; // 消息队列的 ID// 创建或获取消息队列void createQueue(const string& filename) {key = ftok(filename.c_str(), 'a');if (key == -1) {throw runtime_error("Failed to generate key");}msgid = msgget(key, 0666 | IPC_CREAT);if (msgid == -1) {throw runtime_error("Failed to create/get message queue");}}public:// 构造函数Msg(const string& filename) {createQueue(filename);}// 析构函数~Msg() {// 删除消息队列(可选,也可以手动删除)msgctl(msgid, IPC_RMID, nullptr);}// 发送消息到指定频道void send(int channel, const string& data) {Message msg;msg.mtype = channel;strncpy(msg.mtext, data.c_str(), sizeof(msg.mtext) - 1);msg.mtext[sizeof(msg.mtext) - 1] = '\0';if (msgsnd(msgid, &msg, sizeof(msg.mtext), 0) == -1) {throw runtime_error("Failed to send message");}}// 从指定频道读取消息string read(int channel, int size) {Message msg;if (msgrcv(msgid, &msg, size, channel, 0) == -1) {throw runtime_error("Failed to receive message");}return string(msg.mtext);}// 重载 [] 运算符,返回一个代理对象struct ChannelProxy {Msg& msg;int channel;ChannelProxy(Msg& msg, int channel) : msg(msg), channel(channel) {}void send(const string& data) {msg.send(channel, data);}string read(int size) {return msg.read(channel, size);}};ChannelProxy operator[](int channel) {return ChannelProxy(*this, channel);}
};// 测试程序
int main() {try {// 创建消息队列Msg m("testfile");// 发送消息到1号频道m[1].send("Hello from channel 1");// 从1号频道读取消息string received = m[1].read(512);cout << "Received: " << received << endl;// 发送消息到2号频道m[2].send("Hello from channel 2");// 从2号频道读取消息received = m[2].read(512);cout << "Received: " << received << endl;} catch (const exception& e) {cerr << "Error: " << e.what() << endl;return 1;}return 0;
}
封装信号灯集 class Sem{ key_t key int id; int index } 实现以下功能 Sem s(参数x,参数y):创建信号灯集,信号灯集中存在 x 个信号量,并且将所有信号量初始化为 y s.init[1](10):手动初始化信号灯集中的第1个信号量,初始化成 10 s[1] + 1 让信号灯集中的第1个信号量的值 +1 s[1] - 1 让信号灯集中的第1个信号量的值 -1 编写程序测试