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

作业社区

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

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

慕斯卡4428351 的学生作业:

#include "linkstack.h" int get_level(char operator) { switch(operator) { case '+': case '-': return 1; case '*': case '/': return 2; default: printf("%c is invalid\n",operator); return -1; } } int compute(linkstack_t *operand,linkstack_t *operator) { int a = 0,b = 0; char c = 0; int data = 0; b = pop_linkstack(operand); a = pop_linkstack(operand); c = (char)pop_linkstack(operator); // printf("b = %d,a = %d\n",b,a); // printf("c = %c\n",c); switch(c) { case '+': data = a + b; break; case '-': data = a - b; break; case '*': data = a * b; break; case '/': data = a / b; break; default: break; } push_linkstack(operand,data); // printf("data = %d\n",data); return data; } int calc_value(char *buf) { int i = 0; int data = 0; int flag = 0; char operator_top = 0; int operand_top = 0; linkstack_t *operand = NULL; linkstack_t *operator = NULL; operand = create_empty_linkstack(); operator = create_empty_linkstack(); for(i = 0;buf[i] != '\0';i++) { //calc the int if(buf[i] >= '0' && buf[i] = get_level(buf[i])) { compute(operand,operator); if(!is_empty_linkstack(operator)) operator_top = (char)get_top_data(operator); else break; } push_linkstack(operator,buf[i]); } } } while(!is_empty_linkstack(operator)) compute(operand,operator); return pop_linkstack(operand); } int main() { char buf[1024] = "(4+8)*2-3"; int value = 0; value = calc_value(buf); printf("%s = %d\n",buf,value); return 0; }

得分 100
讨论题

万物皆流2026 的学生作业:

** looplist1.h ** #ifndef _LOOPLIST1_H_ #define _LOOPLIST1_H_ #include #include #include typedef int datatype_t; typedef struct node { datatype_t data; struct node* next; } loopnode_t; #define N 8 #define K 3 #define M 4 extern int insert_data_looplist(loopnode_t** head,datatype_t data); extern int empty_data_looplist(loopnode_t** head); extern void print_data_looplist(loopnode_t** head); extern void josephu_data_looplist(loopnode_t** head,datatype_t k,datatype_t m); #endif ** looplist1.c ** #include "looplist1.h" int empty_data_looplist(loopnode_t** head) { return *head==NULL?1:0; } int insert_data_looplist(loopnode_t** head,datatype_t data) { loopnode_t* temp=(loopnode_t*)malloc(sizeof(loopnode_t)); if (NULL==temp) { printf("节点空间申请失败!\n"); return -1; } temp->data=data; if (empty_data_looplist(head)) { *head=temp; //当为空链,头指针指向节点temp,temp即为首元节点 temp->next=temp; // 为循环单链 return 0; } loopnode_t* p=*head; //指针p指向首元节点 while (p->next!=*head) { p=p->next; //在非空链是,指针p从首元节点向后移动,直至指向链尾节点 } temp->next=*head; p->next=temp; //尾插法插入新节点,保证循环所以temp->next!=NULL return 0; } void josephu_data_looplist(loopnode_t** head,datatype_t k,datatype_t m) { int i; if (*head==NULL||mnext!=*head) { tail=tail->next; //将指针tail移动至链尾,作为尾节点定位指针。 } pre=tail; //将指针pre指向链尾。 /*此时头指针*head指向首元节点,指针p指向首元节点,指针pre指向链尾节点(指针p的前一位节点),指针tail指在链尾节点*/ for (i=1;inext; //循环结束时,指针p指在节点3,指针pre指在节点2. } printf("从序号%d开始,往后数%d个位置的出列\n",p->data,m); while (p->next!=p) { for (i=1;inext; //循环结束时,指针p指在节点6,指针pre指在节点5。节点6出列(删除) } printf("序号%d出列\n",p->data); pre->next=p->next; if (p==*head) { *head=p->next; //如果出列的是首元节点,则重新更新一下头指针指向新的首元节点 } loopnode_t* temp=p; //设置过度指针temp指向当前指针p所指向的节点,然后释放其内存,指针p继续下一步 p=p->next; free(temp); } printf("链表中最后剩下的节点是%d\n",p->data); *head=p; } void print_data_looplist(loopnode_t** head) { if (NULL==*head) { printf("链表为空!\n"); return; } loopnode_t* p=*head; do{ printf("%d ",p->data); p=p->next; } while (p->next!=*head); printf("\n"); } ** main.c ** #include "looplist1.h" int main() { loopnode_t* head=NULL; int i=0; datatype_t data; for (i=0;i

得分 100
学习任务

学无止境呀呀呀 的学生作业:

file_transfer.h #ifndef __FILE_TRANSFER_H_ #define __FILE_TRANSFER_H_ #include #include #include #include #include #include #include #include #include #include #include #define FILENAME_SZ 1024 typedef struct file_protocol{ size_t filesize;//文件大小 char filename[FILENAME_SZ];//文件名 }file_protocol_t; //文件协议 extern int recv_protocol_head(int cfd,file_protocol_t *p_head); extern int recv_failedata(int cfd,const char *filename,size_t targetsize); #endif file_transfer.c 编译命令:gcc file_transfer.c file_transfer.h debug.h tcpsocket.h -o file_transfer #include "file_transfer.h" #include "tcpsocket.h" #include "debug.h" int recv_protocol_head(int cfd,file_protocol_t *p_head) { ssize_t rbytes; ssize_t total_received = 0;//收到的数量 char *buffer = (char *)p_head;//按照字节流接收 //进行循环接收,防止tcp粘包 for(;;){ rbytes = tcp_recv_pack(cfd,buffer+total_received,sizeof(file_protocol_t)-total_received); if(rbytes == -1) { DEBUG_INFO("[ERROR]: %s",strerror(errno)); return -1; } else if (rbytes == 0) { DEBUG_INFO("[INFO] : The client has been shutdown.\n"); break; }else if(rbytes>0) { total_received += rbytes; if(total_received == sizeof(file_protocol_t))//完成的接收了 break; } } if(total_received != sizeof(file_protocol_t)) { DEBUG_INFO("[ERROR] : Failed to receive data.\n"); return -1; } return 0; } //接收文件数据 int recv_failedata(int cfd,const char *filename,size_t targetsize) { int fd; ssize_t rbytes = 0,wbytes = 0,file_size = 0; char buffer[1024] = {0}; DEBUG_INFO("[INFO] : filename %s\n",filename); //检查文件是否存在 fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,0666); if(fd == -1) { DEBUG_INFO("[ERROR] Failed to open filename"); return -1; } for(;;) { //接收文件数据 rbytes = tcp_recv_pack(cfd,buffer,sizeof(buffer)); if(rbytes == -1) { DEBUG_INFO("[ERROR] :%s",strerror(errno)); return -1; }else if(rbytes == 0) { DEBUG_INFO("[INFO] : The client has been shutdown.\n"); break; }else if (rbytes >0) { wbytes = write(fd,buffer,rbytes);//写入fd,读多少写多少 if(wbytes != rbytes) { DEBUG_INFO("[ERROR] : Failed to write data.\n"); return -1; } //文件大小 file_size += rbytes; if(file_size == targetsize) break; } } close(fd); return file_size; }

微信客服

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

帮助反馈 APP下载

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

公众号

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