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

作业社区

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

0 提交作业
0 布置作业
0 满分作业
得分 100
讨论题

学无止境呀呀呀 的学生作业:

#include #include #include #include // 链栈节点结构 typedef struct StackNode { int data; // 存储数据(整数或字符的ASCII值) struct StackNode *next; // 指向下一个节点 } StackNode; // 链栈结构 typedef struct { StackNode *top; // 栈顶指针 int size; // 栈的大小 } LinkStack; void compute(LinkStack *operand, LinkStack *anOperator); // 初始化栈 LinkStack* init_stack() { LinkStack stack = (LinkStack)malloc(sizeof(LinkStack)); if (stack == NULL) { printf(“Memory allocation failed!\n”); exit(1); } stack->top = NULL; // 栈顶初始化为空 stack->size = 0; // 栈大小初始化为0 return stack; } // 判断栈是否为空 int is_empty(LinkStack *stack) { return stack->top == NULL; } // 入栈操作 void push_stack(LinkStack *stack, int data) { StackNode new_node = (StackNode)malloc(sizeof(StackNode)); if (new_node == NULL) { printf(“Memory allocation failed!\n”); exit(1); } new_node->data = data; // 设置节点数据 new_node->next = stack->top; // 新节点指向原栈顶 stack->top = new_node; // 栈顶指向新节点 stack->size++; // 栈大小增加 } // 出栈操作 int pop_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty, cannot pop!\n”); exit(1); } StackNode *temp = stack->top; int data = temp->data; // 获取栈顶数据 stack->top = temp->next; // 栈顶指向下一个节点 free(temp); // 释放原栈顶节点 stack->size–; // 栈大小减少 return data; } // 获取栈顶元素(不出栈) int peek_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty!\n”); exit(1); } return stack->top->data; } // 销毁栈 void destroy_stack(LinkStack *stack) { while (!is_empty(stack)) { pop_stack(stack); } free(stack); } // 打印栈内容(用于调试) void print_stack(LinkStack *stack, const char *stack_name) { printf("%s: [", stack_name); StackNode *current = stack->top; int first = 1; while (current != NULL) { if (!first) printf(", “); if (strcmp(stack_name, “Operator”) == 0) { printf(”’%c’", (char)current->data); } else { printf("%d", current->data); } current = current->next; first = 0; } printf("]\n"); } // 获取运算符优先级 int get_level(char operator) { switch(operator) { case ‘(’: return 0; //左括号优先级最低,作为栈底边界 case ‘+’: case ‘-’: return 1; // 加减法优先级为1 case ‘*’: case ‘/’: return 2; // 乘除法优先级为2 default: printf(“Invalid operator: %c\n”, operator); return -1; } } void handle_right_parenthesis(LinkStack *operand,LinkStack *operator){ printf(" Processing right parenthesis ‘)’\n"); printf(" Computing until left parenthesis ‘(’ is found…\n"); //持续计算遇到左括号 while (!is_empty(operator)) { char top_op = (char) peek_stack(operator); if(top_op == ‘(’) { //找到了匹配的左括号,将其弹出并丢弃 pop_stack(operator); printf(" Found matching ‘(’, parenthesis pair processed\n"); return; } //如果不是左括号 执行计算 compute(operand,operator); } // 如果到这里说明没有找到匹配的左括号 printf("Error: Unmatched right parenthesis!\n"); exit(1); } // 执行计算操作 void compute(LinkStack *opd, LinkStack *opt) { if (is_empty(opd) || is_empty(opt)) { printf(“Stack is empty, cannot compute!\n”); return; } int data2 = pop_stack(opd); // 第二个操作数出栈 if (is_empty(opd)) { printf("Not enough operands for computation!\n"); push_stack(opd, data2); // 放回数据 return; } int data1 = pop_stack(opd); // 第一个操作数出栈 char c = (char)pop_stack(opt); // 运算符出栈 int result = 0; // 根据运算符执行相应计算 switch(c) { case '+': result = data1 + data2; break; case '-': result = data1 - data2; break; case '*': result = data1 * data2; break; case '/': if (data2 == 0) { printf("Division by zero!\n"); exit(1); } result = data1 / data2; break; default: printf("Unknown operator: %c\n", c); return; } push_stack(opd, result); // 计算结果入操作数栈 printf(" Computed: %d %c %d = %d\n", data1, c, data2, result); } // 处理运算符 void deal_with_operator(LinkStack *operand, LinkStack *operator, char current_op) { printf(" Processing operator: ‘%c’\n", current_op); // 如果运算符栈为空,直接入栈 if (is_empty(operator)) { push_stack(operator, (int)current_op); printf(" Operator stack empty, '%c' pushed directly\n", current_op); return; } char top_op = (char)peek_stack(operator); int current_level = get_level(current_op); int top_level = get_level(top_op); printf(" Comparing: '%c'(level %d) vs '%c'(level %d)\n", current_op, current_level, top_op, top_level); // 当前运算符优先级大于栈顶运算符,直接入栈 if (current_level > top_level) { push_stack(operator, (int)current_op); printf(" '%c' has higher precedence, pushed to stack\n", current_op); } // 当前运算符优先级小于等于栈顶运算符,先计算再入栈 else { printf(" '%c' has equal/lower precedence, computing first\n", current_op); // 重要:如果栈顶是左括号,不能进行计算,直接入栈当前运算符 if (top_op == '(') { push_stack(operator, (int)current_op); printf(" Top is '(', pushing '%c' directly\n", current_op); } else { compute(operand, operator); // 递归处理,因为可能需要连续计算多个运算符 deal_with_operator(operand, operator, current_op); } } } // 判断字符是否为左括号 int is_left_parenthesis(char c) { return (c == ‘(’); } // 判断字符是否为右括号 int is_right_parenthesis(char c) { return (c == ‘)’); } // 判断字符是否为运算符 int is_operator(char c) { return (c == ‘+’ || c == ‘-’ || c == ‘*’ || c == ‘/’); } // 表达式求值主函数 int evaluate_expression(char *expression) { LinkStack *operand = init_stack(); // 操作数栈 LinkStack *operator = init_stack(); // 运算符栈 char *p = expression; printf("Starting to parse expression: %s\n", expression); printf("Length: %zu characters\n", strlen(expression)); printf("========================================\n"); while (*p != '\0') { printf("Current character: '%c' (ASCII: %d)\n", *p, (int)*p); // 跳过空格 if (*p == ' ' || *p == '\t') { //指针取值 printf(" Skipping whitespace\n"); p++; //移动指针 continue; } // 处理数字 if (*p >= '0' && *p = '0' && *p

得分 100
讨论题

学无止境呀呀呀 的学生作业:

#include #include #include #include // 链栈节点结构 typedef struct StackNode { int data; // 存储数据(整数或字符的ASCII值) struct StackNode *next; // 指向下一个节点 } StackNode; // 链栈结构 typedef struct { StackNode *top; // 栈顶指针 int size; // 栈的大小 } LinkStack; // 初始化栈 LinkStack* init_stack() { LinkStack stack = (LinkStack)malloc(sizeof(LinkStack)); if (stack == NULL) { printf(“Memory allocation failed!\n”); exit(1); } stack->top = NULL; // 栈顶初始化为空 stack->size = 0; // 栈大小初始化为0 return stack; } // 判断栈是否为空 int is_empty(LinkStack *stack) { return stack->top == NULL; } // 入栈操作 void push_stack(LinkStack *stack, int data) { StackNode new_node = (StackNode)malloc(sizeof(StackNode)); if (new_node == NULL) { printf(“Memory allocation failed!\n”); exit(1); } new_node->data = data; // 设置节点数据 new_node->next = stack->top; // 新节点指向原栈顶 stack->top = new_node; // 栈顶指向新节点 stack->size++; // 栈大小增加 } // 出栈操作 int pop_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty, cannot pop!\n”); exit(1); } StackNode *temp = stack->top; int data = temp->data; // 获取栈顶数据 stack->top = temp->next; // 栈顶指向下一个节点 free(temp); // 释放原栈顶节点 stack->size–; // 栈大小减少 return data; } // 获取栈顶元素(不出栈) int peek_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty!\n”); exit(1); } return stack->top->data; } // 销毁栈 void destroy_stack(LinkStack *stack) { while (!is_empty(stack)) { pop_stack(stack); } free(stack); } // 打印栈内容(用于调试) void print_stack(LinkStack *stack, const char *stack_name) { printf("%s: [", stack_name); StackNode *current = stack->top; int first = 1; while (current != NULL) { if (!first) printf(", “); if (strcmp(stack_name, “Operator”) == 0) { printf(”’%c’", (char)current->data); } else { printf("%d", current->data); } current = current->next; first = 0; } printf("]\n"); } // 获取运算符优先级 int get_level(char operator) { switch(operator) { case ‘+’: case ‘-’: return 1; // 加减法优先级为1 case ‘*’: case ‘/’: return 2; // 乘除法优先级为2 default: printf(“Invalid operator: %c\n”, operator); return -1; } } // 执行计算操作 void compute(LinkStack *opd, LinkStack *opt) { if (is_empty(opd) || is_empty(opt)) { printf(“Stack is empty, cannot compute!\n”); return; } int data2 = pop_stack(opd); // 第二个操作数出栈 if (is_empty(opd)) { printf("Not enough operands for computation!\n"); push_stack(opd, data2); // 放回数据 return; } int data1 = pop_stack(opd); // 第一个操作数出栈 char c = (char)pop_stack(opt); // 运算符出栈 int result = 0; // 根据运算符执行相应计算 switch(c) { case '+': result = data1 + data2; break; case '-': result = data1 - data2; break; case '*': result = data1 * data2; break; case '/': if (data2 == 0) { printf("Division by zero!\n"); exit(1); } result = data1 / data2; break; default: printf("Unknown operator: %c\n", c); return; } push_stack(opd, result); // 计算结果入操作数栈 printf(" Computed: %d %c %d = %d\n", data1, c, data2, result); } // 处理运算符 void deal_with_operator(LinkStack *operand, LinkStack *operator, char current_op) { printf(" Processing operator: ‘%c’\n", current_op); // 如果运算符栈为空,直接入栈 if (is_empty(operator)) { push_stack(operator, (int)current_op); printf(" Operator stack empty, '%c' pushed directly\n", current_op); return; } char top_op = (char)peek_stack(operator); int current_level = get_level(current_op); int top_level = get_level(top_op); printf(" Comparing: '%c'(level %d) vs '%c'(level %d)\n", current_op, current_level, top_op, top_level); // 当前运算符优先级大于栈顶运算符,直接入栈 if (current_level > top_level) { push_stack(operator, (int)current_op); printf(" '%c' has higher precedence, pushed to stack\n", current_op); } // 当前运算符优先级小于等于栈顶运算符,先计算再入栈 else { printf(" '%c' has equal/lower precedence, computing first\n", current_op); compute(operand, operator); // 递归处理,因为可能需要连续计算多个运算符 deal_with_operator(operand, operator, current_op); } } // 判断字符是否为运算符 int is_operator(char c) { return (c == ‘+’ || c == ‘-’ || c == ‘*’ || c == ‘/’); } // 表达式求值主函数 int evaluate_expression(char *expression) { LinkStack *operand = init_stack(); // 操作数栈 LinkStack *operator = init_stack(); // 运算符栈 char *p = expression; printf("Starting to parse expression: %s\n", expression); printf("Length: %zu characters\n", strlen(expression)); printf("========================================\n"); while (*p != '\0') { printf("Current character: '%c' (ASCII: %d)\n", *p, (int)*p); // 跳过空格 if (*p == ' ' || *p == '\t') { //指针取值 printf(" Skipping whitespace\n"); p++; //移动指针 continue; } // 处理数字 if (*p >= '0' && *p = '0' && *p

得分 100
学习任务

jelasin 的学生作业:

#include #include #include #include #include #include #include #include #include #include #include #define MAX_EVENTS 1024 #define BUFFER_SIZE 4096 #define WORKER_NUM 4 int create_udp_socket(int port) { int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("socket creation failed"); exit(EXIT_FAILURE); } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("bind failed"); close(sockfd); exit(EXIT_FAILURE); } return sockfd; } void *worker_thread(void *arg) { int sockfd = *(int *)arg; free(arg); // Free the allocated memory for the argument int epoll_fd = epoll_create1(0); if (epoll_fd < 0) { perror("epoll_create1 failed"); exit(EXIT_FAILURE); } struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = sockfd; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockfd, &ev) < 0) { perror("epoll_ctl failed"); close(epoll_fd); exit(EXIT_FAILURE); } struct epoll_event events[MAX_EVENTS]; char buffer[BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t addr_len = sizeof(client_addr); while (1) { int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); if (nfds < 0) { if (errno == EINTR) continue; perror("epoll_wait failed"); break; } for (int i = 0; i < nfds; i++) { if (events[i].data.fd == sockfd) { ssize_t recv_len = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr, &addr_len); if (recv_len < 0) { perror("recvfrom failed"); continue; } printf("Worker[%ld] received %zd bytes from %s:%d\n", pthread_self(), recv_len, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); // Echo back sendto(sockfd, buffer, recv_len, 0, (struct sockaddr*)&client_addr, addr_len); } } } close(epoll_fd); return NULL; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } int port = atoi(argv[1]); int sockfd = create_udp_socket(port); pthread_t tids[WORKER_NUM]; for (int i = 0; i < WORKER_NUM; i++) { int *thread_sockfd = malloc(sizeof(int)); if (thread_sockfd == NULL) { perror("malloc failed"); exit(EXIT_FAILURE); } *thread_sockfd = sockfd; if (pthread_create(&tids[i], NULL, worker_thread, thread_sockfd) != 0) { perror("pthread_create failed"); exit(EXIT_FAILURE); } } printf("Server started with %d workers on port %d\n", WORKER_NUM, port); printf("Master process PID: %d\n", getpid()); for (int i = 0; i < WORKER_NUM; i++) { pthread_join(tids[i], NULL); } close(sockfd); return 0; }

微信客服

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

帮助反馈 APP下载

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

公众号

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