为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

wgf1209 的学生作业:

Player.hpp #ifndef GOMOKUGAME_01_PLAYER_HPP #define GOMOKUGAME_01_PLAYER_HPP #include #include #include /* @Author: wjhu8 @Time: 2024-11-14 19:27:37 @name: Player.hpp */ //标准的命名空间 using namespace std; //定义一个玩家类 class Player { public: Player(const string &name, const string &color) : name(name), color(color) { } string getName(void) const { return name; } string getColor(void) const { return color; } virtual bool placeChess(int x, int y) = 0; private: string name; string color; }; #endif //GOMOKUGAME_01_PLAYER_HPP BlackPlayer.hpp #ifndef __BLACKPLAYER_HPP__ #define __BLACKPLAYER_HPP__ #include #include #include #include "Player.hpp" #include "ChessBoard.hpp" #include "BlackChess.hpp" /* @Author: wjhu8 @Time: 2024-11-14 19:40:21 @name: BlackPlayer.hpp */ //标准的命名空间 using namespace std; //下黑棋的玩家类设计 class BlackPlayer : public Player { public: BlackPlayer(const string &name) : Player(name, "black") {} public: bool placeChess(int x, int y) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); bool ok = chessBoard->isVaildPostion(x, y); if (ok) { chessBoard->placeChess(new BlackChess(x, y)); } return ok; } }; #endif //__BLACKPLAYER_HPP__ WhitePlayer.hpp #ifndef __WHITEPLAYER_HPP__ #define __WHITEPLAYER_HPP__ #include #include #include #include "Player.hpp" #include "ChessBoard.hpp" #include "WhiteChess.hpp" /* @Author: wjhu8 @Time: 2024-11-14 19:33:27 @name: WhitePlayer.hpp */ //标准的命名空间 using namespace std; //下白旗的玩家类设计 class WhitePlayer : public Player { public: WhitePlayer(const string &name) : Player(name, "white") { } bool placeChess(int x, int y) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); bool ok = chessBoard->isVaildPostion(x, y); if (ok) { chessBoard->placeChess(new WhiteChess(x, y)); } return ok; } }; #endif //__WHITEPLAYER_HPP__ main.cpp #include #include #include #include #include #include //标准算法头文件 #include "head/BlackPlayer.hpp" #include "head/WhitePlayer.hpp" /* @Author: wjhu8 @Time: 2024-11-14 16:39:47 @name: main.cpp */ //标准的命名空间 using namespace std; int main() { //创建棋盘对象 ChessBoard *chessBoard = ChessBoard::getChessBoard(); chessBoard->show();//显示棋盘 BlackPlayer blackPlayer("tiechui"); //黑色棋手 WhitePlayer whitePlayer("cuihua"); //白色棋手 blackPlayer.placeChess(5, 1); whitePlayer.placeChess(5, 3); #if 0 //棋盘 ChessBoard *chessBoard = ChessBoard::getChessBoard(); chessBoard->show(); fprintf(stderr, "\n"); //黑棋子 BlackChess *blackChess = new BlackChess(13, 5); //白棋子 WhiteChess *whiteChess = new WhiteChess(13, 7); chessBoard->placeChess(blackChess); chessBoard->placeChess(whiteChess); //判断棋子的位置是否有效 if (chessBoard->isVaildPostion(5, 1)) { chessBoard->placeChess(new WhiteChess(5, 1)); } #endif fprintf(stderr, "\n"); return 0; } 【图片】

得分 100
学习任务

wgf1209 的学生作业:

Chess.hpp #ifndef __CHESS_HPP__ #define __CHESS_HPP__ #include #include #include /* @Author: wjhu8 @Time: 2024-11-14 16:44:12 @name: Chess.hpp */ //标准的命名空间 using namespace std; // 定义一个Chess(棋子)类 class Chess { public: // 构造函数,初始化棋子的颜色、x轴和y轴坐标 Chess(const string &color, int x, int y) : color(color), x(x), y(y) { } // 获取棋子x轴坐标的方法 int getX(void) const { return x; } // 获取棋子y轴坐标的方法 int getY(void) const { return y; } //获取棋子颜色的方法 string getColor(void) const { return color; } virtual void show(void) const = 0;//纯虚函数-->显示功能 private: int x; // 棋子的x轴坐标 int y; // 棋子的y轴坐标 string color; // 棋子的颜色 }; #endif //__CHESS_HPP__ BlackChess.hpp #ifndef __BLACKCHESS_HPP__ #define __BLACKCHESS_HPP__ #include #include #include #include "Chess.hpp" /* @Author: wjhu8 @Time: 2024-11-14 16:59:44 @name: BlackChess.hpp */ //标准的命名空间 using namespace std; //定义一个BlackChess(黑棋子)类,继承Chess(棋子)类 class BlackChess : public Chess { public: BlackChess(int x, int y) : Chess("black", x, y) {} void show(void) const { fprintf(stderr, "\033[%d;%dH\033[43;34m[☻]\033[0m", getY(), getX() - 1); fprintf(stderr, "\033[%d;%dH", getY(), getX()); } }; #endif //__BLACKCHESS_HPP__ WhiteChess.hpp #ifndef __WHITECHESS_HPP__ #define __WHITECHESS_HPP__ #include #include #include #include "Chess.hpp" /* @Author: wjhu8 @Time: 2024-11-14 17:13:35 @name: WhiteChess.hpp */ //标准的命名空间 using namespace std; class WhiteChess : public Chess { public: WhiteChess(int x, int y) : Chess("white", x, y) {} void show(void) const { fprintf(stderr, "\033[%d;%dH\033[44;36m[☺]\033[0m", getY(), getX() - 1); fprintf(stderr, "\033[%d;%dH", getY(), getX()); } }; #endif //__WHITECHESS_HPP__ main.cpp #include #include #include #include #include #include //标准算法头文件 #include "head/BlackChess.hpp" #include "head/WhiteChess.hpp" /* @Author: wjhu8 @Time: 2024-11-14 16:39:47 @name: main.cpp */ //标准的命名空间 using namespace std; int main() { BlackChess blackChess(10, 5); blackChess.show(); fprintf(stderr, "\n"); WhiteChess whiteChess(11,6); whiteChess.show(); fprintf(stderr, "\n"); return 0; } 【图片】

得分 100
学习任务

weixin_慕哥3021856 的学生作业:

#include "file_transfer.h" #include "tcpsocket.h" #include "debug.h" int recv_fprotocol_head(int cfd, file_protocol_t *p_head) { ssize_t rbytes = 0; ssize_t received = 0; char *buffer = (char *)p_head; for (;;) { rbytes = tcp_recv(cfd, buffer + received, sizeof(file_protocol_t) - received); if (rbytes == -1) { DEBUG_INFO("[ERROR] recv : %s\n", strerror(errno)); return -1; } else if (rbytes == 0) { DEBUG_INFO("[INFO] The client has been closed.\n"); break; } else if (rbytes > 0) { received += rbytes; if (received == sizeof(file_protocol_t)) break; } } if (received != sizeof(file_protocol_t)) { DEBUG_INFO("[ERROR] Fail to receive data.\n"); return -1; } return 0; } int recv_file(int cfd, const char *filename, size_t fsize) { int fd; ssize_t rbytes = 0, wbytes = 0, file_size = 0; char buffer[1024] = {0}; // 打开文件 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); if (fd == -1) { DEBUG_INFO("[ERROR] Fail to open.\n"); return -1; } // 接收文件数据,写入到本地文件中 for (;;) { rbytes = tcp_recv(cfd, buffer, sizeof(buffer)); if (rbytes == -1) { DEBUG_INFO("[ERROR] Fail to receive: %s\n", strerror(errno)); return -1; } else if (rbytes == 0) { DEBUG_INFO("[INFO] The client has been closed.\n"); break; } else if (rbytes > 0) { wbytes = write(fd, buffer, rbytes); if (wbytes == -1) { DEBUG_INFO("[ERROR] write: %s\n", strerror(errno)); return -1; } file_size += rbytes; if (file_size == fsize) break; } } close(fd); return file_size; } int client_upload_file(int cfd) { int ret, recvs; file_protocol_t fhead; // 服务端接收协议头信息 ret = recv_fprotocol_head(cfd, &fhead); if (ret == -1) return -1; recvs = recv_file(cfd, fhead.filename, fhead.filesize); printf("recvived data = %d\n", recvs); return recvs; }

得分 100
学习任务

wgf1209 的学生作业:

#include #include #include #include #include #include //标准算法头文件 /* @Author: wjhu8 @Time: 2024-11-14 14:37:13 @name: mizi.cpp */ //标准的命名空间 using namespace std; // 定义一个名为MiZi的类 class MiZi { public: // 构造函数,初始化x轴位置、y轴位置和长度len MiZi(int x, int y, int len) : x(x), y(y), len(len) { } // 在指定位置(x, y)以指定背景颜色backColor和前景颜色frontColor绘制一个点 void drawPoint(int x, int y, int backColor, int frontColor) const { // 移动光标到指定位置 fprintf(stderr, "\033[%d;%dH", y, x); // 设置颜色并打印一个'*'字符,然后重置颜色 fprintf(stderr, "\033[%d;%dm*\033[0m", backColor, frontColor); } // 显示水平方向的图案 void showHorizontal(void) const { for (int i = 0; i < len; i++) { // 在右侧绘制点 drawPoint(x + i, y, 40, 37); // 在左侧绘制点 drawPoint(x - i, y, 40, 37); } } // 显示垂直向下的图案 void showVertical(void) const { for (int i = 0; i < len; i++) { // 向上绘制点 drawPoint(x, y - i, 41, 36); // 向下绘制点 drawPoint(x, y + i, 41, 36); } } // 显示向上坡度的图案 void showUpHill(void) const { for (int i = 0; i < len; i++) { // 上坡方向绘制点 drawPoint(x + i, y - i, 42, 35); // 下坡方向绘制点 drawPoint(x - i, y + i, 42, 35); } } // 显示向下坡度的图案 void showDownHill(void) const { // 将光标移动到(1, 1)位置 fprintf(stderr, "\033[%d;%dH", 1, 1); for (int i = 0; i < len; i++) { //下坡的反方向 drawPoint(x + i, y + i, 43, 34); //上坡的反方向 drawPoint(x - i, y - i, 43, 34); } // 将光标移动到(y+len, 1)位置 fprintf(stderr, "\033[%d;%dH", y + len, 1); } // 显示所有图案的方法 void show(void) const { showHorizontal(); showVertical(); showUpHill(); showDownHill(); return; } private: int x; // x轴位置 int y; // y轴位置 int len; // 图案的长度 }; int main() { // 创建一个MiZi对象,初始化位置为(9, 10),长度为7 MiZi obj(9, 10, 7); // 调用对象的show方法显示图案 obj.show(); return 0; } 【图片】

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号