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

作业社区

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

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

慕九州9493288 的学生作业:

io 接口实现 文件复制 #include #include #include #include #include #include //命令函输入必选参数个数,如 ./a.out read.txt write.txt 必选参数为2 #define REQUIRED_PARAM_CNT 2 #define FILE_MODE 0644 #define BUF_SIZE 4096 /** * 拷贝文件 */ int copy_file(const char *src, const char *dest) { int fd_in = -1; int fd_out = -1; ssize_t rbytes = 0; ssize_t wbytes = 0; char buf[BUF_SIZE] = {0}; fd_in = open(src, O_RDONLY); if (fd_in == -1) { perror("Open source file failed"); return EXIT_FAILURE; } fd_out = open(dest, O_WRONLY | O_CREAT | O_TRUNC, FILE_MODE); if (fd_out == -1) { perror("Open target file failed"); close(fd_in); return EXIT_FAILURE; } while ((rbytes = read(fd_in, buf, (size_t)BUF_SIZE)) > 0) { char *write_ptr = buf; // 未写入数据的起始指针 ssize_t remaining = rbytes; // 剩余未写入字节数 while (remaining > 0) { wbytes = write(fd_out, write_ptr, (size_t)remaining); if (wbytes == -1) { perror("Write target file failed"); goto cleanup; } //加上从0偏移到已写入的字节数(wbytes)开始指向的未写入数据的起始指针 write_ptr += wbytes; //减去已写入的字节数(wbytes)才是剩余未写入的字数 remaining -= wbytes; } } if (rbytes == -1) { perror("Read source file failed"); goto cleanup; } cleanup: if (fd_out != -1) close(fd_out); if (fd_in != -1) close(fd_in); return (rbytes == -1 || wbytes == -1) ? EXIT_FAILURE : EXIT_SUCCESS; } int main(int argc, char const *argv[]) { if (argc != REQUIRED_PARAM_CNT + 1) { fprintf(stderr, "Usage: %s \n", argv[0]); return EXIT_FAILURE; } return copy_file(argv[1], argv[2]); }

得分 100
学习任务

慕工程6300203 的学生作业:

#include #include #include typedef int data_type; typedef struct node { data_type data; struct node *next; } link_node_type; link_node_type *create_empty_link_list() { link_node_type *head = (link_node_type *)malloc(sizeof(link_node_type)); head->next = NULL; return head; } /** * 头节点插入 * @param head * @param data */ void insert_head_linklist(link_node_type *head, const data_type data) { link_node_type *p = head; link_node_type *new_node = (link_node_type *)malloc(sizeof(link_node_type)); new_node->data = data; new_node->next = p->next; p->next = new_node; } /** * 尾节点插入 * @param head * @param data */ void insert_tail_linklist(link_node_type *head, const data_type data) { link_node_type *p = head; link_node_type *new_node = (link_node_type *)malloc(sizeof(link_node_type)); new_node->data = data; while (p->next != NULL) { p = p->next; } new_node->next = p->next; p->next = new_node; } /** * 判断是否为空 * @param head * @param data */ int is_empty_linklist(const link_node_type *head) { return head->next == NULL ? 1 : 0; } /** * 删除和data相同的节点 * @param head * @param data */ int delete_data(link_node_type *head, const data_type data) { if (is_empty_linklist(head)) return -1; link_node_type *p = head; link_node_type *q = NULL; int i = 0; while (p->next != NULL) { if (p->next->data == data) { q = p->next; p->next = p->next->next; free(q); i++; } else { p = p->next; } } printf("%d node is deleted\n", i); return 0; } void print_link_list(link_node_type *head) { const link_node_type *temp = head; while (temp != NULL && temp->next != NULL) { printf("%d ", temp->next->data); temp = temp->next; } printf("\n"); } void reverse_link_list(link_node_type *head) { link_node_type *p = NULL; link_node_type *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; } } void clear_link_list(link_node_type *head) { // 删除前一个节点,输出后一个节点 link_node_type *p = head; link_node_type *q = NULL; while (p != NULL) { q = p->next; free(p); p = q; } } int main() { link_node_type *head = create_empty_link_list(); insert_tail_linklist(head, 1); insert_tail_linklist(head, 5); insert_tail_linklist(head, 3); insert_tail_linklist(head, 7); insert_tail_linklist(head, 9); print_link_list(head); reverse_link_list(head); print_link_list(head); clear_link_list(head); return 0; }

得分 100
讨论题

学渣小白 的学生作业:

#include #define debug 0 #define DEBUG_PRINT(…) do { if(debug) printf(VA_ARGS); } while(0) int main(void) { //DEBUG_PRINT (“Simplified debugging: temp=% d, formula calculation:% d \n”, temp, temp+10); int i=6; int factorial=1; while(–i) { factorial*=i; DEBUG_PRINT (“i=%d\n”,i); DEBUG_PRINT (“factorial=%d\n”,factorial); DEBUG_PRINT("\n*************delimiter**************\n"); } printf("factorial=%d\n",factorial); return 0; } linux@linux:~/test01$ gcc 1j8practicer.c linux@linux:~/test01$ ./a.out factorial=120 linux@linux:~/test01$ #include #define debug 1 #define DEBUG_PRINT(…) do { if(debug) printf(VA_ARGS); } while(0) int main(void) { //DEBUG_PRINT (“Simplified debugging: temp=% d, formula calculation:% d \n”, temp, temp+10); int passwd = 0,num_input=0; int correct_password=123456; do { if (num_input>=3) { printf(“GAME OVER!” “\n”); return 1; }else if (num_input!=0) { printf(“You have already made mistakes with your password %d times, and there are %d more chances. Keep it up!\n”,num_input,3-num_input ); } printf(“Please enter the password” “\n”); scanf("%d", &passwd); num_input++; // 清除输入缓冲区 while (getchar() != ‘\n’); // 读取并丢弃直到换行符的所有字符 }while (correct_password!=passwd); // 尊敬的艾欧尼亚最强王者,本网吧欢迎您! printf("Dear Ionia, the strongest king of K, our internet cafe welcomes you!" "\n"); return 0; } linux@linux:~/test01$ gcc 1j81practicer.c linux@linux:~/test01$ ./a.out Please enter the password dfdsf2134 You have already made mistakes with your password 1 times, and there are 2 more chances. Keep it up! Please enter the password sdfds23434 You have already made mistakes with your password 2 times, and there are 1 more chances. Keep it up! Please enter the password 4324155 GAME OVER! linux@linux:~/test01$ ./a.out Please enter the password fsgdfdsa You have already made mistakes with your password 1 times, and there are 2 more chances. Keep it up! Please enter the password 123456 Dear Ionia, the strongest king of K, our internet cafe welcomes you! linux@linux:~/test01$

得分 100
学习任务

残梦ming 的学生作业:

#include #include #include #include #include #include #define BUFFER_SIZE 1024 int main(){ pid_t cpid; int pipefd[2]; int ret; ret = pipe(pipefd); // 创建管道后,内核会将文件描述符存储到数组 if(ret == -1){ perror("[ERROR] pipe():"); exit(EXIT_FAILURE); } cpid = fork(); if(cpid == -1){ perror("[ERROR] fork():"); exit(EXIT_FAILURE); }else if(cpid == 0){ // 1.从管道中读取数据(fork()返回0子进程) close(pipefd[1]); // 关闭子进程写端文件描述符 ssize_t rbytes; char buffer[BUFFER_SIZE]; while(1){ memset(buffer, 0, BUFFER_SIZE); // 2.参1:文件描述符 参2:数据缓冲区 参3:能够读取的最大字节数 rbytes = read(pipefd[0], buffer, sizeof(buffer)); // 管道无数据,阻塞 if(rbytes == -1){ perror("[ERROR] read():"); close(pipefd[0]); exit(EXIT_FAILURE); }else if(rbytes == 0){ printf("父进程关闭,子进程退出.\n"); break; } printf("buffer: %s\n", buffer); fflush(stdout); if(strcmp(buffer, "quit") == 0){ printf("子进程退出.\n"); break; } } close(pipefd[0]); exit(EXIT_SUCCESS); }else if(cpid > 0){ close(pipefd[0]); ssize_t wbytes; char buffer[BUFFER_SIZE]; while(1){ memset(buffer, 0, sizeof(buffer)); printf("请输入字符串:"); fflush(stdout); scanf("%s", buffer); if(strcmp(buffer, "quit") == 0){ exit(EXIT_SUCCESS); break; } wbytes = write(pipefd[1], buffer, strlen(buffer)); if(wbytes == -1){ perror("[ERROR] write()"); wait(NULL); close(pipefd[1]); exit(EXIT_FAILURE); } if(strcmp(buffer, "quit") == 0){ break; } } close(pipefd[1]); wait(NULL); } return 0; } 【图片】

微信客服

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

帮助反馈 APP下载

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

公众号

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