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

作业社区

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

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

weixin_慕哥3021856 的学生作业:

epoll.c #include #include #include #include #include #include #include #include #define MAXEVENTS 10 #define FIFO "./fifo_epoll" int main(int argc, const char *argv[]) { int epfd, ret, fd; int rbytes; struct epoll_event eevent; struct epoll_event ret_eev[MAXEVENTS]; char buffer[64] = {0}; epfd = epoll_create(1); if (epfd == -1) { perror("[ERROR] epoll_create()"); exit(EXIT_FAILURE); } printf("epfd = %d\n", epfd); fd = open(FIFO, O_RDONLY | O_NONBLOCK); if (fd == -1) { perror("[ERROR] open()"); exit(EXIT_FAILURE); } eevent.events = EPOLLIN; eevent.data.fd = fd; ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &eevent); if (ret == -1) { perror("[ERROR] epoll_ctl()"); exit(EXIT_FAILURE); } for (;;) { ret = epoll_wait(epfd, ret_eev, MAXEVENTS, 1000); if (ret == -1) { perror("[ERROR] epoll_wait()"); exit(EXIT_FAILURE); } else if (ret == 0) { printf("Timeout\n"); } else { // 循环处理事件 for (int i = 0; i < ret; i++) { int current_fd = ret_eev[i].data.fd; rbytes = read(current_fd, buffer, sizeof(buffer)-1); if (rbytes > 0) { buffer[rbytes] = '\0'; if (strcmp(buffer, "q") == 0 || strcmp(buffer, "Q") == 0) { close(current_fd); epoll_ctl(epfd, EPOLL_CTL_DEL, current_fd, NULL); printf("The fd[%d] was closed.\n", current_fd); break; } printf("Read buffer: %s\n", buffer); } else if (rbytes == 0) // 写端关闭 { close(current_fd); epoll_ctl(epfd, EPOLL_CTL_DEL, current_fd, NULL); break; } memset(buffer, 0, sizeof(buffer)); } } } return 0; } namedpipe.c #include #include #include #include #include #include #include #define FIFO "./fifo_epoll" int main(void) { int fd, ret; char wbuf[64]; ssize_t wbytes; memset(wbuf, 0, sizeof(wbuf)); ret = access(FIFO, F_OK); if (ret == -1) { mkfifo(FIFO, 0666); } fd = open(FIFO, O_WRONLY); if (fd == -1) { perror("[ERROR] open()"); exit(EXIT_FAILURE); } for (;;) { printf("Input: "); fgets(wbuf, sizeof(wbuf), stdin); wbuf[strlen(wbuf) - 1] = '\0'; wbytes = write(fd, wbuf, strlen(wbuf)); if (wbytes == -1) { perror("[ERROR] write()"); close(fd); exit(EXIT_FAILURE); } if (strcmp(wbuf, "q") == 0 || strcmp(wbuf, "Q") == 0) { break; } memset(wbuf, 0, sizeof(wbuf)); } close(fd); return 0; }

得分 100
学习任务

慕神4583458 的学生作业:

#ifndef __JUDGE_H__ #define __JUDGE_H__ #include "Player.hpp" #include "ChessBoard.hpp" class Judge { public: bool isWin(Player *player) { bool ok = false; ok = isHorizontalWin(player->getColor()); if (ok) return true; ok = isVerticalWin(player->getColor()); if (ok) return true; ok = isUphillWin(player->getColor()); if (ok) return true; ok = isDownhillWin(player->getColor()); if (ok) return true; return false; }; bool isHorizontalWin(const string &color) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 1; int i; for (i = 1; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, column + i, line); if (ok) { count++; } else { break; } } if (count >= 5) return true; for (i = 1; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, column - i, line); if (ok) { count++; } else { break; } } return count >= 5 ? true : false; }; bool isVerticalWin(const string &color) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 1; int i; for (i = 1; i < 5; i++) { if (chessBoard->isSameColorChess(color, column, line + i)) { count++; } } if (count >= 5) return true; for (i = 1; i < 5; i++) { if (chessBoard->isSameColorChess(color, column, line - i)) { count++; } } return count >= 5 ? true : false; }; bool isUphillWin(const string &color) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 1; int i; for (i = 1; i < 5; i++) { if (chessBoard->isSameColorChess(color, column + i, line - i)) { count++; } } if (count >= 5) return true; for (i = 1; i < 5; i++) { if (chessBoard->isSameColorChess(color, column - i, line + i)) { count++; } } return count >= 5 ? true : false; } bool isDownhillWin(const string &color) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 1; int i; for (i = 1; i < 5; i++) { if (chessBoard->isSameColorChess(color, column + i, line + i)) { count++; } } if (count >= 5) return true; for (i = 1; i < 5; i++) { if (chessBoard->isSameColorChess(color, column - i, line - i)) { count++; } } return count >= 5 ? true : false; } }; #endif

得分 100
学习任务

慕神4583458 的学生作业:

ChessBoard.hpp #ifndef __CHESSBOARD_H__ #define __CHESSBOARD_H__ #include #include "Chess.hpp" using namespace std; #define LINE 15 #define COLUMN 15 #define X_SKIN 4 #define Y_SKIN 2 #define MIN_X 1 #define MAX_X 57 #define MIN_Y 1 #define MAX_Y 29 class ChessBoard { public: ~ChessBoard() { fprintf(stderr, "\033[%d;%dH", MAX_Y + 1, MIN_X); for (int i = 0; i < LINE; i++) { for (int j = 0; j < COLUMN; j++) { if (chess[i][j]) { delete chess[i][j]; } } } } static ChessBoard *getChessBoard(void) { if (!chessBoard) { chessBoard = new ChessBoard; } return chessBoard; }; void show(void) const { FILE *fp; fp = fopen("1_ChessBoard.txt", "r"); if (fp == NULL) { perror("openChessBoard():"); return; } char str[1024]; fprintf(stderr, "\033[%d;%dH", MIN_Y, MIN_X); while(fgets(str, sizeof(str), fp)) { fprintf(stderr, "%s", str); } fclose(fp); }; void placeChess(const Chess *chess) { int y = chess->getY() / Y_SKIN; int x = chess->getX() / X_SKIN; this->chess[y][x] = chess; chess->show(); }; bool isValidPosition(int x, int y) { if (x > MAX_X || y > MAX_Y || x < MIN_X || y < MIN_Y) return false; int line = y / Y_SKIN; int column = x / X_SKIN; return chess[line][column] ? false : true; }; class GC { public: ~GC() { if (chessBoard) { delete chessBoard; } } }; private: ChessBoard() { for (int i = 0; i < LINE; i++) { for (int j = 0; j < COLUMN; j++) { chess[i][j] = NULL; } } }; static ChessBoard *chessBoard; const Chess* chess[LINE][COLUMN]; }; ChessBoard *ChessBoard::chessBoard = nullptr; ChessBoard::GC gc; #endif Player.hpp #ifndef __PLAYER_H__ #define __PLAYER_H__ #include using namespace std; class Player { public: Player(const string &name, const string &color): name(name), color(color) {}; string getName() { return name; }; string getColor() { return color; }; virtual bool placeChess(int x, int y) = 0; private: string name; string color; }; #endif WhitePlayer.hpp #ifndef __WHITEPLAYER_H__ #define __WHITEPLAYER_H__ #include "Player.hpp" #include "ChessBoard.hpp" #include "WhiteChess.hpp" class WhitePlayer: public Player { public: WhitePlayer(const string &name): Player(name, "White") {}; virtual bool placeChess(int x, int y) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); bool ok = chessBoard->isValidPosition(x, y); if (ok) { chessBoard->placeChess(new WhiteChess(x, y)); } return ok; } }; #endif BlackPlayer.hpp #ifndef __BLACKPLAYER_H__ #define __BLACKPLAYER_H__ #include "Player.hpp" #include "ChessBoard.hpp" #include "BlackChess.hpp" class BlackPlayer: public Player { public: BlackPlayer(const string &name): Player(name, "Black") {}; virtual bool placeChess(int x, int y) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); bool ok = chessBoard->isValidPosition(x, y); if (ok) { chessBoard->placeChess(new BlackChess(x, y)); } return ok; } }; #endif main.cpp #include "BlackPlayer.hpp" #include "WhitePlayer.hpp" #include "ChessBoard.hpp" int main() { ChessBoard *chessBoard = ChessBoard::getChessBoard(); chessBoard->show(); WhitePlayer whitePlayer("hqq"); BlackPlayer blackPlayer("pyy"); whitePlayer.placeChess(9, 17); blackPlayer.placeChess(13, 17); } 【图片】

得分 100
学习任务

向佐佐 的学生作业:

#include #include #include #include #include #include #include #include #include #define MAXEVENTS 10 #define FIFO_NAME "./info" int main(void) { int ret,fd,wbytes,rbytes,epfd; char wbuffer[32] = {0}; char rbuffer[32] = {0}; pid_t cpid; //创建有名管道 ret = access(FIFO_NAME,F_OK); if(ret != -1) { unlink(FIFO_NAME); } ret = mkfifo(FIFO_NAME,0644); if(ret == -1) { perror("[ERROR]mkfifo:\n"); exit(EXIT_FAILURE); } fd = open(FIFO_NAME,O_RDWR);//打开有名管道 if(fd == -1) { perror("[ERROR]open():\n"); close(fd); exit(EXIT_FAILURE); } cpid = fork();//开子进程进行有名管道数据写入 if(cpid == -1) { perror("[ERROR]fork()\n"); return -1; } else if(cpid == 0) { printf("Please input your data:\n"); fgets(wbuffer,sizeof(wbuffer),stdin); wbytes = write(fd,wbuffer,strlen(wbuffer)); if(wbytes < 0) { perror("write():\n"); exit(EXIT_FAILURE); } } epfd = epoll_create(1); if(epfd == -1) { perror("[ERROR]epoll_create():\n"); exit(EXIT_FAILURE); } struct epoll_event ev;//定义事件结构体(关联文件描述符与对应的监控事件) ev.events = EPOLLIN; ev.data.fd = fd; ret = epoll_ctl(epfd,EPOLL_CTL_ADD,0,&ev);//将标准输入文件描述符添加到epoll实例中 if(ret == -1) { perror("[ERROR] epoll_ctl();\n"); exit(EXIT_FAILURE); } struct epoll_event res_ev[MAXEVENTS];//存储结果(就绪文件描述符) for(;;) { ret = epoll_wait(epfd,res_ev,MAXEVENTS,500);//ret代表已经就绪的文件描述符的数量 if(ret == -1) { perror("[ERROR]epoll wait()"); exit(EXIT_FAILURE); } else if(ret == 0) { printf("Time out.\n"); } else if(ret > 0) { //有文键描述符就绪 //遍历文件描述符集合 int i; for(i=0;i

得分 100
学习任务

guzimou 的学生作业:

#ifndef __LINKLIST_H_ #define __LINKLIST_H_ #include #include #include typedef int datatype_t; typedef struct node { datatype_t data; struct node *next; } linknode_t; extern linknode_t *create_empty_linklist(); extern void insert_tail_linklist(linknode_t *head, datatype_t data); extern void print_data_linklist(linknode_t *head); extern int is_empty_linklist(linknode_t *head); extern void reverse_data_linklist(linknode_t *head); extern void clean_up_linklist(linknode_t *head); #endif // DEBUG #include "linklist.h" linknode_t *create_empty_linklist() { linknode_t *head = (linknode_t *)malloc(sizeof(linknode_t)); if (NULL == head) { printf("mall is fall\n"); return NULL; } memset(head, 0, sizeof(linknode_t)); head->next = NULL; return head; } void insert_tail_linklist(linknode_t *head, datatype_t data) { linknode_t *temp = (linknode_t *)malloc(sizeof(linknode_t)); if (NULL == temp) { printf("malloc is fail\n"); return; } temp->data = data; linknode_t *p = head; while (p->next != NULL) { p = p->next; } temp->next = p->next; p->next = temp; return; } void print_data_linklist(linknode_t *head) { linknode_t *p = head; while (p->next != NULL) { printf("%d ", p->next->data); p = p->next; } printf("\n"); return; } int is_empty_linklist(linknode_t *head) { return head->next == NULL ? 1 : 0; } void reverse_data_linklist(linknode_t *head) { linknode_t *p = NULL; linknode_t *q = NULL; p = head->next->next; head->next->next = NULL; while (p != NULL) { q = p->next; p->next = head->next; head->next = p; p = q; } return; } void clean_up_linklist(linknode_t *head) { linknode_t *p = head; linknode_t *q = NULL; while (p != NULL) { q = p->next; print_data_linklist(p); free(p); p = q; } return; } #include "linklist.h" int main(int argc, char const *argv[]) { datatype_t data; int n = 0, i = 0, ret = 0; linknode_t *head = create_empty_linklist(); printf("please input you want to data:"); scanf("%d", &n); printf("please input %d data :", n); for (i = 0; i < n; i++) { scanf("%d", &data); insert_tail_linklist(head, data); } print_data_linklist(head); reverse_data_linklist(head); clean_up_linklist(head); return 0; } please input you want to data:5 please input 5 data :1 3 5 7 9 1 3 5 7 9 9 7 5 3 1 7 5 3 1 5 3 1 3 1 1

微信客服

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

帮助反馈 APP下载

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

公众号

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