主页 > 游戏开发  > 

C++day2

C++day2
一、练习1

        封装一个mystring类:

        拥有私有成员: 

                char* p 

                int len 

        需要让以下代码编译通过,并实现对应功能: 

                mystring str = "hello";

                mystring ptr;

                ptr.copy(str);

                ptr.append(str);

                ptr.show();  // 输出 ptr 代表的字符串;

                ptr pare(str);  // 比较 ptr 和 str 是否一样;

                ptr.swap(str);  // 交换 ptr 和 str 的内容;

【完整代码】如下:

#include <iostream> #include <string.h> #include <stdlib.h> #include <stdio.h> using namespace std; class mystring{ private: char* p; int len; public: mystring(); mystring(const char* str); ~mystring(); void copy(const mystring& r); void append(const mystring& r); void show(); bool compare(const mystring& r); void swap(mystring& r); const char* data(); }; mystring::mystring(){ p = NULL; len = 0; } mystring::mystring(const char* str){ len = strlen(str); p = (char*)calloc(1, len+1); strcpy(p, str); } mystring::~mystring(){ if(p != NULL){ free(p); } } void mystring::copy(const mystring& r){ if(p != NULL){ free(p); } len = r.len; p = (char*)calloc(1, len+1); strcpy(p, r.p); } const char* mystring::data(){ return p; } 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); } void mystring::show(){ cout << p << endl; } 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() { mystring str = "hello"; printf("str = %s\n", str.data()); mystring ptr; ptr.copy("你好"); ptr.show(); ptr.append("C++"); ptr.show(); if(ptr pare(str)){ cout << "ptr 和 str 一样" << endl; }else{ cout << "ptr 和 str 不一样" << endl; } // 交换ptr和str的内容 ptr.swap(str); ptr.show(); str.show(); return 0; }

二、作业1

        封装一个 File 类,用有私有成员:

                File* fp;

        实现以下功能:

                File f = "文件名";  // 要求打开该文件;

                f.write(string str);  // 要求将str数据写入文件中;

                string str = f.read(int size);  // 从文件中读取最多 size 个字节,并将读取到的数据返回;

        包含:析构函数;

【完整代码】如下:

#include <iostream> #include <string> #include <cstring> #include <fcntl.h> #include <unistd.h> using namespace std; class myfile { private: int fd; // 文件描述符 public: myfile(); // 默认构造函数 myfile(const char* filename); // 带参构造函数 ~myfile(); // 析构函数 void open(const char* filename); // 打开文件 void write(const string& str); // 写入文件 string read(int size); // 读取文件 }; // 默认构造函数 myfile::myfile() { fd = -1; // 初始化为无效文件描述符 } // 带参构造函数 myfile::myfile(const char* filename) { fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644); // 打开文件 if (fd == -1) { cout << "文件打开失败: " << filename << endl; } } // 析构函数 myfile::~myfile() { if (fd != -1) { close(fd); // 关闭文件 } } // 打开文件 void myfile::open(const char* filename) { if (fd != -1) { close(fd); // 如果文件已打开,先关闭 } fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644); // 打开文件 if (fd == -1) { cout << "文件打开失败: " << filename << endl; } } // 写入文件 void myfile::write(const string& str) { if (fd == -1) { cout << "文件未打开,无法写入" << endl; return; } ssize_t bytesWritten = ::write(fd, str.c_str(), str.size()); // 写入字符串 if (bytesWritten == -1) { cout << "写入文件失败" << endl; } } // 读取文件 string myfile::read(int size) { if (fd == -1) { cout << "文件未打开,无法读取" << endl; return ""; } char* buffer = (char*)calloc(1, size)); // 动态分配缓冲区 if (buffer == NULL) { cout << "内存分配失败" << endl; return ""; } ssize_t bytesRead = ::read(fd, buffer, size); // 读取最多 size 个字节 if (bytesRead == -1) { cout << "读取文件失败" << endl; free(buffer); return ""; } string result(buffer); // 将缓冲区内容转换为字符串 free(buffer); // 释放缓冲区 return result; } int main() { // 测试代码 myfile f = "./1.txt"; // 打开文件 f.write("Hello, World!\n"); // 写入数据 f.write("Hello, C++\n"); string content = f.read(100); // 读取最多 100 个字节 cout << "读取到的内容: " << content << endl; return 0; }

三、练习2

#include <iostream> #include <cstring> #include <cstdlib> #include <unistd.h> #include <sstream> #include <vector> #include <memory> #include <pthread.h> using namespace std; class Mutex{ private: pthread_mutex_t m; int type; public: Mutex(){} Mutex(int type); void init(int type); void lock(); void lock(int sec); void unlock(); }; Mutex::Mutex(int type) :type(type) { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr,type); pthread_mutex_init(&m,&attr); } void Mutex::init(int type){ pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr,type); pthread_mutex_init(&m,&attr); } void Mutex::lock(){ pthread_mutex_lock(&m); } void Mutex::unlock(){ pthread_mutex_unlock(&m); } int main(int argc,const char** argv){ }

标签:

C++day2由讯客互联游戏开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“C++day2