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

作业社区

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

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

Jane3764138 的学生作业:

#include #include #include #define MAX 3 struct student { char name[20]; int id; int age; }; typedef struct student datatype_t; typedef struct { datatype_t buf[MAX]; int n; }seqlist_t; seqlist_t *create_empty_seqlist() { seqlist_t *l = NULL; l = (seqlist_t *)malloc(sizeof(seqlist_t)); if(NULL==l) { printf("malloc is fail\n"); return NULL; free (l); } memset(l,0,sizeof(seqlist_t)); l->n = 0; return l; } int is_full_seqlist(seqlist_t *l) { return l->n == MAX ?1:0; } void insert_name_seqlist(seqlist_t *l,char *data) { strcpy(l->buf[l->n].name,data); return; } void insert_id_seqlist(seqlist_t *l,int data) { l->buf[l->n].id = data; return; } void insert_age_seqlist(seqlist_t *l,int data) { l->buf[l->n].age = data; l->n = l->n + 1; return; } void printf_data_seqlist(seqlist_t *l) { int i = 0; for(i=0;in;i++) { printf("%s %d %d\n",l->buf[i].name,l->buf[i].id,l->buf[i].age); } printf("\n"); } int is_empty_seqlist(seqlist_t *l) { return l->n == 0?1:0; } int delete_data_seqlist(seqlist_t *l,int data ) { int i = 0,j = 0; if(is_empty_seqlist(l)) { return -1; } for(i = 0;in;i++) { if(l->buf[i].id != data) { l->buf[j] = l->buf[i]; j++; } } l->n = j; if(i==j) { return -2; }else { printf("delete %d is succeeful!\n",data); } } int main() { seqlist_t *l = NULL; l = create_empty_seqlist(); char name[20]={0}; int id = 0; int age = 0; int delete_id; int ret = 0; printf("please input %d student data :",MAX); while(!is_full_seqlist(l)) { printf("name :"); scanf("%s",name); insert_name_seqlist(l,name); printf("\n"); printf("id :"); scanf("%d",&id); insert_id_seqlist(l,id); printf("\n"); printf("age :"); scanf("%d",&age); insert_age_seqlist(l,age); printf("\n"); } printf_data_seqlist(l); printf("which one do you want to delete:"); scanf("%d",&delete_id); ret = delete_data_seqlist(l,delete_id); if(ret

得分 100
学习任务

Jane3764138 的学生作业:

#include #include #include #define MAX 3 struct student { char name[20]; int id; int age; }; typedef struct student datatype_t; typedef struct { datatype_t buf[MAX]; int n; }seqlist_t; seqlist_t *create_empty_seqlist() { seqlist_t *l = NULL; l = (seqlist_t *)malloc(sizeof(seqlist_t)); if(NULL==l) { printf("malloc is fail\n"); return NULL; free (l); } memset(l,0,sizeof(seqlist_t)); l->n = 0; return l; } int is_full_seqlist(seqlist_t *l) { return l->n == MAX ?1:0; } void insert_name_seqlist(seqlist_t *l,char *data) { strcpy(l->buf[l->n].name,data); return; } void insert_id_seqlist(seqlist_t *l,int data) { l->buf[l->n].id = data; return; } void insert_age_seqlist(seqlist_t *l,int data) { l->buf[l->n].age = data; l->n = l->n + 1; return; } void printf_data_seqlist(seqlist_t *l) { int i = 0; for(i=0;in;i++) { printf("%s %d %d\n",l->buf[i].name,l->buf[i].id,l->buf[i].age); } printf("\n"); } int main() { seqlist_t *l = NULL; l = create_empty_seqlist(); char name[20]={0}; int id = 0; int age = 0; printf("please input %d student data :",MAX); while(!is_full_seqlist(l)) { printf("name :"); scanf("%s",name); insert_name_seqlist(l,name); printf("\n"); printf("id :"); scanf("%d",&id); insert_id_seqlist(l,id); printf("\n"); printf("age :"); scanf("%d",&age); insert_age_seqlist(l,age); printf("\n"); } printf_data_seqlist(l); free(l); l = NULL; return 0; }

得分 100
学习任务

慕神4583458 的学生作业:

file_transfer.c #include "file_transfer.h" #include "debug.h" int recv_protocol_head(int cfd, file_protocol_t *p_head) { int total_size = sizeof(file_protocol_t); int total_recevied = 0; char *buf = (char *)p_head; ssize_t rbytes; for (;;) { rbytes = tcp_recv_pack(cfd, buf+ total_recevied, total_size - total_recevied); if (rbytes == -1) { DEBUG_INFO("[ERROR]: %s", strerror(errno)); return -1; } else if (rbytes == 0) { DEBUG_INFO("[INFO]: client is shutdown"); break; } else if (rbytes > 0) { total_recevied += rbytes; if (total_recevied >= total_size) break; } } if (total_recevied > total_size) { DEBUG_INFO("[INFO]: recevied failed"); return -1; } return 0; } int recv_filedata(int cfd,const char *filename,size_t targetsize) { int total_recevied = 0; ssize_t rbytes, wbytes; int fd = open(filename, O_RDWR | O_TRUNC | O_CREAT); if (fd == -1) { DEBUG_INFO("[INFO]: recevied failed"); return -1; } char buf[1024]; for(;;) { memset(buf, 0, sizeof(buf)); rbytes = recv(cfd, buf, sizeof(buf), 0); if (rbytes == -1) { DEBUG_INFO("[ERROR]: %s", strerror(errno)); return -1; } else if (rbytes == 0) { DEBUG_INFO("[INFO]: client is shutdown"); break; } else if (rbytes > 0) { wbytes = write(fd, buf, rbytes); if (rbytes != wbytes) { DEBUG_INFO("[INFO]: recevied fail"); return -1; } total_recevied += rbytes; if (total_recevied >= targetsize) break; } } if (total_recevied > targetsize) { DEBUG_INFO("[INFO]: recevied too large"); return -1; } close(fd); return total_recevied; } int client_upload_file(int cfd) { file_protocol_t file_protocol; int ret; ret = recv_protocol_head(cfd, &file_protocol); if (ret == -1) return -1; ret = recv_filedata(cfd, file_protocol.filename, file_protocol.filesize); if (ret == -1) return -1; printf("file recevied size = %d", ret); return 0; } int send_protocol_head(const char *filename,int sockfd) { file_protocol_t file_protocol; int fd = open(filename, O_RDONLY); if (fd == -1) { DEBUG_INFO("[ERROR]: %s", strerror(errno)); return -1; } int pos = lseek(fd, 0, SEEK_END); strcpy(file_protocol.filename, filename); file_protocol.filesize = pos; close(fd); int wbytes = tcp_send_pack(sockfd, &file_protocol, sizeof(file_protocol_t)); if (wbytes == -1) { DEBUG_INFO("[ERROR]: %s", strerror(errno)); return -1; } return pos; } int upload_file(const char *filename,int sockfd) { int file_size, wbytes, rbytes, total_sended; file_size = send_protocol_head(filename, sockfd); if (file_size == -1) return -1; int fd = open(filename, O_RDONLY); if (fd == -1) return -1; char buf[1024]; for(;;) { memset(buf, 0, sizeof(buf)); rbytes = read(fd, buf, sizeof(buf)); if (rbytes == -1) { DEBUG_INFO("[ERROR]: %s", strerror(errno)); return -1; } wbytes = tcp_send_pack(sockfd, buf, rbytes); if (wbytes != rbytes) { DEBUG_INFO("[INFO] send failed"); return -1; } total_sended += rbytes; if (total_sended >= rbytes) break; } close(fd); return total_sended; }

微信客服

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

帮助反馈 APP下载

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

公众号

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