| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 983 人关注过本帖
标题:链表出现 Null pointer assignment 希望各位高手帮忙解决,麻烦大家了
只看楼主 加入收藏
eed
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-8
收藏
 问题点数:0 回复次数:5 
链表出现 Null pointer assignment 希望各位高手帮忙解决,麻烦大家了
程序很长,麻烦大家了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 40

/* here is the declaration of a linked list structure */
struct Stdinfo
{
  char num[MAX];  /* for students number */
  char names[MAX];  /* for students name */
  float score;
  struct Stdinfo *nextAddr;
};

/* here is the definition of the first structure pointer */
struct Stdinfo *tosp;


void main()
{
  FILE *outfile;
  int select;
  struct Stdinfo *newaddr;
  void add_record(struct Stdinfo *),delet_record(struct Stdinfo *),check_scores(struct Stdinfo *),recompose_scores(struct Stdinfo *),save_record(struct Stdinfo *),check(struct Stdinfo *);
  tosp=NULL;
  
  outfile=fopen("c:\\record.txt","r");    /* read the information from the file */
  fseek(outfile,0L,SEEK_SET);
  if(outfile==NULL||filelength(outfile)==0)
  {
    printf("\nThere is not any record.");
    printf("\nPlease enter the students' information first.");
  }
  while(!feof(outfile))
  {
    newaddr=(struct Stdinfo *) malloc(sizeof(struct Stdinfo));
    fread(newaddr,sizeof(struct Stdinfo),1,outfile);
    newaddr->nextAddr=tosp;
    tosp=newaddr;
  }
  tosp=tosp->nextAddr;
  fclose(outfile);

  printf("\n********************************************************************************");
  printf("\nEnter a select code:");        /* let user make a choice */
  printf("\n1 for adding new records.");
  printf("\n2 for deleting new records.");
  printf("\n3 for checking all the students' scores.");
  printf("\n4 for recomposing the scores.");
  printf("\n5 for checking the only one student's score.");
  printf("\n6 for exiting.\n");
  scanf("%d",&select);
  printf("\n********************************************************************************\n");
  do
  {
    switch(select) /* use switch to prompt 3 choice */
    {
      case 1: /* start adding new records */
    add_record(tosp);  /* prototype for add_record function */
    break;
      case 2: /* start deleting new records */
    delet_record(tosp);  /* prototype for delet_record function */
    break;
      case 3: /* start checking scores */
    check_scores(tosp);  /* prototype for check_scores function */
    break;
      case 4: /* start recomposing the scores */
    recompose_scores(tosp);
    break;
      case 5: /* start checking scores */
        check(tosp);
        break;    
      case 6: /* exit the program */
     save_record(tosp);   
    exit(1);
    break;
    }  /* end of switch statement */
  printf("\n********************************************************************************");
  printf("\nEnter a select code:");
  printf("\n1 for adding new records.");
  printf("\n2 for deleting new records.");
  printf("\n3 for checking scores.");
  printf("\n4 for recomposing the scores.");
  printf("\n5 for checking the only one student's score.");
  printf("\n6 for exiting.\n");
  }
  while(scanf("%d",&select)!=EOF);   /* use do-while let the 5 functions back to the main function */
  printf("\n********************************************************************************\n");
}

void add_record(struct Stdinfo *p)
{
  char names[MAX];
  char num[MAX];
  float score;
  void push(struct Stdinfo *,char *,char *,float);

  getchar();
  printf("\n--------------------------------------------------------------------------------\n");
  printf("\nEnter as many information as you wish.\n");
  while (1)
  {
    printf("\nPlease enter the student's number:");
    printf("\n(To stop entering,enter a single x)");
    scanf("%s",num);
    getchar();
    if (strcmp(num,"x")==0)
      break;
    printf("Please enter the student's name:");
    gets(names);
    printf("Please enter the student's score:");
    scanf("%f",&score);
    getchar();
   
    printf("\nNow you have input the imformation of this student successful.\n");
    printf("--------------------------------------------------------------------------------\n");
    push(tosp,num,names,score);
  }
}

void push(struct Stdinfo *p,char *num,char *names,float score)
{
  struct Stdinfo *newaddr;
  newaddr=(struct Stdinfo *) malloc(sizeof(struct Stdinfo));
  if (newaddr==(struct Stdinfo *) NULL)
  {
    printf("\nFailed to allocate memory for this structure.\n");
    exit(1);
  }
  strcpy(newaddr->num,num);
  strcpy(newaddr->names,names);
  newaddr->score=score;
  newaddr->nextAddr=p;
  tosp=newaddr;
}

void recompose_scores(struct Stdinfo *p)
{
  struct Stdinfo *newaddr;
  float new_score;
  char info[MAX];

  getchar();
  printf("\nEnter the student's number or name:");
  gets(info);
  while(1)
  {
    if(p!=NULL)
    {
      if(strcmp(p->names,info)==0||strcmp(p->num,info)==0)
      {
       printf("Enter the new score of this student.");
       scanf("%f",&new_score);
       p->score=new_score;
       break;
      }
      else
        p=p->nextAddr;
    }
    else
    {
      printf("\nCan not find this student.\n");
      break;
    }
  }
}


void check_scores(struct Stdinfo *p)
{
  char names[MAX];
  char num[MAX];
  float score;
  struct Stdinfo *f,*q,*s;
  int i=1;

  while(i!=0)
  {
   
    q=tosp;
    s=q->nextAddr;
    i=0;
    while(s!=NULL)
    {
      if(q->score>=s->score)
      {
       q=s;
       s=s->nextAddr;
     }
      else
      {
       f->score=s->score;
       s->score=q->score;
       q->score=f->score;
       strcpy(f->names,s->names);
       strcpy(s->names,q->names);
       strcpy(q->names,f->names);
       strcpy(f->num,s->num);
       strcpy(s->num,q->num);
       strcpy(q->num,f->num);
       q=q->nextAddr;
       s=q->nextAddr;
      
       i++;
     }
    }
  }
  s=tosp;
  
  while(s!=NULL)
  {
    printf("\n%3s %15s %16.1f\n",s->num,s->names,s->score);
    s=s->nextAddr;
  }
}
  


void delet_record(struct Stdinfo *p)
{
  struct Stdinfo *newaddr;
  char info[MAX];
  getchar();  
  while(1)
  {
    printf("\nEnter the student's number or name:");
    printf("\n(To stop entering,enter a single x)\n");
    gets(info);
    if (strcmp(info,"x")==0)
      break;
    while(1)
    {
      if(p!=NULL)
      {
        if(strcmp(p->names,info)==0||strcmp(p->num,info)==0)
        {
            tosp=tosp->nextAddr;
            free(p);
            break;
        }         
        if(strcmp(p->nextAddr->names,info)==0||strcmp(p->nextAddr->num,info)==0)
        {
          p->nextAddr=p->nextAddr->nextAddr;
          break;
        }
        else
            p=p->nextAddr;
      }
      else
      {
        printf("\nCan not find this student.\n");
        break;
      }  
    }
  }  
}

void save_record(struct Stdinfo *tosp)
{
  FILE *infile;

  infile=fopen("c:\\record.txt","w");   /* to record the information in the "record.txt" file */
  if(infile==NULL)
    {
      printf("\nFailed to open the file.\n");
      exit(1);
    }
    while(tosp!=NULL)
    {
      fwrite(tosp,sizeof(struct Stdinfo),1,infile);
      tosp=tosp->nextAddr;
    }
    fclose(infile);
}

void check(struct Stdinfo *p)
{
  char info[MAX];
  struct Stdinfo *newaddr;
  getchar();
  
  while(1)
  {
    printf("\nEnter the student's number or name:");
    printf("\n(To stop entering,enter a single x)\n");
    gets(info);
    if (strcmp(info,"x")==0)
      break;
    while(1)
    {
      if(p!=NULL)
      {
        if(strcmp(p->names,info)==0||strcmp(p->num,info)==0)
       {
         printf("The student's score is:%3.1f",p->score);
         break;
       }
       else
          p=p->nextAddr;
      }
      else
      {
        printf("\nCan not find this student.\n");
        break;
      }
    }
  }   
}
搜索更多相关主题的帖子: 链表 assignment pointer Null 
2008-09-08 11:18
eed
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-8
收藏
得分:0 
补充一下
程序运行完后出现Null pointer assignment ,输出正确,就是存盘少一行。
麻烦大家了,我挺急的。
2008-09-08 11:22
p1s
Rank: 4
等 级:贵宾
威 望:10
帖 子:454
专家分:3
注 册:2005-11-4
收藏
得分:0 
会调试么?最好调试下缩小问题范围啊,这么长的代码,很多都无关的,都不高兴看了。
2008-09-08 13:47
eed
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-8
收藏
得分:0 
再说明一下
我的程序是在TC下编的,主要问题是第一次运行时,退出时出现Null pointer assignment ,我的qq是745740646,希望可以得到大家的帮助,麻烦大家了
2008-09-08 14:10
eed
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-8
收藏
得分:0 
我对链表不是很懂,所以也不好缩小范围。程序运行时不按“6”退出,各功能都基本实现,只是一退就显示那行字,且下次读盘时少一组成绩。求求大家,帮帮忙吧,我已经要崩溃了!!
2008-09-08 14:18
csynyk
Rank: 1
等 级:新手上路
威 望:1
帖 子:114
专家分:0
注 册:2008-8-9
收藏
得分:0 
太长了,一个函数一个函数调试单步调试吧!把当前调试的函数屏蔽起来,找到问题所在!

http://blog./csynyk
2008-09-08 14:39
快速回复:链表出现 Null pointer assignment 希望各位高手帮忙解决,麻烦大家了 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036687 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved