03.08

1、使用链表的加载和保存的代码,实现,当按 ctrl + c的时候,保存链表

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <stdlib.h>

#include <signal.h>

typedef struct Link{

    int data;

    struct Link* next;

    struct Link* prev;

}link_t;

link_t* gol_head;

link_t* create_node(){

    link_t* head = malloc(sizeof(link_t));

    head->next = head;

    head->prev = head;

    return head;

}

void insert_node(link_t* head,int data){

    link_t* newnode = create_node();

    newnode->data = data;

    link_t* tail = head->prev;

    tail->next = newnode;

    newnode->next = head;

    head->prev = newnode;

    newnode->prev = tail;

}

link_t* find_node(link_t* head,int data){

    link_t* p = head->next;

    while(p != head){

        if(p->data == data){

            return p;

        }

        p = p->next;

    }

    return head;

}

void save_link(link_t* head,const char* filename){

    FILE* fp = fopen(filename,"w");

    if(fp==NULL){return;}

    link_t* p = head->next;

    while(p != head){

        fprintf(fp,"%d\n",p->data);

        p = p->next;

    }

    fclose(fp);

}

void load_link(link_t* head,const char* filename){

    FILE* fp = fopen(filename,"r");

    if(fp==NULL){return;}

    while(1){

        int data = 0;

        int res = fscanf(fp,"%d\n",&data);

        if(res!=1){break;}

        //link_t* newnode = create_node();// 通过if判断表示读取到了数据,读取到数据之后才去创建节点

        insert_node(head,data);

    }

    fclose(fp);

}

void remove_node(link_t* head,int data){

    link_t* tar = find_node(head,data);

    if(tar==head){return;}

    tar->prev->next = tar->next;

    tar->next->prev = tar->prev;

    free(tar);

}

void show_link(link_t* head){

    link_t* p = head->next;

    while(p != head){

        printf("%d\n",p->data);

        p = p->next;

    }

}

void handler(int signo){

    if(signo == SIGINT){

        save_link(gol_head,"./hw.txt");

    }

}

int main(int argc, const char *argv[])

{

    int ch = 0;

    link_t* head = create_node();

    gol_head = head;

    load_link(head,"./hw.txt");

    signal(SIGINT,handler);

    while(1){

        printf("1:添加数据\n");

        printf("2:查看数据\n");

        //printf("3:保存数据\n");

        scanf("%d",&ch);

        getchar();

        switch(ch){

            case 1:{

                int data = 0;

                printf("请输入数据:");

                scanf("%d",&data);

                getchar();

                insert_node(head,data);

                break;

            }

            case 2:

                show_link(head);

                break;

            case 3:

                //save_link(head,"./hw.txt");

                break;

            default:

                break;

        }

    }

    return 0;

}

2、编写2个.c文件,生成2个可执行文件 1.c 输入正方形的长和宽 或者 三角形的三边长 2.c 输出长方形或者三角形的面积 要求数据通信使用无名管道实现

1.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[])

{

    // 假设读取管道的描述符是 fd

//  while(1){

    int fd = atoi(argv[0]);

    //printf("fd = %d\n",fd);

    while(1){

        double arr[3] = {0};

        read(fd,arr,24);

        //printf("arr[0] = %g\n",arr[0]);

        if(arr[2] == 0){

            // 说明计算的是矩形

            double s = arr[0] * arr[1];

            printf("矩形的面积为:%g\n",s);

        }else{

            double a = arr[0];

            double b = arr[1];

            double c = arr[2];

            double p = (a + b + c) / 2;

            double s = sqrt(p*(p-a)*(p-b)*(p-c));

            printf("三角形的面积为:%g\n",s);

        }

    }

//  }

   

    return 0;

}

2.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[])

{

    int pipefd[2] = {0};

    if(pipe(pipefd)==-1){

        perror("pipe");

        return 11;

    }

    int res = fork();

    if(res > 0){

        while(1){

            double arr[3] = {0};

            printf("请输入矩形2边长或者三角形的3边长:");

            scanf("%lf %lf %lf",arr,arr+1,arr+2);

            while(getchar()!='\n');

            write(pipefd[1],arr,24);

            //wait(0);

            usleep(1000);

        }

    }else if(res == 0){

        char str_fd[16] = {0};

        sprintf(str_fd,"%d",pipefd[0]);//将管道读端描述符转换成字符串类型

        //printf("str_fd = %s\n",str_fd);

        //while(1){

            execl("./hw2-1",str_fd,NULL);

            printf("进程替换失败\n");

        //}

    }

   

    return 0;

}

3、使用有名管道,实现2个终端之间的互相聊天功能 要求:能够并发

1.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>

void* run(void* arg){

    if(access("myfifo1",F_OK)==-1){

        mkfifo("myfifo2",0666);

    }

    int rfd = open("./myfifo2",O_RDONLY);

    int size = 0;

    char buf[128] = {0};

    while(1){

        memset(buf,0,size);

        int res = read(rfd,buf,128);

        if(res == 0){

            printf("管道破裂\n");

            break;

        }

        printf("读取到的消息为:%s\n",buf);

    }

}

int main(int argc, const char *argv[])

{

    pthread_t id;

    pthread_create(&id,NULL,run,NULL);

    pthread_detach(id);

    if(access("myfifo1",F_OK)==-1){

        mkfifo("myfifo1",0666);

    }

    int wfd = open("./myfifo1",O_WRONLY | O_TRUNC);

    int size = 0;

    char buf[128] = {0};

    while(1){

        memset(buf,0,size);

        printf("请输入:");

        scanf("%128s",buf);

        while(getchar()!=10);

        size = strlen(buf);

        write(wfd,buf,size);

    }

    return 0;

}

2.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>

void* run(void* arg){

    if(access("myfifo2",F_OK)==-1){

        mkfifo("myfifo2",0666);

    }

    int wfd = open("./myfifo2",O_WRONLY | O_TRUNC);

    int size = 0;

    char buf[128] = {0};

    while(1){

        memset(buf,0,size);

        printf("请输入:");

        scanf("%128s",buf);

        while(getchar()!=10);

        size = strlen(buf);

        write(wfd,buf,size);

    }

}

int main(int argc, const char *argv[])

{

    pthread_t id;

    pthread_create(&id,NULL,run,NULL);

    pthread_detach(id);

    if(access("myfifo1",F_OK)==-1){

        mkfifo("myfifo1",0666);

    }

    int rfd = open("./myfifo1",O_RDONLY);

    int size = 0;

    char buf[128] = {0};

    while(1){

        memset(buf,0,size);

        int res = read(rfd,buf,128);

        if(res == 0){

            printf("管道破裂\n");

            break;

        }

        printf("读取到的消息为:%s\n",buf);

    }


 

    return 0;

}

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值