作业社区
探索学习新天地,共享知识资源!
浪潮君 的学生作业:
#include // 引入标准输入输出库,使用 std::cout 输出到终端 // ------------------- 抽象基类定义 ------------------- class ChessPiece { public: // 纯虚函数:派生类必须实现此函数,用于绘制棋子 virtual void draw(int row, int col) const = 0; // 虚析构函数:用于确保通过基类指针删除派生类对象时调用正确析构函数 virtual ~ChessPiece() = default; }; // ------------------- 工具函数:移动光标 ------------------- void move_cursor(int row, int col) { // 使用 ANSI VT100 控制码移动光标:\033[row;colH std::cout
upcan 的学生作业:
#include // 简单选择排序函数 void selectionSort(unsigned char str[]) { int n = 0; // 计算字符串长度(不包括 ‘\0’) while (str[n] != ‘\0’) { n++; } for (int i = 0; i < n - 1; i++) { // 假设当前未排序部分的第一个元素为最小值的索引 int minIndex = i; // 遍历未排序部分,找到最小值的索引 for (int j = i + 1; j < n; j++) { if (str[j] < str[minIndex]) { minIndex = j; } } // 如果最小值的索引不是当前未排序部分的第一个元素,则交换它们 if (minIndex != i) { unsigned char temp = str[i]; str[i] = str[minIndex]; str[minIndex] = temp; } } } int main() { unsigned char str[] = “decba”; // 调用简单选择排序函数 selectionSort(str); // 输出排序后的字符串 printf(“排序后的字符串: %s\n”, str); return 0; }
+109
upcan 的学生作业:
#include // 冒泡排序函数 void bubbleSort(unsigned char str[]) { int n = 0; // 计算字符串长度(不包括 ‘\0’) while (str[n] != ‘\0’) { n++; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (str[j] > str[j + 1]) { // 交换 str[j] 和 str[j+1] str[j] ^=str[j+1]; str[j+1]^=str[j]; str[j] ^=str[j+1]; } } } } int main() { unsigned char str[] = “decba”; // 调用冒泡排序函数 bubbleSort(str); // 输出排序后的字符串 printf(“排序后的字符串: %s\n”, str); return 0; }
+113