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

作业社区

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

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

Felixxx 的学生作业:

#include #include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 10 #define MSG_TYPE_A 100 #define MSG_TYPE_B 200 #define MSG_SZ 64 struct msgbuf { long mtype; char mtext[MSG_SZ]; }; void main() { pid_t cpid; key_t key; int msgid,retA,retB; char buffer[MSG_SZ]; ssize_t rbytes; struct msgbuf msgA; struct msgbuf msgB; struct msgbuf rcv_msg; key = ftok(PATHNAME,PRO_ID); if(key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } msgid = msgget(key,IPC_CREAT | 0666); if(msgid == -1) { perror("[ERROR] msgget(): "); exit(EXIT_FAILURE); } printf("msgid is %d \n",msgid); cpid = fork(); if(cpid == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if(cpid == 0) { while(1) { rbytes = msgrcv(msgid,(void *)&rcv_msg,MSG_SZ,MSG_TYPE_A,0); if(rbytes == -1) { perror("[ERROR] msgcv():"); exit(EXIT_FAILURE); } printf("进程A mtype: %ld\n",rcv_msg.mtype); printf("进程A mtext: %s\n",rcv_msg.mtext); } } else if(cpid > 0) { cpid = fork(); if(cpid == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if(cpid == 0) { while(1) { rbytes = msgrcv(msgid,(void *)&rcv_msg,MSG_SZ,MSG_TYPE_B,0); if(rbytes == -1) { perror("[ERROR] msgcv():"); exit(EXIT_FAILURE); } printf("进程B mtype: %ld\n",rcv_msg.mtype); printf("进程B mtext: %s\n",rcv_msg.mtext); } } else if(cpid > 0) { while(1) { fgets(buffer,sizeof(buffer),stdin); buffer[strcspn(buffer,"\n")] = '\0'; if(strcmp(buffer,"quit") == 0) exit(EXIT_SUCCESS); msgA.mtype = MSG_TYPE_A; msgB.mtype = MSG_TYPE_B; strcpy(msgA.mtext,buffer); strcpy(msgB.mtext,buffer); retA = msgsnd(msgid,(const void *)&msgA,strlen(msgA.mtext)+1,0); retB = msgsnd(msgid,(const void *)&msgB,strlen(msgB.mtext)+1,0); if(retA == -1 || retB == -1) { perror("[ERROR] msgsnd(): "); exit(EXIT_FAILURE); } } } } }

得分 100
讨论题

cjozGV 的学生作业:

#include "stdio.h" #include "stdlib.h" #include typedef struct { int data[1024]; int top; } Stack; //初始化栈 void init_stack(Stack *s){ s->top = -1; } //检查栈是否为空 int is_empty(Stack *s){ return s->top == -1; } //压栈 void push(Stack *s,int value){ s->data[++s->top] = value; } //出栈 int pop(Stack *s){ return s->data[s->top--]; } //查看栈顶元素 int peek(Stack *s){ return s->data[s->top]; } //运算符优先级和计算函数 int get_level(char op){ switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return -1; } } //根据运算符执行相应的计算 int compute(int a,int b,int op){ switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } //表达式求值函数 int evaluate_expression(const char *expr){ //operands操作数字 operators操作字符 初始化两个栈 Stack operands,operators; init_stack(&operands); init_stack(&operators); while (*expr) { if (isdigit(*expr)) { int num = 0; while (isdigit(*expr)) { num = num * 10 + (*expr - '0'); expr++; } push(&operands, num); continue; } if (*expr == '(') { push(&operators, *expr); } else if (*expr == ')') { while (!is_empty(&operators) && peek(&operators) != '(') { int b = pop(&operands); //从操作数栈弹出 int a = pop(&operands); //从操作数栈弹出 char op = pop(&operators); //从操作符栈弹出 push(&operands, compute(a, b, op)); // 结果压入操作数栈 } pop(&operators); //弹出左括号 } else if (*expr == '+' || *expr == '-' || *expr == '*' || *expr == '/') { //处理运算符:先计算栈中优先级 >= 当前运算符的运算 while (!is_empty(&operators) && get_level(peek(&operators)) >= get_level(*expr)) { int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a, b, op)); } push(&operators, *expr); //当前运算符压栈 } expr++; //处理下一个字符 } // 处理剩余运算符 while (!is_empty(&operators)) { int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a, b, op)); } return pop(&operands); // 最终结果在操作数栈顶 } int main() { const char *expression = "(4+8)*2-3"; int result = evaluate_expression(expression); printf("Result: %d\n", result); return 0; }

得分 100
讨论题

cjozGV 的学生作业:

#include "stdio.h" #include "stdlib.h" #include typedef struct { int data[1024]; int top; } Stack; //初始化栈 void init_stack(Stack *s){ s->top = -1; } //检查栈是否为空 int is_empty(Stack *s){ return s->top == -1; } //压栈 void push(Stack *s,int value){ s->data[++s->top] = value; } //出栈 int pop(Stack *s){ return s->data[s->top--]; } //查看栈顶元素 int peek(Stack *s){ return s->data[s->top]; } //运算符优先级和计算函数 int get_level(char op){ switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return -1; } } //根据运算符执行相应的计算 int compute(int a,int b,int op){ switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } //表达式求值函数 int evaluate_expression(const char *expr){ //operands操作数字 operators操作字符 初始化两个栈 Stack operands,operators; init_stack(&operands); init_stack(&operators); while (*expr) { if (isdigit(*expr)) { int num = 0; while (isdigit(*expr)) { num = num * 10 + (*expr - '0'); expr++; } push(&operands, num); continue; } // 如果是运算符,处理优先级并压入运算符栈 if (*expr == '+' || *expr == '-' || *expr == '*' || *expr == '/'){ //如果栈顶运算符的优先级大于或等于当前运算符,则先计算栈顶的运算 while (!is_empty(&operators) && get_level(peek(&operators)) >= get_level(*expr)){ int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a,b,op)); } push(&operators,*expr); } expr++; } //处理剩余的运算符 while (!is_empty(&operators)){ int b = pop(&operands); int a = pop(&operands); char op = pop(&operators); push(&operands, compute(a,b,op)); } return pop(&operands); } int main() { const char *expression = "1+123*5"; int result = evaluate_expression(expression); printf("Result: %d\n", result); return 0; }

得分 100
讨论题

Hee_cryLQ0 的学生作业:

josehp.h #ifndef _JOSEHP_H_ #define _JOSEHP_H_ #include #include #include typedef int datatype_t; typedef struct node { datatype_t data; struct node *next; }linknode_t; extern linknode_t *create_josephlist(); extern void print_josehplist(linknode_t *head); extern void josephlist_function(linknode_t *head, int arr[], int index, int k, int m); #endif //_JOSEHP_H_ josehp.c #include "joseph.h" linknode_t *create_josephlist(const int n) { printf("-----初始化约瑟夫环-----"); linknode_t *head = (linknode_t *)malloc(sizeof(linknode_t));//先创建头结点 linknode_t *p = head; for(int i = 1; i data = i; p->next = temp; //链接新创建的结点 p = temp;//移动p到新的结点 } //跳过头结点连成环,相当于把创建的头结点释放掉,第一个数据结点成为新的头结点 p->next = head->next;//经过插入数据,p已经到链表尾部了,把p的next指向head的next,head被空出来等待释放 linknode_t *q = head; //保存头结点地址 head = head->next; //移动头结点到下一个结点 free(q); //释放掉头结点内存 return head; } void print_josehplist(linknode_t *head) { linknode_t *p = head; // while(p->next != NULL) // { // printf("%d", p->data); // p = p->next; // } // printf("%d", p->next);//由于条件判断的局限性,不加这行打印不出来最后一个结点的值,或者直接改成下面的写法 do{ printf("%d", p->data); p = p->next; }while(p != head); printf("\n"); } void josephlist_function(linknode_t *head, int arr[], int index, int k, int m) { linknode_t *current = head; linknode_t *prev = NULL; for(int i = 1; i < k; i++)//移动到从current结点开始数 { prev = current; current = current->next; } printf("当k值为%d时,约瑟夫环的内容为:\n", k); print_josehplist(current); while(current->next != current) { //current每次移动m个结点 for(int i = 1; i < m; i++) { prev = current; current = current->next; } //移动到m处,把m处的结点信息取出并删除结点(current),同时current结点向后移动一位。 prev->next = current->next; //printf("%d", current->data); arr[index++] = current->data; //把每次出列的结点保存到数组 free(current); //释放掉已经出列的结点堆内存 current = prev->next; //current继续保存prev前面的结点 } //当环中只有一个结点时跳出循环并取出最后一个值保存进数组 //printf("%d", current->data); arr[index++] = current->data; free(current); current = NULL; //全部出列,指针置空 return ; } main.c #include "joseph.h" int main() { int n = 8, k = 3, m = 4; int arr[8] = {'0'};//保存出列的结点数据 int index = 0; //定义数组下标 linknode_t *head = create_josephlist(n); print_josehplist(head); josephlist_function(head, arr, index, k, m); printf("约瑟夫环出列顺序为:\n"); for(int i = 0; i < 8; i++) { printf("arr[%d] = %d ", i, arr[i]); } printf("\n"); return 0; } 运行结果: 12345678 当k值为3时,约瑟夫环的内容为: 34567812 约瑟夫环出列顺序为: arr[0] = 6 arr[1] = 2 arr[2] = 7 arr[3] = 4 arr[4] = 3 arr[5] = 5 arr[6] = 1 arr[7] = 8

得分 100
讨论题

cjozGV 的学生作业:

#include // 直接插入排序函数 void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; // 将 arr[i] 插入到已排序的序列 arr[0...i-1] 中 while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } // 打印数组的函数 void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } // 主函数,用于测试插入排序 int main() { int arr[] = {12, 11, 13, 5, 6}; int n = sizeof(arr) / sizeof(arr[0]); printf("原始数组: \n"); printArray(arr, n); insertionSort(arr, n); printf("排序后的数组: \n"); printArray(arr, n); return 0; } 1.外层循环 直接插入排序有一个外层循环,它遍历数组中的每个元素(除了第一个元素,因为它被认为是已排序的) 如果数组有 n 个元素,外层循环将执行 n−1 次 2.外层循环 对于外层循环中的每个元素,内层循环负责将其插入到已排序部分的正确位置 在平均情况下,假设每个元素都需要与已排序部分的大约一半元素进行比较 这是一个简化的假设,用于说明目的 因此,对于第 i 个元素(i 从 1 开始),内层循环大约需要执行 i/2 次比较(在平均情况下) 3.总操作数 将外层循环和内层循环的操作数相乘 直接插入排序的平均时间复杂度是 O(n2)

得分 100
学习任务

qq_康斯坦丁_0 的学生作业:

#include #include #include #include #include #include #include #include #define MSG_KEY 1234 #define MAX_MSG_SIZE 256 // 消息结构 struct msg_buffer { long msg_type; char msg_text[MAX_MSG_SIZE]; }; void child_process(int msgid, int type) { struct msg_buffer message; while (1) { if (msgrcv(msgid, &message, sizeof(message.msg_text), type, 0) < 0) { perror(“msgrcv”); exit(1); } printf(“子进程 (类型 %d) 收到消息: %s”, type, message.msg_text); if (strncmp(message.msg_text, “quit”, 4) == 0) { break; } } exit(0); } int main() { int msgid; pid_t pid_a, pid_b; struct msg_buffer message; char input_buffer[MAX_MSG_SIZE]; // 创建消息队列 msgid = msgget(MSG_KEY, 0666 | IPC_CREAT); if (msgid < 0) { perror("msgget"); exit(1); } // 创建子进程A pid_a = fork(); if (pid_a < 0) { perror("fork A"); exit(1); } if (pid_a == 0) { child_process(msgid, 100); } // 创建子进程B pid_b = fork(); if (pid_b < 0) { perror("fork B"); exit(1); } if (pid_b == 0) { child_process(msgid, 200); } // 父进程 printf("父进程启动,请输入要发送的消息 ('quit' 退出):\n"); while (1) { printf("> "); fgets(input_buffer, sizeof(input_buffer), stdin); // 发送给子进程A message.msg_type = 100; strcpy(message.msg_text, input_buffer); if (msgsnd(msgid, &message, sizeof(message.msg_text), 0) < 0) { perror("msgsnd to A"); exit(1); } // 发送给子进程B message.msg_type = 200; strcpy(message.msg_text, input_buffer); if (msgsnd(msgid, &message, sizeof(message.msg_text), 0) < 0) { perror("msgsnd to B"); exit(1); } if (strncmp(input_buffer, "quit", 4) == 0) { break; } } // 等待子进程结束 waitpid(pid_a, NULL, 0); waitpid(pid_b, NULL, 0); // 删除消息队列 if (msgctl(msgid, IPC_RMID, NULL) < 0) { perror("msgctl"); exit(1); } printf("父进程退出\n"); return 0; }

微信客服

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

帮助反馈 APP下载

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

公众号

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