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

作业社区

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

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

SamstagBaron 的学生作业:

main #include"stackhead.h" typedef struct charstack stackc; typedef struct intstack stack; int main(){ stack* datas = creat_istack(); stackc* operate = creat_cstack(); char candidate1[] = "( 4 + 8 ) * 2 - 3"; int len = sizeof(candidate1)/sizeof(char); int idx = 0; char* candidate = candidate1; while(*candidate!='\0'){ int num = 0; char i = *candidate; if(i=='+'||i=='-'||i=='*'||i=='/'||i=='('){ push_char(operate,i); candidate++; candidate++; continue; } if(i==')'){ candidate++; if(*candidate!='\0') candidate++; while(top_char(operate)!='('){ int data1 = pop_int(datas); int data2 = pop_int(datas); char ope = pop_char(operate); push_int(datas,data1+data2); } pop_char(operate); continue; } while(*candidate!=' '&& *candidate!='\0'){ num = num*10+((*candidate)-'0'); candidate++; } if(!empty_char(operate)){ char ope = top_char(operate); if(ope=='*'||ope=='/'){ int data = pop_int(datas); if(ope=='/')data/=num; if(ope=='*')data*=num; push_int(datas,data); pop_char(operate); } else if(ope=='+'||ope=='('){ push_int(datas,num); } else if(ope=='-'){ push_int(datas,-num); pop_char(operate); push_char(operate,'+'); } } else{ push_int(datas,num); } if(*candidate!='\0') candidate++; } while(!empty_char(operate)){ int data1 = pop_int(datas); int data2 = pop_int(datas); char ope = pop_char(operate); push_int(datas,data1+data2); } printf("%s = %d\n",candidate1,top_int(datas)); return 0; } 栈 #include #include"stackhead.h" #define data_t int typedef struct charstack stackc; typedef struct intstack stack; typedef struct node{ struct node* next; data_t data; }intnode; struct intstack{ intnode* head; }; stack* creat_istack(){ stack* res = (stack*)malloc(sizeof(stack)); res->head = (intnode*)malloc(sizeof(intnode)); res->head->next = NULL; res->head->data = 0; return res; } void push_int(stack* stc,data_t value){ intnode* node = (intnode*)malloc(sizeof(intnode)); node->data = value; node->next = stc->head->next; stc->head->next = node; } data_t top_int(stack* stc){ return stc->head->next->data; } int empty_int(stack* stc){ return stc->head->next==NULL? 1:0; } data_t pop_int(stack* stc){ data_t res = stc->head->next->data; intnode* curr = stc->head->next; stc->head->next = curr->next; free(curr); return res; } 【图片】

得分 100
学习任务

SamstagBaron 的学生作业:

练习1 #include"common.h" struct tree{ data_t value; struct tree* left; struct tree* right; }; typedef struct tree Treenode; int getHeight( struct tree* head){ if(head==NULL){ return 0; } int l = getHeight(head->left); int r = getHeight(head->right); return 1+(l>r?l:r); } struct tree* buildTree(int n,int maxn){ if(n>maxn){ return NULL; } struct tree* node = ( struct tree*)malloc(sizeof( struct tree)); node->value = n; node->left = buildTree(2*n+1,maxn); node->right = buildTree(2*n+2,maxn); return node; } #include"common.h" int main() { int n = 22; struct tree* root = buildTree(0,n); int height = getHeight(root); printf("deep %d \n",height); return 0; } 设maxn=22,输出5【图片】 练习2 typedef struct linknode{ data_t value; struct linknode* next; }linklist; linklist* create_linklist(data_t value){ linklist* res = (linklist*)malloc(sizeof(linklist)); res->next = NULL; res->value = value; } linklist* creatlistbylist(data_t* datas,int len){ if(len==0){ return NULL; } int idx = 1; linklist** curr = NULL; linklist* res = create_linklist(datas[0]); curr = &res; while(idxnext = create_linklist(datas[idx]); curr = &((*curr)->next); idx+=1; } return res; } void printList(linklist* list){ while(list){ printf("%d ",list->value); list = list->next; } printf("\n"); } void sortedBubble(linklist* list){ linklist** curr = &list; linklist* end = NULL; while((*curr)!=end){ while((*curr)->next!=end){ if((*curr)->value>(*curr)->next->value){ ((*curr)->value)^=(*curr)->next->value; (*curr)->next->value^=((*curr)->value); ((*curr)->value)^=(*curr)->next->value; } curr = &((*curr)->next); } end = *curr; curr = &list; } } int main() { int data[] = {2,5,7,10,22,578,321,9,3,5}; linklist* list = creatlistbylist(data,sizeof(data)/sizeof(int)); sortedBubble(list); printList(list); return 0; } 【图片】 练习3 #include"common.h" typedef struct linknode{ data_t value; struct linknode* next; }linklist; linklist* create_linklist(data_t value){ linklist* res = (linklist*)malloc(sizeof(linklist)); res->next = NULL; res->value = value; } linklist* creatlistbylist(data_t* datas,int len){ if(len==0){ return NULL; } int idx = 1; linklist** curr = NULL; linklist* res = create_linklist(datas[0]); curr = &res; while(idxnext = create_linklist(datas[idx]); curr = &((*curr)->next); idx+=1; } return res; } linklist* merge(linklist* list1,linklist* list2){ linklist* head = create_linklist(0); linklist* res = head; while(list1&&list2){ if(list1->valuevalue){ head->next = list1; list1 = list1->next; } else{ head->next = list2; list2 = list2->next; } head = head->next; } if(list1){ head->next = list1; } if(list2){ head->next = list2; } head = res; res = res->next; free(head); return res; } void printList(linklist* list){ while(list){ printf("%d ",list->value); list = list->next; } printf("\n"); } int main() { int data[] = {2,5,7,10,22,578,321,9,3,5}; int data1[] = {1,3,5,7,9}; int data2[] = {2,4,6,8,10}; linklist* list1 = creatlistbylist(data1,5); linklist* list2 = creatlistbylist(data2,5); linklist* newlist = merge(list1,list2); printList(newlist); return 0; } 【图片】 练习4【图片】

得分 100
讨论题

SamstagBaron 的学生作业:

#include #include #include extern struct charstack* creat_cstack(); extern void push_char(struct charstack* stc,char value); extern char top_char(struct charstack* stc); extern char pop_char(struct charstack* stc); extern int empty_char(struct charstack* stc); extern struct intstack* creat_istack(); extern void push_int(struct intstack* stc,int value); extern int top_int(struct intstack* stc); extern int pop_int(struct intstack* stc); extern int empty_int(struct intstack* stc); #include #include"stackhead.h" #define data_t int typedef struct charstack stackc; typedef struct intstack stack; typedef struct node{ struct node* next; data_t data; }intnode; struct intstack{ intnode* head; }; stack* creat_istack(){ stack* res = (stack*)malloc(sizeof(stack)); res->head = (intnode*)malloc(sizeof(intnode)); res->head->next = NULL; res->head->data = 0; return res; } void push_int(stack* stc,data_t value){ intnode* node = (intnode*)malloc(sizeof(intnode)); node->data = value; node->next = stc->head->next; stc->head->next = node; } data_t top_int(stack* stc){ return stc->head->next->data; } int empty_int(stack* stc){ return stc->head->next==NULL? 1:0; } data_t pop_int(stack* stc){ data_t res = stc->head->next->data; intnode* curr = stc->head->next; stc->head->next = curr->next; free(curr); return res; } 主文件 #include"stackhead.h" typedef struct charstack stackc; typedef struct intstack stack; int main(){ stack* datas = creat_istack(); stackc* operate = creat_cstack(); char candidate1[] = "4 + 8 * 2 - 3"; int len = sizeof(candidate1)/sizeof(char); int idx = 0; char* candidate = candidate1; while(*candidate!='\0'){ int num = 0; char i = *candidate; if(i=='+'||i=='-'||i=='*'||i=='/'){ push_char(operate,i); candidate++; candidate++; continue; } while(*candidate!=' '&& *candidate!='\0'){ num = num*10+((*candidate)-'0'); candidate++; } if(!empty_char(operate)){ char ope = top_char(operate); if(ope=='*'||ope=='/'){ int data = pop_int(datas); if(ope=='/')data/=num; if(ope=='*')data*=num; push_int(datas,data); pop_char(operate); } else if(ope=='+'){ push_int(datas,num); } else{ push_int(datas,-num); pop_char(operate); push_char(operate,'+'); } } else{ push_int(datas,num); } if(*candidate!='\0') candidate++; } while(!empty_char(operate)){ int data1 = pop_int(datas); int data2 = pop_int(datas); char ope = pop_char(operate); push_int(datas,data1+data2); } printf("%s = %d\n",candidate1,top_int(datas)); return 0; } 【图片】

微信客服

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

帮助反馈 APP下载

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

公众号

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