18292846017 发表于 2019-9-2 21:43:12

很奇怪的错误,明明是有)的

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct student *creat(); //新建
struct student *del(struct student *heat,int n);//删除
struct student *cha(struct student *heat);//插入
void print(student *heat);//打印
#define LEN sizeof(struct student);
struct student
{
        int num;
        float score;
        struct student *next;
};
void main()
{
        struct student *p;
        int m;
        p=creat();//新建
    print(p);

        printf("请输入要删除的学生的学号\n\n");//删除
        scanf("%d",&m);
        print(del(p,m));


        printf("结果\n");//插入
        print(cha(p));

}
struct student *creat()
{
        struct student *heat;
        struct student *p,*q;
        q=p=(struct student *)malloc(LEN);
        heat=NULL;
        while(1)
        {
                int n=0;
          n++;
          if(n==1)
          {
           p=heat;
          }
          else
          {
                  q->next = p;
          }
          q=p;
          
          printf("请输入学号:");
          scanf("%d",&p->num);
          printf("请输入成绩:");
          scanf("%f",&p->score);
        }
       
        return heat;
}
void print(student *heat)
{
       
        struct student *p;
        printf("输入结果\n");
        p=heat;
        while(p)
        {
                printf("学号是%3d 的成绩是%f\n",p->num,p->score);
                p->next;
        }


}
struct student *del(struct student *heat,int n)
{
        struct student *p,*q;
        p=heat;
        if(p==NULL)
        {
                printf("无信息可删除\n\n");
        }
                else
                {
                                while(p->num!=n&&p->next!=NULL)
                                {
                                        q=p;
                                    p->next;
                                }
                               if(n==p->num)
                               {
                                       if(p==heat)
                                                heat=p->next;
                                       q->next=p->next;
                                }
                                printf("删除成功\n\n");
   
                                return heat;
                }

}
struct student *cha(struct student *heat)
{
        struct student *p,*q,*w;
        p=w=(struct student *)malloc(LEN);
        int a;
        p=heat;
        if(p==NULL)//判断是否是空表
        {
                printf("请输入信息\n\n");
                printf("请输入学号:");
                scanf("%d",p->num);
                printf("请输入成绩:");
                scanf("%f",p->score);
                printf("输入成功\n\n");
        }
        printf("请输入学号:");
        scanf("%d",q->num);
    printf("请输入成绩:");
        scanf("%f",q->score);
        if(q->num>p->num)//插入头
        {
                q=heat;
                p=q->next;
        }
        else
        {
                        while(q->num <p->num)
                        {
                                p->next;
                        }
                        if(p->next==NULL)//插在尾
                        {
                                p->next=q;
                                q->next=NULL;
                        }
                        w=p->next;   //插在中间
                        p->next=q;
                        q->next=w;
        }
        return heat;


}

superbe 发表于 2019-9-2 22:50:50

本帖最后由 superbe 于 2019-9-3 13:23 编辑

1. #define LEN sizeof(struct student); 把后面的分号去掉。
编译预处理时,q=p=(struct student *)malloc(LEN); 这行里的LEN被sizeof(struct student); 代替,就变成了 q=p=(struct student *)malloc(sizeof(struct student);); 现在看是不是;前面少了一个)。
2. cha函数:scanf输入没加&,变量q未初始化。
其它还有错误,改了这些再调试吧。
PS:我是用.cpp编译的(把代码保存为.cpp)。

ba21 发表于 2019-9-2 22:56:05

首先在我的编译器中,有些代码顺序不能错,其次还有一处 看注释
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define LEN sizeof(struct student)


struct student
{
      int num;
      float score;
      struct student *next;
};

struct student *creat(); // 新建
struct student *del(struct student *heat,int n); // 删除
struct student *cha(struct student *heat); // 插入
void print(struct student *heat);// 打印 struct student *heat

void main()
{
      struct student *p;
      int m;
      p=creat();//新建
    print(p);

      printf("请输入要删除的学生的学号\n\n");//删除
      scanf("%d",&m);
      print(del(p,m));


      printf("结果\n");//插入
      print(cha(p));

}

struct student *creat()
{
      struct student *heat;
      struct student *p,*q;
      q=p=(struct student *)malloc(LEN);
      heat=NULL;
      while(1)
      {
                int n=0;
          n++;
          if(n==1)
          {
         p=heat;
          }
          else
          {
                  q->next = p;
          }
          q=p;
         
          printf("请输入学号:");
          scanf("%d",&p->num);
          printf("请输入成绩:");
          scanf("%f",&p->score);
      }
      
      return heat;
}

void print(struct student *heat)
{
      
      struct student *p;
      printf("输入结果\n");
      p=heat;
      while(p)
      {
                printf("学号是%3d 的成绩是%f\n",p->num,p->score);
                p->next;
      }


}

struct student *del(struct student *heat,int n)
{
      struct student *p,*q;
      p=heat;
      if(p==NULL)
      {
                printf("无信息可删除\n\n");
      }
                else
                {
                              while(p->num!=n&&p->next!=NULL)
                              {
                                        q=p;
                                    p->next;
                              }
                                 if(n==p->num)
                                 {
                                       if(p==heat)
                                                heat=p->next;
                                       q->next=p->next;
                              }
                              printf("删除成功\n\n");
   
                              return heat;
                }

}

struct student *cha(struct student *heat)
{
      struct student *p,*q,*w;
                int a;

      p=w=(struct student *)malloc(LEN);      
      p=heat;
      if(p==NULL)//判断是否是空表
      {
                printf("请输入信息\n\n");
                printf("请输入学号:");
                scanf("%d",p->num);
                printf("请输入成绩:");
                scanf("%f",p->score);
                printf("输入成功\n\n");
      }
          printf("请输入学号:");
      scanf("%d",q->num);
    printf("请输入成绩:");
      scanf("%f",q->score);
      if(q->num>p->num)//插入头
      {
                q=heat;
                p=q->next;
      }
      else
      {
                        while(q->num <p->num)
                        {
                              p->next;
                        }
                        if(p->next==NULL)//插在尾
                        {
                              p->next=q;
                              q->next=NULL;
                        }
                        w=p->next;   //插在中间
                        p->next=q;
                        q->next=w;
      }
      return heat;


}

页: [1]
查看完整版本: 很奇怪的错误,明明是有)的