weixin_73800050 2022-12-22 20:53 采纳率: 100%
浏览 14
已结题

为什么第一个函数的fou循环只能输入一次值


// 第七次作业第三题vs.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"malloc.h"
#define LEN sizeof(struct student)
struct student
{int num;char name[10];struct student*next;
};

struct student* create(struct student *p1,struct student*p2,int n)
{
    int i;
    struct student *head;
    head=p1;
    for(i=0;i<n;i++)
    {
        p2=p1;
     scanf_s("%d%s",&p1->num,&p1->name);
     fflush(stdin);
     if(i==(n-1))
     {p1->next=NULL;}
     else
     {     p1=(struct student*)malloc(LEN);
      }  p2->next=p1;
    }
    return (head);
}

void f(struct student *head)
{
    
    struct student *p1,*p2;
    int i=0;
    p1=head;
    while(p1->next!=NULL)
    {p2=p1;
    p1=p1->next;
    i++;
    }
    i++;
    printf("主函数的个数为:%d\n",i);
}

struct student* fv(struct student *head)
{
    struct student *p1,*p2,*p3,*head2;
    p1=head;p2=head;p3=head;
    while(p1!=NULL)
    {
        if(p1==p2)
        {p1=p1->next;
        p2->next=NULL;
        p2=p1;
        p3=p1;
        p3->next=head;
        p1=p1->next;
        }
        else
        {
            p2=p1;
            p1=p1->next;
            p2->next=p3;
        }
    }
    head2=p2;
    return head2;
}

void list(struct student*head)
{
    struct student *p;
    p=head;
    while(p!=NULL)
    {
        printf("学号/名字:%s  %s\n",p->num,p->name);
        p=p->next;
    }

}

void search(int m,struct  student* head)
{
    struct student *p;
    p=head;
    while(p!=NULL)
    {
        if(p->num==m)
            printf("\n学生学号/名字为:%d   %s",p->num,p->name);
        else p=p->next;
    }
    if(p==NULL)
    {printf("\n查无此人");}
}

int _tmain(int argc, _TCHAR* argv[])
{
    struct student *p1,*head,*p2,*head2;
        p1=(struct student*)malloc(LEN);
        p2=p1;
    int n,m;
    printf("请输入学生总人数:");
    scanf_s("%d",&n);
    fflush(stdin);
    head=create(p1,p2,n);
    list(head);
    f(head);
    head2=fv(head);
    list(head2);
    printf("请输入要检索的学生的学号:");
    scanf_s("%d",&m);
    search(m,head2);
    return 0;
}


  • 写回答

1条回答 默认 最新

  • 浪客 2022-12-22 21:31
    关注

    scanf_s("%d%s",&p1->num,p1->name);
    字符串不用加&

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月1日
  • 已采纳回答 12月24日
  • 创建了问题 12月22日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大