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

作业社区

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

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

慕尼黑0001808 的学生作业:

// 大家自己写代码实现求二叉树的深度。 #include "tree.h" int main() { bitree_t * root = create_node(1); root->lchild = create_node(2); root->rchild = create_node(3); root->lchild->lchild = create_node(4); root->lchild->rchild = create_node(5); int result = max_depth(root); printf("二叉树深度是:%d\n",result); return 0; } // tree.h #ifndef __TREE_H__ #define __TREE_H__ #include #include //二叉树结构体 typedef struct bitree{ int val;//保存编号 struct bitree *lchild;//保存左孩子地址 struct bitree *rchild;//保存右孩子地址 }bitree_t; extern bitree_t * create_node(int val); extern int max_depth(bitree_t * root); #endif // tree.c #include "tree.h" bitree_t * create_node(int val) { bitree_t * node = (bitree_t *)malloc(sizeof(bitree_t)); if(node == NULL) { printf("bitree_t malloc fail\n"); exit(EXIT_FAILURE); } node->val = val; node->lchild = NULL; node->rchild = NULL; return node; } int max_depth(bitree_t * root) { if(NULL == root) { return 0; } int left_depth = max_depth(root->lchild); int right_depth = max_depth(root->rchild); return 1 + (left_depth>right_depth?left_depth:right_depth); } //main.c //在单向链表中实现冒泡排序。 #include "link.h" int main() { int arr[] = {5,2,7,4,8,9}; int len = sizeof(arr)/sizeof(arr[0]); node_t * head = create_empty_linklist(); for(int i = 0;i < len;i++) { insert_tail_linklist(head,arr[i]); } print_linklist(head); sort_linklist(head); print_linklist(head); return 0; } //link.h #ifndef __LINK_H__ #define __LINK_H__ #include #include #include //单向链表节点结构体 typedef struct node{ int data; struct node * next; }node_t; //创建空的单向链表头节点 extern node_t * create_empty_linklist(); //单向链表尾插法 extern void insert_tail_linklist(node_t * head,int data); //单向链表冒泡排序 extern void sort_linklist(node_t * head); //顺序打印单向链表数据 extern void print_linklist(node_t * head); #endif //link.c #include "link.h" node_t * create_empty_linklist() { node_t * head = NULL; head = (node_t*)malloc(sizeof(node_t)); if(NULL == head) { printf("malloc head fail\n"); exit(EXIT_FAILURE); } memset(head,0,sizeof(node_t)); head->next = NULL; return head; } void insert_tail_linklist(node_t * head,int data) { node_t * temp = NULL; temp = (node_t*)malloc(sizeof(node_t)); if(NULL == temp) { printf("malloc temp fail\n"); exit(EXIT_FAILURE); } memset(temp,0,sizeof(node_t)); temp->data = data; temp->next = NULL; node_t * p = head; while(p->next!=NULL)p = p->next; p->next = temp; } void sort_linklist(node_t * head) { if (head == NULL || head->next == NULL) { return; // 空链表或只有一个节点,无需排序 } int swapped; node_t * prev; // 前一个节点 node_t * curr; // 当前节点 node_t * next_node; // 下一个节点 do { swapped = 0; curr = head->next; // 第一个有效节点 prev = head; // 头节点 while (curr->next != NULL) { next_node = curr->next; if (curr->data > next_node->data) { // 交换curr和next_node两个节点 curr->next = next_node->next; next_node->next = curr; prev->next = next_node; // 更新指针位置 curr = next_node; // curr现在指向原来的next_node swapped = 1; } prev = curr; curr = curr->next; } } while (swapped); } void print_linklist(node_t * head) { node_t * temp = head->next; while(temp->next!=NULL) { printf("%d ",temp->data); temp = temp->next; } printf("%d ",temp->data); printf("\n"); } //将两个有序链表合成一个有序链表。 //main.c #include "tree.h" int main() { int arr1[] = {1,3,5,7,9}; int arr2[] = {2,4,6,8,10}; int len1 = sizeof(arr1)/sizeof(arr1[0]); int len2 = sizeof(arr2)/sizeof(arr2[0]); linknode_t * head1 = create_empty_linknode(); linknode_t * head2 = create_empty_linknode(); for(int i = 0;i < len1;i++) { insert_tail_linknode(head1,arr1[i]); } for(int i = 0;i < len2;i++) { insert_tail_linknode(head2,arr2[i]); } print_linklist(head1); print_linklist(head2); linknode_t * head = and_linklist(head1,head2); print_linklist(head); head == head1 ? free(head2) : free(head1); // 释放内存 free_linklist(head); return 0; } //tree.h #ifndef __TREE_H__ #define __TREE_H__ #include #include #include typedef struct node{ int data; struct node * next; }linknode_t; extern linknode_t * create_empty_linknode(); extern void insert_tail_linknode(linknode_t * head,int data); extern linknode_t * and_linklist(linknode_t * head1,linknode_t * head2); extern void print_linklist(linknode_t * head); extern void free_linklist(linknode_t * head); #endif //tree.c #include "tree.h" linknode_t * create_empty_linknode() { linknode_t * head = (linknode_t*)malloc(sizeof(linknode_t)); if(NULL == head) { printf("malloc head fail\n"); exit(EXIT_FAILURE); } memset(head,0,sizeof(linknode_t)); head->next = NULL; return head; } void insert_tail_linknode(linknode_t * head,int data) { linknode_t * temp = (linknode_t*)malloc(sizeof(linknode_t)); if(NULL == temp) { printf("malloc temp fail\n"); exit(EXIT_FAILURE); } temp->data = data; temp->next = NULL; linknode_t * p = head; while(p->next != NULL)p = p->next; p->next = temp; return; } linknode_t * and_linklist(linknode_t * head1, linknode_t * head2) { if (head1->next == NULL) return head2; if (head2->next == NULL) return head1; linknode_t *head, *tail; linknode_t *p1 = head1->next; linknode_t *p2 = head2->next; // 选择第一个节点较小的链表作为结果链表 if (p1->data data) { head = head1; tail = p1; p1 = p1->next; } else { head = head2; tail = p2; p2 = p2->next; } head->next = tail; // 合并剩余节点 while (p1 != NULL && p2 != NULL) { if (p1->data data) { tail->next = p1; tail = p1; p1 = p1->next; } else { tail->next = p2; tail = p2; p2 = p2->next; } } // 连接剩余链表 if (p1 != NULL) { tail->next = p1; } else { tail->next = p2; } return head; } void print_linklist(linknode_t * head) { linknode_t * p = head->next; while(NULL != p) { printf("%d ",p->data); p = p->next; } printf("\n"); } void free_linklist(linknode_t * head) { linknode_t * current = head; while (current != NULL) { linknode_t * temp = current; current = current->next; free(temp); } } //已经知道一棵二叉树。 //先序遍历:ABDGHCEIF //中序遍历:GDHBAEICF //要求大家画出这颗二叉树。 /* A / \ B C / / \ D E F / \ \ G H I */

得分 100
讨论题

慕尼黑0001808 的学生作业:

// stack.h #ifndef __STACK_H__ #define __STACK_H__ #define MAX_STACK_SIZE 100 #include // 运算符栈 typedef struct num_stack{ int data[MAX_STACK_SIZE]; int top; }num_stack; // 操作符栈 typedef struct char_stack{ char data[MAX_STACK_SIZE]; int top; }char_stack; extern void push_num(num_stack * s,int value); extern void push_char(char_stack * s,char value); extern int pop_num(num_stack * s); extern char pop_char(char_stack * s); extern int peek_num(num_stack * s); extern char peek_char(char_stack * s); #endif //stack.c #include "stack.h" #include #include void push_num(num_stack * s,int value) { if(s->top < MAX_STACK_SIZE-1) { s->data[++s->top] = value; } else { printf("错误:数字栈溢出!\n"); exit(EXIT_FAILURE); } } void push_char(char_stack * s,char value) { if(s->top < MAX_STACK_SIZE-1) { s->data[++s->top] = value; } else { printf("错误:操作符栈溢出!\n"); exit(EXIT_FAILURE); } } int pop_num(num_stack * s) { if(s->top > -1) { return s->data[s->top--]; } printf("错误:数字栈下溢!\n"); exit(EXIT_FAILURE); } char pop_char(char_stack * s) { if(s->top> -1) { return s->data[s->top--]; } printf("错误:操作符栈下溢!\n"); exit(EXIT_FAILURE); } int peek_num(num_stack * s) { if(s->top > -1) { return s->data[s->top]; } printf("错误:数字栈为空!\n"); exit(EXIT_FAILURE); } char peek_char(char_stack * s) { if(s->top > -1) { return s->data[s->top]; } printf("错误:操作符栈为空!\n"); exit(EXIT_FAILURE); } //main.c #include "stack.h" #include #include #include int get_priority(char op) { if(op == '*'||op == '/')return 3; if(op == '+'||op =='-')return 2; if(op == '(')return 1; return 0; } int calculate(int a,int b,char op) { switch(op) { case '+':return a + b; case '-':return a - b; case '*':return a * b; case '/': if(b == 0) { printf("错误:被除数不能为0!\n"); exit(EXIT_FAILURE); } return a / b; default: printf("错误:未知运算符!\n"); exit(EXIT_FAILURE); } } int calculate_expression(char * expr) { num_stack operand_s; //运算数栈 char_stack operator_s; // 运算符栈 num_stack * operand = &operand_s; char_stack * operator = &operator_s; // 初始化栈 operand->top = -1; operator->top = -1; int i = 0; while(expr[i]!='\0') { if(isdigit(expr[i])) { int num = 0; while(isdigit(expr[i])) { num = num * 10 + expr[i] - '0'; i++; } push_num(operand,num); }else if(expr[i]=='('){ push_char(operator,expr[i]); i++; }else if(expr[i]==')'){ // 遇到右括号,计算直到遇到左括号 while(operator->top>-1 && peek_char(operator)!='(') { char op = pop_char(operator); int b = pop_num(operand); int a = pop_num(operand); int result = calculate(a,b,op); push_num(operand,result); } // 弹出左括号 if(operator->top>-1 && peek_char(operator)=='(') { pop_char(operator); } i++; }else if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/'){ //当前运算符优先级 top>-1 && get_priority(peek_char(operator))>=get_priority(expr[i])) { char op = pop_char(operator); int b = pop_num(operand); int a = pop_num(operand); int result = calculate(a,b,op); push_num(operand,result); } push_char(operator,expr[i]); i++; }else{ // 忽略空格等其他字符 i++; } } // 处理剩余的运算符 while(operator->top > -1) { char op = pop_char(operator); int b = pop_num(operand); int a = pop_num(operand); int result = calculate(a, b, op); push_num(operand, result); } return pop_num(operand); } int main() { char expr[100]; printf("请输入表达式: "); scanf("%s",expr); int result = calculate_expression(expr); printf("计算结果是:%d\n",result); return 0; }

得分 100
讨论题

慕尼黑0001808 的学生作业:

// stack.h #ifndef __STACK_H__ #define __STACK_H__ #define MAX_STACK_SIZE 10 #include // 运算符栈 typedef struct num_stack{ int data[MAX_STACK_SIZE]; int top; }num_stack; // 操作符栈 typedef struct char_stack{ char data[MAX_STACK_SIZE]; int top; }char_stack; extern void push_num(num_stack * s,int value); extern void push_char(char_stack * s,char value); extern int pop_num(num_stack * s); extern char pop_char(char_stack * s); extern int peek_num(num_stack * s); extern char peek_char(char_stack * s); #endif //stack.c #include "stack.h" // void push_num(num_stack * s,int value) { if(s->top < MAX_STACK_SIZE-1) { s->data[++s->top] = value; } } void push_char(char_stack * s,char value) { if(s->top < MAX_STACK_SIZE-1) { s->data[++s->top] = value; } } int pop_num(num_stack * s) { if(s->top > -1) { return s->data[s->top--]; } return -1; } char pop_char(char_stack * s) { if(s->top> -1) { return s->data[s->top--]; } return ' '; } int peek_num(num_stack * s) { if(s->top > -1) { return s->data[s->top]; } return -1; } char peek_char(char_stack * s) { if(s->top > -1) { return s->data[s->top]; } return ' '; } //main.c #include "stack.h" #include #include int get_priority(char op) { if(op == '*'||op == '/')return 2; if(op == '+'||op =='-')return 1; return 0; } int calculate(int a,int b,char op) { switch(op) { case '+':return a + b; case '-':return a - b; case '*':return a * b; case '/': if(b == 0) { printf("错误:被除数不能为0!\n"); exit(EXIT_FAILURE); } return a / b; default: printf("错误:未知运算符!\n"); exit(EXIT_FAILURE); } } int calculate_expression(char * expr) { num_stack operand_s; //运算数栈 char_stack operator_s; // 运算符栈 num_stack * operand = &operand_s; char_stack * operator = &operator_s; // 初始化栈 operand->top = -1; operator->top = -1; int i = 0; while(expr[i]!='\0') { if(isdigit(expr[i])) { int num = 0; while(isdigit(expr[i])) { num = num * 10 + expr[i] - '0'; i++; } push_num(operand,num); }else if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/'){ //当前运算符优先级 top>-1 && get_priority(peek_char(operator))>=get_priority(expr[i])) { char op = pop_char(operator); int b = pop_num(operand); int a = pop_num(operand); int result = calculate(a,b,op); push_num(operand,result); } push_char(operator,expr[i]); i++; }else{ i++; } } // 处理剩余的运算符 while(operator->top > -1) { char op = pop_char(operator); int b = pop_num(operand); int a = pop_num(operand); int result = calculate(a, b, op); push_num(operand, result); } return pop_num(operand); } int main() { char expr[100]; scanf("%s",expr); int result = calculate_expression(expr); printf("计算结果是:%d\n",result); return 0; }

得分 99
讨论题

学渣小白 的学生作业:

第7行输出____0 第10行输出____1 第13行输出____0 第16行输出____0 第19行输出____1 第22行输出____0 第25行输出____1 第27行输出____0,0,1 #include #define debug 1 #define DEBUG_PRINT(…) if(debug) printf(VA_ARGS) int main(void) { //DEBUG_PRINT (“Simplified debugging: temp=% d, formula calculation:% d \n”, temp, temp+10); int value = 0; int a = 0; int b = 1; int Estimate=0,Estimate_a=0,Estimate_b=0; value = a && b; printf("value = a && b;" "\n"); Estimate=0 ; printf("Estimate=0 ;" "\n"); Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); value = a || b; printf("value = a || b;" "\n"); Estimate=1 ; printf("Estimate=1 ;" "\n"); Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); value = a++ && b ++; printf("value = a || b;" "\n"); Estimate=0 ; printf("Estimate=0 ;" "\n"); Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); //a=1;b=1 value = --a || b --; printf("value = --a || b --;" "\n"); Estimate=1 ; printf("Estimate=1 ;" "\n"); Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); //a=0;b=0 value = (a + b) || (a - b); printf("value = (a + b) || (a - b);" "\n"); Estimate=0; printf("Estimate=0 ;" "\n"); Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); //a=0;b=0 value = (a * b) && (a + b); printf("value = (a * b) && (a + b);" "\n"); Estimate=0 ; printf("Estimate=0 ;" "\n"); Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); //a=0;b=0 value = !a; printf("value = !a;" "\n"); Estimate=1 ; printf("Estimate=1 ;" "\n"); Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); //a=0;b=0 Estimate_a=0,Estimate_b=0,Estimate=Estimate; printf("Estimate_a=0,Estimate_b=0,Estimate=Estimate;" "\n"); Estimate_a==a && Estimate_b==b && Estimate==value?printf("Great, you guessed it right!\n"):printf("Wrong, recalculate!\n"); printf("\n*************delimiter**************\n"); return 0; } linux@linux:~/test01$ gcc 1i8practicer.c linux@linux:~/test01$ ./a.out value = a && b; Estimate=0 ; Great, you guessed it right! delimiter* value = a || b; Estimate=1 ; Great, you guessed it right! delimiter* value = a || b; Estimate=0 ; Great, you guessed it right! delimiter* value = --a || b --; Estimate=1 ; Great, you guessed it right! delimiter* value = (a + b) || (a - b); Estimate=0 ; Great, you guessed it right! delimiter* value = (a * b) && (a + b); Estimate=0 ; Great, you guessed it right! delimiter* value = !a; Estimate=1 ; Great, you guessed it right! delimiter* Estimate_a=0,Estimate_b=0,Estimate=Estimate; Great, you guessed it right! delimiter* linux@linux:~/test01$

微信客服

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

帮助反馈 APP下载

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

公众号

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