搜索
查看: 743|回复: 9
打印 上一主题 下一主题

一运行就停止工作

[复制链接]
跳转到指定楼层
楼主
发表于 2014-12-8 22:45:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
#include <stdio.h>
#include <stdlib.h>
float a[3];
struct stduent
{
int num;
struct stduent *next;
};
struct stduent *creat (int n)
{
struct stduent *head,*last,*p;
int i;
float s1,s2,s3;
head=last=NULL;
for (i=0;i<n;i++)
{
  p=(struct stduent *)malloc(sizeof(struct stduent));
  printf("\nPlease input student%d number:",i++);
  scanf ("%d",&p->num);
  printf ("score1:");
  scanf ("%f",&s1);
  printf ("score2:");
  scanf ("%f",&s2);
  printf ("score3:");
  scanf ("%f",&s3);
  a[i]=(((s1)+(s2)+(s3))/3);
  p->next=NULL;
  if (i=0)
    head=last=p;
        else
          {
           last->next=p;
           last=p;
          }
}
return head;
}
main(){
int i=0;
struct stduent *head,*p;
head=creat(5);
while (head!=NULL)
{
  printf ("%5d,%5.1f\n",p->num,a[i]);
  p=p->next;
  i++;
}
}

最佳答案

查看完整内容

31行 if (i=0) i=0的值还是0, 这个选择支从来进不去,所以还是对NULL进行操作 49行 a 这通不过编译的 24,26,28三行,float对应的占位符是%f,不是%d
沙发
发表于 2014-12-8 22:45:29 | 只看该作者
31行  if (i=0)
i=0的值还是0, 这个选择支从来进不去,所以还是对NULL进行操作

49行 a[i ][i ]
这通不过编译的

24,26,28三行,float对应的占位符是%f,不是%d
板凳
发表于 2014-12-9 01:31:41 | 只看该作者
head=last=NULL;
for (i=0;i<n;i++)
{
  p=(struct stduent *)malloc(sizeof(struct stduent));
  printf("\nPlease input student%d number:",i++);
  scanf ("%d",&p->num);
  printf ("score1:");
  scanf ("%f",&s1);
  printf ("score2:");
  scanf ("%f",&s2);
  printf ("score3:");
  scanf ("%f",&s3);
  a[ i ]=(((s1)+(s2)+(s3))/3);
  p->next=NULL;
  if (i=0)
    head=last=p;
        else
          {
           last->next=p;
注意红色的部分,last一开始是NULL,第一轮循环你多写一个i++变成1,最后的循环进入的是else分支,然后last->net,对NULL指针进行操作,程序就崩溃了
地板
发表于 2014-12-9 01:41:38 | 只看该作者
另外,分数存储在数组a里面,a的长度只有3,存不下5个数据

struct stduent *head,*p;
head=creat(5);
while (head!=NULL)
{
  printf ("%5d,%5.1f\n",p->num,a[ i ]);
  p=p->next;

这里用head和p两个指针也不对,p没有初始化,直接p->next也是会让程序崩溃的

最后,student拼写错了……
5#
 楼主| 发表于 2014-12-9 09:31:21 | 只看该作者
本帖最后由 rosynirvana 于 2014-12-9 11:21 编辑
rosynirvana 发表于 2014-12-9 01:41
另外,分数存储在数组a里面,a的长度只有3,存不下5个数据

struct stduent *head,*p;

修改后这样 还是不行

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. float a[5];
  4. struct student
  5.         {
  6.         int num;
  7.         float score1;
  8.         float score2;
  9.         float score3;
  10.         struct student *next;
  11. };
  12. struct student *creat (int n)
  13.         {
  14.         struct student *head,*last,*p;
  15.         int i;
  16.         head=last=NULL;
  17.         for (i=0;i<n;i++)
  18.         {
  19.                 p=(struct student *)malloc(sizeof(struct student));
  20.                 printf("\nPlease input student%d number:",i);
  21.                 scanf ("%d",&p->num);
  22.                 printf (" score1:");
  23.                 scanf ("%d",&p->score1);
  24.                 printf (" score2:");
  25.                 scanf ("%d",&p->score2);
  26.                 printf (" score3:");
  27.                 scanf ("%d",&p->score3);
  28.                 a[i]=((p->score1)+(p->score2)+(p->score3))/3 ;
  29.                 p->next=NULL;
  30.                 if (i=0)
  31.                   head=last=p;
  32.         else
  33.           {
  34.                   last->next=p;
  35.                   last=p;
  36.           }
  37.         }
  38.         return head;
  39. }

  40. main()
  41.         {
  42.         struct student *head;
  43.         head=creat(5);
  44.         int i=0;
  45.         while (head!=NULL)
  46.         {
  47.                 printf("%5d,%5.1f\n",head->num,a[i][i]);
  48.                 head=head->next;
  49.                 i++;
  50.         }
  51. }
复制代码
6#
 楼主| 发表于 2014-12-9 18:14:12 | 只看该作者
rosynirvana 发表于 2014-12-9 13:13
31行  if (i=0)
i=0的值还是0, 这个选择支从来进不去,所以还是对NULL进行操作

谢啦  终于成功了 我在前面加了个char  name[20];
后面输入的时候加了剧gets(p->name);
怎么直接跳过这句了?
7#
发表于 2014-12-9 19:06:14 | 只看该作者
zcy961024 发表于 2014-12-9 18:14
谢啦  终于成功了 我在前面加了个char  name[20];
后面输入的时候加了剧gets(p->name);
怎么直接跳 ...

你加在哪里了? 贴完整代码
8#
 楼主| 发表于 2014-12-9 22:24:34 | 只看该作者
rosynirvana 发表于 2014-12-9 19:06
你加在哪里了? 贴完整代码

#include <stdio.h>
#include <stdlib.h>
float a[5];
struct student
        {
        int num;
       char name[20];
        float score1;
        float score2;
        float score3;
        struct student *next;
};
struct student *creat (int n)
        {
        struct student *head,*last,*p;
        int i;
        head=last=NULL;
        for (i=0;i<n;i++)
        {
                p=(struct student *)malloc(sizeof(struct student));
                printf("\nPlease input student%d number:",i);
                scanf ("%d",&p->num);
                printf(" name:");
                gets(p->name);
                printf (" score1:");
                scanf ("%f",&p->score1);
                printf (" score2:");
                scanf ("%f",&p->score2);
                printf (" score3:");
                scanf ("%f",&p->score3);
                a=((p->score1)+(p->score2)+(p->score3))/3 ;
                p->next=NULL;
                if (i==0)
                  head=last=p;
        else
          {
                  last->next=p;
                  last=p;
          }
        }
        return head;
}

main()
        {
        struct student *head;
        head=creat(5);
        int i=0;
        while (head!=NULL)
        {
                printf("%5d,%5.1f\n",head->num,a);
                head=head->next;
                i++;
        }
}
9#
发表于 2014-12-9 22:59:56 | 只看该作者
前面的scanf在输入流上面残留了一个回车
然后gets读到这个回车直接认为输入结束了

在两行之间加个getchar(); 把回车消耗掉就好了
10#
 楼主| 发表于 2014-12-10 21:49:51 | 只看该作者
rosynirvana 发表于 2014-12-9 22:59
前面的scanf在输入流上面残留了一个回车
然后gets读到这个回车直接认为输入结束了

完全好了 谢谢大神
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表