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

作业社区

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

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$

得分 100
学习任务

cjozGV 的学生作业:

#include #include #include #include #include #include #define FIFO_PATH "mypipe" #define MAXEVENTS 10 int main(void) { int epfd, fd, ret; struct epoll_event ev; struct epoll_event ret_ev[MAXEVENTS]; char buffer[64]; // 如果管道不存在,创建有名管道 if (access(FIFO_PATH, F_OK) == -1) { if (mkfifo(FIFO_PATH, 0666) == -1) { perror("mkfifo"); exit(EXIT_FAILURE); } } // 打开管道读端(非阻塞) fd = open(FIFO_PATH, O_RDONLY | O_NONBLOCK); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } // 创建 epoll 实例 epfd = epoll_create1(0); if (epfd == -1) { perror("epoll_create1"); exit(EXIT_FAILURE); } printf("epfd = %d\n", epfd); // 注册管道 fd 到 epoll,监听可读事件 ev.data.fd = fd; ev.events = EPOLLIN; ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); if (ret == -1) { perror("epoll_ctl"); exit(EXIT_FAILURE); } // 主循环:等待事件发生 for (;;) { ret = epoll_wait(epfd, ret_ev, MAXEVENTS, 5000); // 最多等待 5 秒 if (ret == -1) { perror("epoll_wait"); exit(EXIT_FAILURE); } else if (ret == 0) { printf("Timeout.\n"); } else { for (int i = 0; i < ret; i++) { if (ret_ev[i].events & EPOLLIN) { ssize_t len = read(ret_ev[i].data.fd, buffer, sizeof(buffer) - 1); if (len > 0) { buffer[len] = '\0'; printf("收到数据:%s\n", buffer); } else if (len == 0) { // 写端关闭,重新打开管道 printf("写端关闭,重新打开管道...\n"); close(fd); fd = open(FIFO_PATH, O_RDONLY | O_NONBLOCK); ev.data.fd = fd; epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); } } } } } close(epfd); close(fd); return 0; }

得分 100
讨论题

学渣小白 的学生作业:

#include // 求出下列数据的答案。(上一个表达式x,y的值会影响下面x和y的值) int main() { int x = 10 ,y =20; printf(“int x = 10 ,y =20;” “\n”); int z = 0; printf(“int z = 0;” “\n”); printf("\n*************delimiter**************\n"); z = (++x) + (y++); printf("z = (++x) + (y++);" "\n"); //X is first increased by 1, then assigned to 11, and y is first assigned to 20 before adding 1, so z=11+20=31; x=11; y=21; printf("//X is first increased by 1, then assigned to 11, and y is first assigned to 20 before adding 1, so z=11+20=31;x=11;y=21;" "\n"); printf("z = %d\n",z); printf("x=%d\n",x); printf("y=%d\n",y); printf("\n*************delimiter**************\n"); z = (--x) + (y++); printf("z = (--x) + (y++);" "\n"); // x先减1,再赋值为10,y先赋值为21,再减1,所以z=10+21=31;x=10;y=22; printf("//X is first subtracted by 1 and then assigned a value of 10, y is first assigned a value of 21 and then subtracted by 1, so z=10+21=31; x=10; y=22;" "\n"); printf("z = %d\n",z); printf("x=%d\n",x); printf("y=%d\n",y); printf("\n*************delimiter**************\n"); z= (++x) + (--y); printf("z= (++x) + (--y);" "\n"); //x先增加1,再赋值为11,y先减1,再赋值为21,所以z=11+21=32;x=11;y=21; printf("//X is first increased by 1 and then assigned a value of 11, y is first decreased by 1 and then assigned a value of 21, so z=11+21=32; x=11; y=21;" "\n"); printf("z = %d\n",z); printf("x=%d\n",x); printf("y=%d\n",y); printf("\n*************delimiter**************\n"); printf("x = %d y = %d\n",x,y); return 0; } linux@linux:~/test01$ gcc 1i4practicer.c linux@linux:~/test01$ ./a.out int x = 10 ,y =20; int z = 0; delimiter* z = (++x) + (y++); //X is first increased by 1, then assigned to 11, and y is first assigned to 20 before adding 1, so z=11+20=31;x=11;y=21; z = 31 x=11 y=21 delimiter* z = (–x) + (y++); //X is first subtracted by 1 and then assigned a value of 10, y is first assigned a value of 21 and then subtracted by 1, so z=10+21=31; x=10; y=22; z = 31 x=10 y=22 delimiter* z= (++x) + (–y); //X is first increased by 1 and then assigned a value of 11, y is first decreased by 1 and then assigned a value of 21, so z=11+21=32; x=11; y=21; z = 32 x=11 y=21 delimiter* x = 11 y = 21 linux@linux:~/test01$

微信客服

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

帮助反馈 APP下载

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

公众号

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