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

作业社区

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

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

weixin_慕哥3021856 的学生作业:

// expression.h #ifndef __EXPRESSION_H_ #define __EXPRESSION_H_ #include #include #include typedef int datatype; typedef struct node { datatype data; struct node *next; }linknode_t; typedef struct { linknode_t *top; // 栈顶指针 int n; // 栈的元素个数 }linkstack_t; extern linkstack_t *init_linkstack(); // 初始化栈 extern int push_stack(linkstack_t *lstack, datatype data); // 入栈 extern datatype pop_stack(linkstack_t *lstack); // 出栈 extern int is_empty_linkstack(linkstack_t *lstack); // 判断栈是否为空 extern datatype get_top_data(linkstack_t *lstack); // 获取栈顶元素 extern int chtoi(const char *buf); // 数字字符串转正数 extern int get_level(char optr); // 获取运算符优先级 extern int compute(linkstack_t *opd, linkstack_t *opt); // 计算之后再入栈 extern int deal_with(linkstack_t *opd, linkstack_t *opt, char optr); // 处理运算符 #endif // expression.c #include "expression.h" linkstack_t *init_linkstack() { linkstack_t *lstack = (linkstack_t *)malloc(sizeof(linkstack_t)); lstack->top = NULL; lstack->n = 0; return lstack; } int push_stack(linkstack_t *lstack, datatype data) { linknode_t *newnd = NULL; newnd = (linknode_t *)malloc(sizeof(linknode_t)); newnd->data = data; newnd->next = lstack->top; lstack->top = newnd; lstack->n++; return 0; } datatype pop_stack(linkstack_t *lstack) { if (is_empty_linkstack(lstack)) { perror("Empty linkstack.\n"); return 0; } linknode_t *tnode = NULL; datatype tdata; tnode = lstack->top; tdata = tnode->data; lstack->top = tnode->next; free(tnode); tnode = NULL; lstack->n--; return tdata; } int is_empty_linkstack(linkstack_t *lstack) { return lstack->top == NULL; } datatype get_top_data(linkstack_t *lstack) { return lstack->top->data; } int chtoi(const char *buf) { int data = 0; for (int i = 0; i < strlen(buf); i++) { data = data * 10 + buf[i] - '0'; } return data; } int get_level(char optr) { switch (optr) { case '+': case '-': return 1; case '*': case '/': return 2; case '(': return 0; default: printf("Invalid operator: %c\n", optr); return -1; } } int compute(linkstack_t *opd, linkstack_t *opt) { int data = 0, n1 = 0, n2 = 0; char c = 0; c = pop_stack(opt); // 运算符出栈 n2 = pop_stack(opd); // 运算数2出栈 n1 = pop_stack(opd); // 运算数1出栈 switch (c) { case '+': data = n1 + n2; break; case '-': data = n1 - n2; break; case '*': data = n1 * n2; break; case '/': if (n2 == 0) { perror("除数不为0!\n"); return -1; } data = n1 / n2; break; } push_stack(opd, data); // 计算得到的数入栈 return 0; } int deal_with(linkstack_t *opd, linkstack_t *opt, char optr) { int cur_optr_level = 0, prev_optr_level = 0; while(1) { if (is_empty_linkstack(opt) || (get_level(get_top_data(opt)) == 0)) { push_stack(opt, optr); return 0; } prev_optr_level = get_level(get_top_data(opt)); cur_optr_level = get_level(optr); // 当前运算符的优先级小于或等于栈顶的,则直接计算 if (cur_optr_level = '0' && *p

得分 100
讨论题

weixin_慕哥3021856 的学生作业:

// expression.h #ifndef __EXPRESSION_H_ #define __EXPRESSION_H_ #include #include #include typedef int datatype; typedef struct node { datatype data; struct node *next; }linknode_t; typedef struct { linknode_t *top; // 栈顶指针 int n; // 栈的元素个数 }linkstack_t; extern linkstack_t *init_linkstack(); // 初始化栈 extern int push_stack(linkstack_t *lstack, datatype data); // 入栈 extern datatype pop_stack(linkstack_t *lstack); // 出栈 extern int is_empty_linkstack(linkstack_t *lstack); // 判断栈是否为空 extern datatype get_top_data(linkstack_t *lstack); // 获取栈顶元素 extern int chtoi(const char *buf); // 数字字符串转整数 extern int get_level(char optr); // 获取运算符优先级 extern int compute(linkstack_t *opd, linkstack_t *opt); // 计算之后再入栈 extern int deal_with(linkstack_t *opd, linkstack_t *opt, char optr); // 处理运算符 #endif // expression.c #include "expression.h" linkstack_t *init_linkstack() { linkstack_t *lstack = (linkstack_t *)malloc(sizeof(linkstack_t)); lstack->top = NULL; lstack->n = 0; return lstack; } int push_stack(linkstack_t *lstack, datatype data) { linknode_t *newnd = NULL; newnd = (linknode_t *)malloc(sizeof(linknode_t)); newnd->data = data; newnd->next = lstack->top; lstack->top = newnd; lstack->n++; return 0; } datatype pop_stack(linkstack_t *lstack) { if (is_empty_linkstack(lstack)) { perror("Empty linkstack.\n"); return 0; } linknode_t *tnode = NULL; datatype tdata; tnode = lstack->top; tdata = tnode->data; lstack->top = tnode->next; free(tnode); tnode = NULL; lstack->n--; return tdata; } int is_empty_linkstack(linkstack_t *lstack) { return lstack->top == NULL; } datatype get_top_data(linkstack_t *lstack) { return lstack->top->data; } int chtoi(const char *buf) { int data = 0; for (int i = 0; i < strlen(buf); i++) { data = data * 10 + buf[i] - '0'; } return data; } int get_level(char optr) { switch (optr) { case '+': case '-': return 1; case '*': case '/': return 2; default: printf("Invalid operator: %c\n", optr); return -1; } } int compute(linkstack_t *opd, linkstack_t *opt) { int data = 0, n1 = 0, n2 = 0; char c = 0; n2 = pop_stack(opd); // 运算数2出栈 n1 = pop_stack(opd); // 运算数1出栈 c = pop_stack(opt); // 运算符出栈 switch (c) { case '+': data = n1 + n2; break; case '-': data = n1 - n2; break; case '*': data = n1 * n2; break; case '/': if (n2 == 0) { perror("除数不为0!\n"); return -1; } data = n1 / n2; break; } push_stack(opd, data); // 计算得到的数入栈 return data; } int deal_with(linkstack_t *opd, linkstack_t *opt, char optr) { int cur_optr_level = 0, prev_optr_level = 0, s = 0; while(1) { if (is_empty_linkstack(opt)) { push_stack(opt, optr); return 0; } prev_optr_level = get_level(get_top_data(opt)); cur_optr_level = get_level(optr); // 当前运算符的优先级小于或等于栈顶的,则直接计算 if (cur_optr_level = '0' && *p

微信客服

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

帮助反馈 APP下载

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

公众号

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