鱼C论坛

 找回密码
 立即注册
查看: 3011|回复: 2

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

[复制链接]
发表于 2019-9-2 21:43:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#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;


}

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 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)。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-2 22:56:05 | 显示全部楼层
首先在我的编译器中,有些代码顺序不能错,其次还有一处 看注释
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define LEN sizeof(struct student)


  5. struct student
  6. {
  7.         int num;
  8.         float score;
  9.         struct student *next;
  10. };

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

  15. void main()
  16. {
  17.         struct student *p;
  18.         int m;
  19.         p=creat();//新建
  20.     print(p);

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


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

  26. }

  27. struct student *creat()
  28. {
  29.         struct student *heat;
  30.         struct student *p,*q;
  31.         q=p=(struct student *)malloc(LEN);
  32.         heat=NULL;
  33.         while(1)
  34.         {
  35.                 int n=0;
  36.           n++;
  37.           if(n==1)
  38.           {
  39.            p=heat;
  40.           }
  41.           else
  42.           {
  43.                   q->next = p;
  44.           }
  45.           q=p;
  46.          
  47.           printf("请输入学号:");
  48.           scanf("%d",&p->num);
  49.           printf("请输入成绩:");
  50.           scanf("%f",&p->score);
  51.         }
  52.         
  53.         return heat;
  54. }

  55. void print(struct student *heat)
  56. {
  57.         
  58.         struct student *p;
  59.         printf("输入结果\n");
  60.         p=heat;
  61.         while(p)
  62.         {
  63.                 printf("学号是%3d 的成绩是%f\n",p->num,p->score);
  64.                 p->next;
  65.         }


  66. }

  67. struct student *del(struct student *heat,int n)
  68. {
  69.         struct student *p,*q;
  70.         p=heat;
  71.         if(p==NULL)
  72.         {
  73.                 printf("无信息可删除\n\n");
  74.         }
  75.                 else
  76.                 {
  77.                                 while(p->num!=n&&p->next!=NULL)
  78.                                 {
  79.                                         q=p;
  80.                                     p->next;
  81.                                 }
  82.                                  if(n==p->num)
  83.                                  {
  84.                                          if(p==heat)
  85.                                                 heat=p->next;
  86.                                          q->next=p->next;
  87.                                 }
  88.                                 printf("删除成功\n\n");
  89.    
  90.                                 return heat;
  91.                 }

  92. }

  93. struct student *cha(struct student *heat)
  94. {
  95.         struct student *p,*q,*w;
  96.                 int a;

  97.         p=w=(struct student *)malloc(LEN);        
  98.         p=heat;
  99.         if(p==NULL)//判断是否是空表
  100.         {
  101.                 printf("请输入信息\n\n");
  102.                 printf("请输入学号:");
  103.                 scanf("%d",p->num);
  104.                 printf("请输入成绩:");
  105.                 scanf("%f",p->score);
  106.                 printf("输入成功\n\n");
  107.         }
  108.           printf("请输入学号:");
  109.         scanf("%d",q->num);
  110.     printf("请输入成绩:");
  111.         scanf("%f",q->score);
  112.         if(q->num>p->num)//插入头
  113.         {
  114.                 q=heat;
  115.                 p=q->next;
  116.         }
  117.         else
  118.         {
  119.                         while(q->num <p->num)
  120.                         {
  121.                                 p->next;
  122.                         }
  123.                         if(p->next==NULL)//插在尾
  124.                         {
  125.                                 p->next=q;
  126.                                 q->next=NULL;
  127.                         }
  128.                         w=p->next;   //插在中间
  129.                         p->next=q;
  130.                         q->next=w;
  131.         }
  132.         return heat;


  133. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 06:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表