Clefairysso 2023-01-08 04:21 采纳率: 50%
浏览 34
已结题

碰到了不会做的C语言链表问题

代码如下:

#include<cstring>
#include<stdio.h>
#include<stdlib.h>
#define n 2
    struct book                        //建立结构体数组
    {
     char zhanghao[8];
    char num[11];
    char suoyinhao[6];
    char time[10];
    char returntime[10];
    }a[n],b[n],c[n];


    struct node                         //建立结构体链表
 {
    char zhanghao[8];
    char num[11];
    char suoyinhao[6];
    char time[10];
    char returntime[10];
    struct node *next;
   };
    FILE *fp1,*fp2,*fp3;
    struct node *insertone(struct node *head1)  /*输入a.txt中所有的信息,并构建新的带头结点的链表*/
     {
        int i;
      struct node *p,*q;
      head1=q=(struct node *)malloc(sizeof(struct node));
     printf("         ~~~~~~欢迎进入借还书信息系统,请输入相关信息~~~~~~\n");
      for(i=0;i<n;i++)
    {
     p=(struct node *)malloc(sizeof(struct node));
    printf("账号:");
    scanf("%s",a[i].zhanghao);
    printf("学号:");
    scanf("%s",a[i].num);
    printf("图书索引号:");
    scanf("%s",a[i].suoyinhao);
    printf("借书时间:");
    scanf("%s",a[i].time);
    printf("\n");
      q->next=p;
      q=p;
     }
      p->next=0;
      return head1;
     }

      struct node *inserttwo(struct node *head2)  /*输入b.txt中所有的信息,并构建新的带头结点的链表*/
     {
        int i;
      struct node *p,*q;
      head2=q=(struct node *)malloc(sizeof(struct node));
      for(i=0;i<n;i++)
    {
          p=(struct node *)malloc(sizeof(struct node));
    printf("账号:");
    scanf("%s",b[i].zhanghao);
    printf("学号:");
    scanf("%s",b[i].num);
    printf("图书索引号:");
    scanf("%s",b[i].suoyinhao);
    printf("还书时间:");
    scanf("%s",b[i].returntime);
    printf("\n");
      q->next=p;
      q=p;
     }
      p->next=0;
      return head2;
     }


      void showone(struct node *head1)    /*保存到a.txt文件中并显示出来*/
     { 
         int i;
       struct node *temp;
      temp=head1;
       printf("a.txt\n");
    fp1=fopen("a.txt","w");
    for(i=0;i<n;i++)
    if(fwrite(&a[i],sizeof(struct book),1,fp1)!=1)          /*将结构体数组a[i]中的数据写入a.txt文件*/
    printf("文件写入数据错误\n");
    fclose(fp1);
    fp1=fopen("a.txt","r");
    printf("\n账号      学号      图书索引号    借书时间\n");         
    for(i=0;i<n;i++)
    {
    fread(&a[i],sizeof(struct book),1,fp1);                                          /*从a.txt文件中读出数据并打印出来*/
    printf("%s %11s %8s %15s\n",a[i].zhanghao,a[i].num,a[i].suoyinhao,a[i].time);
    temp=temp->next;
    }
    fclose(fp1);
     }
   
 void showtwo(struct node *head2)   /*保存到b.txt文件中并显示出来*/
 {
         int i;
       struct node *temp;
      temp=head2;
    printf("b.txt\n");
    fp2=fopen("b.txt","w");
    for(i=0;i<n;i++)
    if(fwrite(&b[i],sizeof(struct book),1,fp2)!=1)                /*将结构体数组b[i]中的数据写入b.txt文件*/
    printf("文件写入数据错误\n");
    fclose(fp2);
    fp2=fopen("b.txt","r");
    printf("\n账号      学号      图书索引号    还书时间\n");
    for(i=0;i<n;i++)
    {
    fread(&b[i],sizeof(struct book),1,fp2);                                                /*从a.txt文件中读出数据并打印出来*/
    printf("%s %11s %8s %15s\n",b[i].zhanghao,b[i].num,b[i].suoyinhao,b[i].returntime);
    temp=temp->next;
    }
    fclose(fp2);
 }

  struct node *loadone(void)  /*将文件a中的数据生成一个链表,如果文件不存在,则是一个空链*/
   {
       int i;
    struct node *head1, *p,*q;
    struct book a[n];
    head1=(struct node *)malloc(sizeof(struct node));
    q=head1=NULL;
 if((fp1=fopen("a.txt","rb"))==NULL)
    return head1;
 else
 {while(!feof(fp1))
    {
      for(i=0;i<n;i++)
        if(fread(&a[i],sizeof(struct book),1,fp1)==1)
        {p=(struct node *)malloc(sizeof(struct node));
          strcpy(p->zhanghao,a[i].zhanghao);
          strcpy(p->num,a[i].num);    
          strcpy(p->suoyinhao,a[i].suoyinhao);
          strcpy(p->time,a[i].time);
          head1=p;
          p->next=q;
          q=head1;
        }
    }
    fclose(fp1);
    return head1;
 }
   }

     
     struct node *loadtwo(void)  /*将文件b中的数据生成一个链表,如果文件不存在,则是一个空链*/
   {
       int i;
    struct node *head2, *p,*q;
    struct book b[n];
    head2=(struct node *)malloc(sizeof(struct node));
    q=head2=NULL;
 if((fp2=fopen("b.txt","rb"))==NULL)
    return head2;
 else
 {while(!feof(fp2))
    {
      for(i=0;i<n;i++)
        if(fread(&b[i],sizeof(struct book),1,fp2)==1)
        {p=(struct node *)malloc(sizeof(struct node));
          strcpy(p->zhanghao,b[i].zhanghao);
          strcpy(p->num,b[i].num);    
          strcpy(p->suoyinhao,b[i].suoyinhao);
          strcpy(p->returntime,b[i].returntime);
          head2=p;
          p->next=q;
          q=head2;
        }
    }
    fclose(fp2);
    return head2;
 }
   }
    void  hebing(struct node *head1, struct node *head2)   /*合并两个文件*/
    {
        char ch;
        FILE *fp1,*fp2,*fp3;
        if((fp1=fopen("a.txt","r"))==NULL)
        {printf("打开文件a.txt失败!\n");
         exit(1);
        }
        else
         printf("打开文件a.txt成功!\n");
       if((fp2=fopen("b.txt","r"))==NULL)
       {printf("打开文件b.txt失败!\n");
        exit(2);
       }
        else
        printf("打开文件b.txt成功!\n");
       if((fp3=fopen("c.txt","r"))==NULL)
       {printf("打开文件c.txt失败!\n");
      exit(3);
       }
      else
     printf("打开文件c.txt成功!\n");
while((ch=fgetc(fp1))!=EOF)
        fputc(ch,fp3);
       while((ch=fgetc(fp2))!=EOF)
      fputc(ch,fp3);
       fclose(fp1);
       fclose(fp2);
       fclose(fp3);
    }

     struct node *loadthree(void)  /*将文件c中的数据生成一个链表,如果文件不存在,则是一个空链*/
   {
       int i;
    struct node *head, *p,*q;
    struct book c[n];
    head=(struct node *)malloc(sizeof(struct node));
    q=head=NULL;
 if((fp3=fopen("c.txt[]","rb"))==NULL)
    return head;
 else
 {while(!feof(fp3))
    {
      for(i=0;i<n;i++)
        if(fread(&c[i],sizeof(struct book),1,fp3)==1)
        {p=(struct node *)malloc(sizeof(struct node));
          strcpy(c[i].zhanghao,p->zhanghao);
          strcpy(c[i].num,p->num);    
          strcpy(c[i].suoyinhao,p->suoyinhao);
           strcpy(c[i].time,p->time);
          strcpy(c[i].returntime,p->returntime);
          head=p;
          p->next=q;
          q=head;
        }
    }
    fclose(fp3);
    return head;
 }
   }
     void showthree(struct node *head)    /*保存c.txt文件中的信息并显示出来*/
     { 
         int i;
       struct node *p;
       p=head;
       printf("c.txt\n");
    fp3=fopen("c.txt","w");
    for(i=0;i<n;i++)
    if(fwrite(&c[i],sizeof(struct book),1,fp3)!=1)
    printf("文件写入数据错误\n");
    fclose(fp3);
    fp3=fopen("c.txt","r");
    printf("\n账号      学号      图书索引号    借书时间      还书时间\n");
    for(i=0;i<n;i++)
    {
    fread(&c[i],sizeof(struct book),1,fp3);
    printf("%s %11s %8s %15s %15s\n",c[i].zhanghao,c[i].num,c[i].suoyinhao,c[i].time,c[i].returntime);
    p=p->next;
    }
    fclose(fp3);
     }

  main()
   {
     struct node *head;
      struct node *head1;
       struct node *head2;
      head1=insertone(head1);    //输入a.txt文件中的信息
    head2=inserttwo(head2);      //输入b.txt文件中的信息
     showone(head1);             //保存a.txt文件中的信息并打印出来
     showtwo(head2);             //保存b.txt文件中的信息并打印出来
    head1=loadone();             //从a.txt中读取相关信息存储到链表中
    head2=loadtwo();             //从b.txt中读取相关信息存储到链表中
    hebing(head1,head2);          //合并两个文件
    head=loadthree();            //对合并后的文件建立链表
     showthree(head);            //保存链表中的信息到文件中并打印出来
   }

img

img

img


a文件和b文件合并后的c文件内容一直不显示呢?

  • 写回答

1条回答 默认 最新

  • m0_54204465 2023-01-08 07:50
    关注
    
    void showthree(struct node *head1, struct node *head2)
    {
        int i;
        struct node *temp1, *temp2;
        temp1 = head1;
        temp2 = head2;
    
        // Open "c.txt" for writing
        fp3 = fopen("c.txt", "w");
        if (fp3 == NULL)
        {
            printf("Error opening file c.txt!\n");
            exit(1);
        }
    
        // Combine information from "a.txt" and "b.txt" and save to "c.txt"
        while (temp1 != NULL && temp2 != NULL)
        {
            if (strcmp(temp1->suoyinhao, temp2->suoyinhao) < 0)
            {
                // Save information from "a.txt" to "c.txt"
                if (fwrite(temp1, sizeof(struct node), 1, fp3) != 1)
                {
                    printf("Error writing to file c.txt!\n");
                    exit(1);
                }
                temp1 = temp1->next;
            }
            else if (strcmp(temp1->suoyinhao, temp2->suoyinhao) > 0)
            {
                // Save information from "b.txt" to "c.txt"
                if (fwrite(temp2, sizeof(struct node), 1, fp3) != 1)
                {
                    printf("Error writing to file c.txt!\n");
                    exit(1);
                }
                temp2 = temp2->next;
            }
            else
            {
                // Save combined information from "a.txt" and "b.txt" to "c.txt"
                strcpy(temp1->returntime, temp2->returntime);
                if (fwrite(temp1, sizeof(struct node), 1, fp3) != 1)
                {
                    printf("Error writing to file c.txt!\n");
                    exit(1);
                }
                temp1 = temp1->next;
                temp2 = temp2->next;
            }
        }
    
        // Save remaining information from "a.txt" or "b.txt" to "c.txt"
        while (temp1 != NULL)
        {
            if (fwrite(temp1, sizeof(struct node), 1, fp3) != 1)
            {
                printf("Error writing to file c.txt!\n");
                exit(1);
            }
            temp1 = temp1->next;
        }
        while (temp2 != NULL)
        {
            if (fwrite(temp2, sizeof(struct node), 1, fp3) != 1)
            {
                printf("Error writing to file c.txt!\n");
                exit(1);
            }
            temp2 = temp2->next;
        }
    
        // Close "c.txt"
        fclose(fp3);
    
        // Open "c.txt" for reading
        fp3 = fopen("c.txt", "r");
        if (fp3 == NULL)
        {
            printf("Error opening file c.txt!\n");
            exit(1);
        }
    
        // Display information from "c.txt" on screen
        printf("c.txt\n");
        while (fread(&c[i], sizeof(struct book), 1, fp3) == 1)
        {
            printf("账号:%s ", c[i].zhanghao);
            printf("学号:%s ", c[i].num);
            printf("图书索引号:%s ", c[i].suoyinhao);
            printf("借书时间:%s ", c[i].time);
            printf("还书时间:%s\n", c[i].returntime);
            i++;
        }
    
        // Close "c.txt"
        fclose(fp3);
    }
    
    head1 = insertone(head1);
    head2 = inserttwo(head2);
    showthree(head1, head2);
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月16日
  • 已采纳回答 1月8日
  • 创建了问题 1月8日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。