作业社区
探索学习新天地,共享知识资源!
沫颖 的学生作业:
strop.h #ifndef __STROP_HEAD_H__ #define __STROP_HEAD_H__ #include #include #include #ifdef __cplusplus extern "C" { #endif extern char *strcpy(char *dest, const char *src); extern char *strcat(char *dest, const char *src); #ifdef __cplusplus } #endif #endif cpy.c #include "strop.h" // 实现 strcpy() 函数 char *strcpy(char *dest, const char *src) { // 保存源字符串的起始位置 char *ret = dest; // 逐个字符进行赋值 while (*src) { *dest++ = *src++; } // 添加字符串结束符 *dest = '\0'; return ret; } cat.c #include "strop.h" // 实现 strcat() 函数 char *strcat(char *dest, const char *src) { // 保存源字符串的起始位置 char *p = dest; // 找到源字符串的末尾位置 while (*p != '\0') { p++; } // 逐个字符进行拼接 while (*src != '\0') { *p++ = *src++; } // 添加字符串结束符 *p = '\0'; return dest; } main.app // 完成一个C语言的动态库制作,动态库中包含strcpy和strcat函数实现,在C++代码中使用这两个函数。 #include #include "strop.h" int main(int argc, char *argv[]) { char str[100]; char str1[100] = "hello"; char str2[100] = ",world!"; strcpy(str, str1); std::cout
+76
沫颖 的学生作业:
代码 // 观察如下代码,写出两个不同的重载函数,完成浮点数和字符串操作 #include using namespace std; extern int my_swap(int a, int b); extern float my_swap(float a, float b); extern string my_swap(string a, string b); int my_swap(int a, int b) { return a + b; } float my_swap(float a, float b) { return a + b; } string my_swap(string a, string b) { return a + " " + b; } int main(int argc, char const *argv[]) { int a = 1, b = 2; float c = 1.1, d = 2.2; string e = "hello", f = "world"; cout
+91
浪潮君 的学生作业:
#include #include #include // 自定义 String 类,模拟 std::string 的基本行为 class String { private: char *data_; // 指向字符串数据的动态分配内存 size_t size_; // 字符串长度,不包括结尾符 '\0' public: // 显式构造函数:从 C 字符串初始化 String 对象 explicit String(const char *str) { if (str == NULL) { // 空指针处理,创建一个空字符串 size_ = 0; data_ = new char[1]; data_[0] = '\0'; } else { // 正常字符串,计算长度并复制内容 size_ = std::strlen(str); data_ = new char[size_ + 1]; // 多分配 1 个字节用于 '\0' std::strcpy(data_, str); // 拷贝字符串内容 } } // 析构函数:释放动态分配的内存,防止内存泄漏 ~String() { delete[] data_; } // 拷贝构造函数:深拷贝另一个 String 对象 String(const String &other) { size_ = other.size_; // 拷贝长度 data_ = new char[size_ + 1]; // 分配新内存 std::strcpy(data_, other.data_); // 拷贝字符串内容 } // 拷贝赋值运算符:避免自我赋值,释放旧内存后深拷贝 String &operator=(const String &other) { if (this != &other) { delete[] data_; // 释放旧内存 size_ = other.size_; // 拷贝长度 data_ = new char[size_ + 1]; // 分配新内存 std::strcpy(data_, other.data_); // 拷贝内容 } return *this; // 返回自身引用以支持链式赋值 } // 获取字符串长度(不含 '\0') size_t size() const { return size_; } // 下标运算符重载:非 const 版本,返回字符引用,可修改内容 char &operator[](size_t index) { if (index >= size_) { throw std::out_of_range("Index out of range"); } return data_[index]; } // 下标运算符重载:const 版本,返回只读字符引用 const char &operator[](size_t index) const { if (index >= size_) { throw std::out_of_range("Index out of range"); } return data_[index]; } }; // 主函数:测试自定义 String 类的基本功能 int main() { String str("hello"); // 创建字符串对象 str,内容为 "hello" str[0] = 'w'; // 修改第 0 个字符为 'w' str[1] = 'x'; // 修改第 1 个字符为 'x' // 逐字符打印修改后的字符串内容 for (size_t i = 0; i < str.size(); ++i) { std::cout
+9
浪潮君 的学生作业:
#include // 前向声明 Test 类,使得其他类可以在 Test 定义之前使用它 class Test; // FriendMember 类声明 // 该类中的某个成员函数将被设置为 Test 的友元成员函数 class FriendMember { public: // 提前声明成员函数签名,供 Test 类在友元声明中使用 void show(const Test &t); }; // Test 类定义 class Test { private: // 私有成员变量,仅能被本类成员或友元访问 int x_; int y_; public: // 构造函数,初始化私有成员变量 x_ 和 y_ Test(int x, int y) : x_(x), y_(y) {} // 声明全局函数 showFriend 是 Test 的朋友,允许访问私有成员 friend void showFriend(const Test &t); // 声明 FriendMember 类中的成员函数 show 是 Test 的朋友 friend void FriendMember::show(const Test &t); // 声明整个 FriendClass 类为 Test 的朋友,其所有成员函数可以访问私有成员 friend class FriendClass; }; // 全局友元函数定义 // 虽然不是 Test 的成员函数,但由于被声明为 friend,可以访问私有成员 void showFriend(const Test &t) { std::cout
+8