亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

作業社區

探索學習新天地,共享知識資源!

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
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號