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

作业社区

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

0 提交作业
0 布置作业
0 满分作业
得分 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
您的移动学习伙伴

公众号

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