北京免费网站建设模板下载,南江县建设局网站,萍乡做网站的公司有哪些,哈尔滨网站建设团队1#xff1a;编写链表#xff0c;链表里面随便搞点数据 使用 fprintf 将链表中所有的数据#xff0c;保存到文件中 使用 fscanf 读取文件中的数据#xff0c;写入链表中#xff0c;实现#xff0c;当按 ctrl c的时候#xff0c;保存链表
#include stdio.h
#in…1编写链表链表里面随便搞点数据 使用 fprintf 将链表中所有的数据保存到文件中 使用 fscanf 读取文件中的数据写入链表中实现当按 ctrl c的时候保存链表
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
#include signal.h
typedef struct link_list
{int data;struct link_list *next;
}lis,*linkp;linkp p NULL;void insert(int data)
{linkp new (linkp)malloc(sizeof(lis));if(newNULL){perror(NULL);return;}new-datadata;new-next p;p new;
}
void reads() { FILE* rfp fopen(data.txt, r); if (rfp NULL) { perror(reading); return; } int a; while (fscanf(rfp, %d, a) ! EOF) { insert(a); } fclose(rfp); printf(链表已根据data.txt文件更新\n);
} void savefun() { FILE *file fopen(save.txt, w); if (file NULL) { perror(writing); return; } linkp q p; while (q) { fprintf(file, %d\n, q-data); q q-next; } fclose(file); printf(链表已保存到save.txt文件中\n);
} void handler(int signo) { if (signo SIGINT) { printf(按下ctrlc后链表结果,存在save.txt中\n); savefun(); } exit(0);
} int main(int argc, const char *argv[]) { signal(SIGINT, handler); insert(12); insert(34); insert(56); insert(78); reads(); printf(头插法写入链表完成\n); while (1) { //收到SIGINT信号} return 0;
}2编写2个.c文件生成2个可执行文件 1.c 输入正方形的长和宽 或者 三角形的三边长 2.c 输出长方形或者三角形的面积 要求数据通信使用无名管道实现
in.c
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
#include wait.h
int main(int argc, const char *argv[])
{float buf[3];int pipefd[2] {0};pid_t pid;if(pipe(pipefd)-1){perror(pipe);return 1;}char std[2];std[0]pipefd[0];std[1]pipefd[1];pid fork();if(pid-1){perror(fork);return 1;}if(pid0){execlp(./area,2,(std[0]),(std[1]),NULL);perror(execlp);exit(0);}else{printf(输入正方形的长和宽 或者 三角形三边 \n);printf(tips:如果是正方形第三个数输入0\n);scanf(%f %f %f,buf[0],buf[1],buf[2]);write(pipefd[1],buf,sizeof(buf));wait(NULL);close(pipefd[0]);close(pipefd[1]);}return 0;
}area.c
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
#include math.h
int main(int argc, const char *argv[])
{float a,b,c;double area;int rp*(argv[1]);int wp*(argv[2]);if (read(rp,a,sizeof(a)) -1) {perror(read a);return 1;}if (read(rp,b,sizeof(b)) -1) {perror(read b);return 1;}if(read(rp,c,sizeof(c)) -1){perror(read c);return 1;}if (c0){if(ab){// 正方形的面积area a * b;printf(正方形的面积为%.2f\n, area);}else{printf(输入长方形\n);}}else {// 三角形的面积海伦公式double p (a b c) / 2.0;area sqrt(p * (p - a) * (p - b) * (p - c));printf(三角形的面积为%.2f\n, area);} return 0;
}3使用有名管道实现2个终端之间的互相聊天功能 要求能够并发
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
int main(int argc, const char *argv[])
{if(mkfifo(./myfifo1, 0664) -1){perror(mkfifo1 error);return -1;}if(mkfifo(./myfifo2, 0664) -1){perror(mkfifo2 error);return -1;}getchar();system(rm myfifo1);system(rm myfifo2);return 0;
}
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.hint main(int argc, char const *argv[])
{pid_t pidfork();if(pid0){int wfd -1;if((wfd open(./myfifo1, O_WRONLY)) -1){perror(open error);return -1;}char wbuf[128] ;while(1){printf(这里是1号机,请输入);fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf)-1] 0;write(wfd, wbuf, strlen(wbuf));if(strcmp(wbuf,quit) 0)break;}close(wfd); }else if(pid0){int rfd -1;if((rfd open(./myfifo2, O_RDONLY)) -1){perror(open error);return -1;}char rbuf[128] ;while(1){bzero(rbuf, sizeof(rbuf));read(rfd, rbuf, sizeof(rbuf));printf(\t\t\t\t\t1号机收到的数据为%s\n, rbuf);if(strcmp(rbuf,quit) 0)break;}close(rfd);exit(EXIT_SUCCESS);}elseperror(fork); return 0;
}#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
int main(int argc, char const *argv[])
{pid_t pidfork();if(pid0){int rfd -1;if((rfd open(./myfifo1, O_RDONLY)) -1){perror(open error);return -1;}char rbuf[128] ;while(1){bzero(rbuf, sizeof(rbuf));read(rfd, rbuf, sizeof(rbuf));printf(\t\t\t\t\t2号机收到的数据为%s\n, rbuf);if(strcmp(rbuf,quit) 0)break;}close(rfd); }else if(pid0){int wfd -1;if((wfd open(./myfifo2, O_WRONLY)) -1){perror(open error);return -1;}char wbuf[128] ;while(1){printf(这里是2号机,请输入);fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf)-1] 0;write(wfd, wbuf, strlen(wbuf));if(strcmp(wbuf,quit) 0)break;}close(wfd); exit(EXIT_SUCCESS);}elseperror(fock); return 0;
}