
作业社区
探索学习新天地,共享知识资源!
weixin_慕哥3021856 的学生作业:
玩家类、白棋手类、黑棋手类 // player.hpp #ifndef _PLAYER_HEAD_H #define _PLAYER_HEAD_H #include using namespace std; class Player { public: Player(const string &name, const string &pieceColor): name(name), pieceColor(pieceColor){} string getName(void) const { return name; } string getPieceColor(void) const { return pieceColor; } virtual bool placePiece(int x, int y) = 0; private: string name; string pieceColor; }; #endif // whitePlayer.hpp #ifndef _WHITEPIECEPLAYER_H #define _WHITEPIECEPLAYER_H #include "player.hpp" #include "board.hpp" #include "whitePiece.hpp" class WhitePiecePlayer: public Player { public: WhitePiecePlayer(const string &name): Player(name, "white"){} bool placePiece(int x, int y) { Board *board = Board::getBoardObj(); bool placeOK = board->isValidPosition(x, y); if (placeOK) { board->placePiece(new WhitePiece(x, y)); } return placeOK; } }; #endif // blackPlayer.hpp #ifndef _BLACKPIECEPLAYER_H #define _BLACKPIECEPLAYER_H #include "player.hpp" #include "board.hpp" #include "blackPiece.hpp" class BlackPiecePlayer: public Player { public: BlackPiecePlayer(const string &name): Player(name, "black"){} bool placePiece(int x, int y) { Board *board = Board::getBoardObj(); bool placeOK = board->isValidPosition(x, y); if (placeOK) { board->placePiece(new BlackPiece(x, y)); } return placeOK; } }; #endif 棋子基类、白棋类、黑棋类 // piece.hpp #ifndef __PIECE_HEAD_H_ #define __PIECE_HEAD_H_ #include using namespace std; class Piece { public: Piece(const string &color, int x, int y): x(x), y(y), color(color){} int getX(void) const { return x; } int getY(void) const { return y; } string getColor(void) const { return color; } virtual void showPiece(void) const = 0; virtual ~Piece(){} protected: int x; int y; string color; }; #endif // whitePiece.hpp #ifndef __WHITEPIECE_HEAD_H #define __WHITEPIECE_HEAD_H #include #include "piece.hpp" using namespace std; class WhitePiece:public Piece { public: WhitePiece(int x, int y):Piece("white", x, y){} void showPiece(void) const { fprintf(stderr, "\033[%d;%dH\033[40;37m⚪\033[0m", y, x - 1); fprintf(stderr, "\033[%d;%dH\n", y, x); } }; #endif // blackPiece.hpp #ifndef __BLACKPIECE_HEAD_H #define __BLACKPIECE_HEAD_H #include #include "piece.hpp" using namespace std; class BlackPiece:public Piece { public: BlackPiece(int x, int y):Piece("black", x, y){} void showPiece(void) const { fprintf(stderr, "\033[%d;%dH\033[30m⚫\033[0m", y, x - 1); fprintf(stderr, "\033[%d;%dH\n", y, x); } }; #endif 棋盘类 #ifndef _BOARD_HEAD_H #define _BOARD_HEAD_H #include "piece.hpp" using namespace std; #define MIN_X 1 #define MAX_X 57 #define MIN_Y 1 #define MAX_Y 29 #define MAX_LINE 15 #define MAX_COLUMN 15 #define SPACE_X 4 #define SPACE_Y 2 class Board { public: static Board *getBoardObj() { if (!board) board = new Board; return board; } ~Board() { fprintf(stdout, "\033[%d;%dH", MAX_Y+2, MIN_X); for (int i = 0; i < MAX_LINE; i++) { for (int j = 0; j < MAX_COLUMN; j++) { if (this->boardPiece[i][j]) delete boardPiece[i][j]; } } } void show() const { const char *filename = "./ChessBoard.txt"; char buf[1024]; int i = 1; FILE *fp = fopen(filename, "r"); if (!fp) { perror("Failed to open ChessBoard.txt!"); return; } // fprintf(stdout, "\033[%d;%dH", MIN_Y, MIN_X); while (fgets(buf, sizeof(buf), fp)) { fprintf(stdout, "\033[%d;%dH", 1+i, 2); fputs(buf, stdout); i++; } fclose(fp); } void placePiece(Piece *piece) { int line = piece->getY() / SPACE_Y; int column = piece->getX() / SPACE_X; this->boardPiece[line][column] = piece; piece->showPiece(); return; } bool isValidPosition(int x, int y) { if (x < MIN_X || y < MIN_Y) return false; if (x > MAX_X || y > MAX_Y) return false; int line = y / SPACE_Y; int column = x / SPACE_X; return boardPiece[line][column] ? false: true; } class GC { public: ~GC() { if (board) { delete board; } } }; private: Board() { for (int i = 0; i < MAX_LINE; i++) { for (int j = 0; j < MAX_COLUMN; j++) { this->boardPiece[i][j] = nullptr; } } } Piece *boardPiece[MAX_LINE][MAX_COLUMN]; static Board *board; }; Board *Board::board = nullptr; Board::GC gc; #endif main函数 #include "blackPiecePlayer.hpp" #include "whitePiecePlayer.hpp" int main(int argc, const char *argv[]) { Board *board = Board::getBoardObj(); board->show(); BlackPiecePlayer *laowang = new BlackPiecePlayer("laowang"); WhitePiecePlayer *laoli = new WhitePiecePlayer("laoli"); laowang->placePiece(6, 2); laoli->placePiece(10, 2); return 0; } 【图片】





慕尼黑3595755 的学生作业:
#include //简单选择排序 void select_sort(int *p,int n) { int i = 0,j = 0,k = 0; for(i = 0;i < n - 1;i++) //遍历数组中除最后一个元素外所有的元素 { k = i; //k记录起始值 for(j = k + 1;j < n;j++) { if(p[j] < p[k]) k = j; } if(k != i) { p[i] ^= p[k]; p[k] ^= p[i]; p[i] ^= p[k]; } } return ; } void ouput(int *p,int n) { int i = 0; for(i = 0;i < n;i++) { printf("%c ",p[i]); } printf("\n"); } int main() { unsigned char str[] = “decba”; int n = sizeof(a)/sizeof(a[0])-1; ouput(a,n); select_sort(a,n); ouput(a,n); return 0; }





慕尼黑3595755 的学生作业:
#include //冒泡排序 void buddle_sort(int *p,int n) { int i = 0,j = 0; for(i = 0;i < n - 1;i++) //轮数 { for(j = 0;j < n - 1 - i;j++) { if(p[j] > p[j + 1]) { p[j] ^= p[j + 1]; p[j + 1] ^= p[j]; p[j] ^= p[j + 1]; } } } return ; } void ouput(int *p,int n) { int i = 0; for(i = 0;i < n;i++) { printf("%c ",p[i]); } printf("\n"); } int main() { unsigned char str[] = “decba”; int n = sizeof(a)/sizeof(a[0])-1; ouput(a,n); buddle_sort(a,n); ouput(a,n); return 0; }





风_往北吹 的学生作业:
#include int main() { double score; //栈区 char grade; //栈区 printf("请输入学生分数:\n"); scanf("%lf", &score); grade = (score < 0.0 || score>100.0) ? 'F' : (score < 60.0 ? 'E' : (score < 70.0 ? 'D' : (score < 80.0 ? 'C' : (score < 90.0 ? 'B' : 'A' )))); switch (grade) //.text代码段 { case 'A':printf("该用户分数等级为:%c\n", grade); break; case 'B':printf("该用户分数等级为:%c\n", grade); break; case 'C':printf("该用户分数等级为:%c\n", grade); break; case 'D':printf("该用户分数等级为:%c\n", grade); break; case 'E':printf("该用户分数等级为:%c\n", grade); break; case 'F':printf("该用户分数非法\n"); break; } return 0; }





风_往北吹 的学生作业:
#include #include typedef struct { char name[20]; int id; int score; }s_t; int* get_memeory_addr(s_t * p,int plen) { int* q = (int*)malloc(p,plen); if (NULL == q) { printf("malloc Failed!\n"); return -1; } else { memset(q, 0, plen); return q; } } void input_student(s_t* s) { printf("please input info:\n[Name]\t[id]\t[score]\n"); for(int i=0;i




