QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 865|回复: 10

第二次文件读取段错误????

[复制链接]
发表于 2004-6-11 02:58:52 | 显示全部楼层 |阅读模式
第一次保存没有段错误,但程序第二次运行,插入新的信息再保存,就出现段错误了。且刚才插入的信息无法保存。

[code:1]
/*简单学生信息表v2.0*/
#include <stdio.h>            /*基本输入输出*/
#include <string.h>           /*字符串函数*/
#include <stdlib.h>           
#define true 1
#define false 0
#define maxsize 20            /*姓名长度*/

typedef struct courseInfo     /*课程结构体*/
{
  float chinese;
  float english;
  float math;
} courseTp;

typedef struct dateInfo       /*出生日期结构体*/
{
  int year;
  int month;
  int day;
} dateTp;

typedef struct node           /*学生信息结构体*/
{
  int num;                    /*学号*/
  char name[maxsize];         /*姓名*/
  char sex[10];               /*性别*/
  struct dateInfo date;       /*出身日期*/
  int age;                    /*年龄*/
  struct courseInfo course;   /*课程*/
  struct node *next;
} Student;

Student* Initiate_Student();                   /*初始化表*/
Student* Create_Student(Student *head);        /*建表*/
Student* Find_Student(Student *head, int num); /*查找结点位置*/
Student* Insert_Student(Student *head);        /*插入结点*/
Student* Delete_Student(Student *head, int i); /*删除结点*/
Student* Destroy_Students(Student *head);      /*销毁表*/
Student* Load_Students(Student *head);         /*读取表*/
int Print_Student(Student *head);              /*屏显表*/
int LocationName_Student(Student *head);       /*定位(字符串)*/        
int Save_Students(Student *head);              /*保存*/
int LocateNum_Student(Student *head, int num); /*定位(数字)*/


int main (void)
{
  FILE *fp;
  Student *head, *p;
  int choose, num;
  float average, chinese, math, english;
  
  head = Initiate_Student();
  printf("%s\n%s\n%s\n%s",
     "*************************************************",
     "*         简单学生信息表系统 for lluct.         *",
     "* Simple students information system for lluct  *",
     "*************************************************");
  
  if ((fp = fopen ("students.dat", "r")) == NULL)
    {
      head = Create_Student(head);
    }
  else
    head = Load_Students(head);
  while (true)
    {
      printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
             "###菜单列表  MENU###",
             "1:查看学生列表  SHOW LIST OF STUDENTS",
             "2:查找学生序号  SEARCH LOCATION OF STUDENT",
             "3:按姓名查找    SEARCH NAME OF STUDENT",
             "4:按学号查找    SEARCH NO. OF STUDENT",
             "5:插入学生信息  INSERT INFO OF STUDENT",
             "6:删除学生信息  DELETE INFO OF STUDENT",
             "7:保存当前信息  SAVE",
             "8:读取文件信息  LOAD",
             "0:退出          EXIT",
             "请选择          PLEASE CHOOSE:");
      scanf("%d", &choose);
      
      switch (choose)
    {
    case 1:
      Print_Student(head);
      break;
    case 2:
      printf("\n请输入要查找的序号  INPUT LOCATION FOR SEARCH:");
      scanf("%d", &num);
      p = Find_Student(head, num);
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;
      printf("\n该序号的学生信息为  NOW LIST:\n\n");
      printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.  \n");
      printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
      printf("%4d  %4d  %6s  %4s  %4d-%2d-%2d   %4d  %4g  %4g  %4g  %4g\n",
             num, p -> num, p -> name, p ->sex, p -> date.year,
             p -> date.month, p -> date.day, p -> age,
             p -> course.chinese, p -> course.math, p -> course.english,
             average);
      printf("\n");
      break;
    case 3:
      LocationName_Student(head);
      break;
    case 4:
      printf("\n请输入要查找的学生学号  INPUT SNO FOR SEARCH:");
      scanf("%d", &num);
      LocateNum_Student(head, num);
      break;
    case 5:
      head = Insert_Student(head);
      break;
    case 6:
      printf("\n请输入要删除的位置(序号)  INPUT LOCATION FOR DELETE:");
      scanf("%d", &num);
      head = Delete_Student(head, num);
      break;
    case 7:
      Save_Students(head);
      break;
    case 8:
      head = Load_Students(head);
      break;
    case 0:
      Destroy_Students(head);
      exit(0);
    default:
      printf("\n选择错误!  CHOOSE ERROR!\n");
    }
    }
}

Student* Initiate_Student (void)
{
  Student *head;
  head = (Student *) malloc (sizeof(Student));
  head -> next = NULL;
  return (head);
}

Student* Create_Student (Student *head)
{
  Student *new, *end;
  int age, num;
  int year, month, day;
  float chinese, english, math;
  char input;
  int count;
  int choose;
  int nextStu = true, sexFin = true, dateFin = true;
  end = head;

  while (nextStu)
    {
      sexFin = true, dateFin = true;
      count = 0;
      new = (Student *) malloc (sizeof(Student));
      printf("\n请输入学生学号(四位)  NO.(MAX 4):");
      scanf("%d", &num);
      getchar();
      printf("请输入学生姓名  NAME:");

      while ((input = getchar()) != '\n')
    {
      new -> name[count] = input;
      count++;
    }
      new -> name[count] = '\0';
     
      while (sexFin)
    {
      printf("请选择学生性别(1:男性 2:女性)  SEX(1:MALE 2:FEMALE):");
      scanf("%d", &choose);
      switch (choose)
        {
        case 1:
          strcpy (new -> sex, "男 M");
          sexFin = false;
          break;
        case 2:
          strcpy (new -> sex, "女 F");
          sexFin = false;
          break;
        default:
          printf("\n选择错误,请重新选择!  ERROR\n");
        }
    }

      while (dateFin)
    {
      printf("请输入学生生日(年-月-日)  BORN(YYYY-MM-DD):");
      scanf("%d-%d-%d", &year, &month, &day);
      if ((year > 2000) || (year < 1949))
        printf("\n年份输入错误,请重新输入!  ERROR\n");
      else if ((month >12) || (month < 1))
        printf("\n月份输入错误,请重新输入!  ERROR\n");
      else if ((day > 31) || (day < 1))
        printf("\n天数输入错误,请重新输入!  ERROR\n");
      else
        dateFin = false;
    }

      printf("请输入学生年龄  AGE:");
      scanf("%d", &age);
      
      printf("请输入语文成绩(可带小数)  SCORES OF CHINESE:");
      scanf("%f", &chinese);
      printf("请输入数学成绩(同上)  SCORES OF MATH:");
      scanf("%f", &math);
      printf("请输入英语成绩(同上)  SCORES OF ENGLISH:");
      scanf("%f", &english);

      new -> num = num;
      new -> date.year = year;
      new -> date.month = month;
      new -> date.day = day;
      new -> age = age;
      new -> course.chinese = chinese;
      new -> course.math = math;
      new -> course.english = english;
      end -> next = new;
      end = new;
      getchar();
      printf("\n是否继续输入(Y/N)  CONTINUE?");
      scanf("%c", &input);
      if (input == 'Y' || input == 'y')
    nextStu = true;
      else
    nextStu = false;
    }
  return (head);
}

int Print_Student (Student *head)
{
  Student *p;
  int j = 0;
  float average, chinese, english, math;
  p = head;


  printf("\n目前学生列表  NOW LIST:\n\n");
  printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.\n");
  printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
  printf("-------------------------------------------------------------------------------\n");
  if (head != NULL)
    {
      p = p -> next;
      while (p != NULL)
    {
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;
      printf("(%d)", j + 1);
      j++;
      printf("   %d  %6s  %4s  %4d-%2d-%2d   %4d  %4g  %4g  %4g  %4g\n",
         p -> num, p -> name, p ->sex, p -> date.year,
         p -> date.month, p -> date.day, p -> age,
         p -> course.chinese, p -> course.math, p -> course.english,
         average);
      p = p -> next;
    }
    }
  printf("-------------------------------------------------------------------------------\n");
  printf("共有%d个学生  TOTAL %d STUDENT(S).\n\n", j, j);
  return false;
}

Student* Find_Student (Student *head, int i)
{
  Student *p;
  int j = 0;
  p = head;
  while ((p -> next != NULL) && (j < i))
    {
      p = p -> next;
      j++;
    }
  if (i == j)
    return (p);
  else
    {
      printf("该位置不存在!  LOCATION NOT FOUND!\n");
      return false;
    }
}

Student* Insert_Student (Student *head)
{
  Student *p, *s;
  int age, num;
  int year, month, day;
  char input;
  float chinese, math, english;
  int count;
  int sexFin = true, dateFin = true;
  int choose, insertLocation;

  printf("\n请输入要插入的位置(序号):  LOCATION:");
  scanf("%d", &insertLocation);
  p = Find_Student(head, insertLocation - 1);
  if (p == NULL)
    {
      printf("不存在第%d个位置!  LOCATION NOT FOUND\n", insertLocation);
      return false;
    }
  else
    {
      sexFin = true, dateFin = true;
      count = 0;
      s = (Student *) malloc (sizeof(Student));
      printf("\n请输入学生学号  NO.:");
      scanf("%d", &num);
      getchar();
      printf("请输入学生姓名  NAME:");

      while ((input = getchar()) != '\n')
        {
          s -> name[count] = input;
          count++;
        }
      s -> name[count] = '\0';

      while (sexFin)
        {
          printf("请选择学生性别(1:男性 2:女性)  SEX(1:MALE 2:FEMAL):");
          scanf("%d", &choose);
         
      switch (choose)
          {
          case 1:
        strcpy (s -> sex, "男 M");
        sexFin = false;
        break;
          case 2:
        strcpy (s -> sex, "女 F");
        sexFin = false;
        break;
          default:
        printf("\n选择错误,请重新选择!  CHOOSE ERROR!\n");
          }
        }

      while (dateFin)
        {
          printf("请输入学生生日(年-月-日)  BORN(YYYY-MM-DD):");
          scanf("%d-%d-%d", &year, &month, &day);
          if ((year > 2000) || (year < 1949))
            printf("\n年份输入错误,请重新输入!  ERROR\n");
          else if ((month >12) || (month < 1))
            printf("\n月份输入错误,请重新输入!  ERROR\n");
          else if ((day > 31) || (day < 1))
            printf("\n天数输入错误,请重新输入!  ERROR\n");
          else
            dateFin = false;
        }

      printf("请输入学生年龄  AGE:");
      scanf("%d", &age);

      printf("请输入语文成绩  SCORES OF CHINESE:");
      scanf("%f", &chinese);
      printf("请输入数学成绩  SCORES OF MATH:");
      scanf("%f", &math);
      printf("请输入英语成绩  SCORES OF ENGLISH:");
      scanf("%f", &english);

      s -> num = num;
      s -> date.year = year;
      s -> date.month = month;
      s -> date.day = day;
      s -> age = age;
      s -> course.chinese = chinese;
      s -> course.math = math;
      s -> course.english = english;
      s -> next = p -> next;
      p -> next = s;
    }
  return (head);
}

Student* Delete_Student (Student *head, int i)
{
  Student *p, *s;
  
  p = Find_Student(head, i - 1);
  if ((p != NULL) && (p -> next != NULL))
    {
      s = p -> next;
      p -> next = s -> next;
      free(s);
    }
  else
    {
      printf("不存在第%d个位置!  LOCATION NOT FOUND\n", i);
      return 0;
    }
  return (head);
}      

int LocationName_Student (Student *head)
{
  Student *p;
  char input;
  char siName[20];
  int count = 0, j = 0;
  float average, chinese, english, math;
  p = head;

  getchar();
  printf("请输入要查找的姓名  NAME FOR SEARCH:");
  while ((input = getchar ()) != '\n')
    {
      siName[count] = input;
      count++;
    }
  siName[count] = '\0';

  while ((p -> next != NULL) && (strcmp(siName, p -> name) != 0))
    {
      p = p -> next;
      j++;
    }
  if (strcmp (siName, p -> name) == 0)
    {
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;

      printf("\n该序号的学生信息为  NOW LIST:\n\n");
      printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.\n");
      printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
      printf("%4d  %4d  %6s  %4s  %4d-%2d-%2d   %4d  %4g  %4g  %4g  %4g\n",
         j, p -> num, p -> name, p ->sex, p -> date.year,
         p -> date.month, p -> date.day, p -> age,
         p -> course.chinese, p -> course.math, p -> course.english
         , average);
      printf("\n");

    }
  else
    {
      printf("\n查无此人!  STUDENT NOT FOUND\n");
      return false;
    }
  return false;
}

int LocateNum_Student (Student *head, int i)
{
  int j = 0;
  float average, english, chinese, math;
  Student *p;
  p = head;

  while ((p -> next != NULL) && (p -> num != i))
    {
      p = p -> next;
      j++;
    }
  if (p -> num == i)
    {
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;
      printf("\n该序号的学生信息为  NOW LIST:\n\n");
      printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.\n");
      printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
      printf("%4d  %4d  %6s  %4s  %d-%d-%d   %4d  %4g  %4g  %4g  %4g\n",
         j, p -> num, p -> name, p ->sex, p -> date.year,
         p -> date.month, p -> date.day, p -> age,
         p -> course.chinese, p -> course.math, p -> course.english,
         average);
      printf("\n");

    }
  else
    {
      printf("\n查无此人!  STUDENT NOT FOUND\n");
      return false;
    }
  return false;
}

Student* Destroy_Students (Student *head)
{
  int j = 0;
  Student *p;
  p = head;


  if (head -> next == NULL)
    {
      free (head);
      return 0;
    }
  else
    {
      while (p != NULL)
    {
      j++;
      p = p -> next;
    }
      while (j == 0)
    {
      head = Delete_Student(head, j);
      j--;
    }
      free(head);
      return 0;
    }
}


int Save_Students(Student *head)
{

    FILE *fp;
    int j = 0;
    Student *p;
    p = head;
   
    if ((fp = fopen("students.dat", "wb")) == NULL)
    {
        printf("file can not open!\n");
        exit(false);
    }
    while (p != NULL)
    {
        j++;
        p = p -> next;
    }
    printf("\n文件保存成功!  FILE SAVED!\n\n");

    fwrite(head, sizeof(Student),j,fp);
    fclose(fp);
    return 0;
}

Student* Load_Students(Student *head)
{
    FILE *fp;
    int j = 0;
   
    if ((fp = fopen("students.dat", "r")) == NULL)
    {
        printf("系统错误!  SYSTEM ERROR!\n");
        exit(false);
    }

    printf("\n源文件有几个学生信息  HOW MANY STUDNET IN SOURCE FILES?");
    scanf("%d", &j);
    j++;

    fread(head, sizeof(Student), j , fp);

    printf("\n文件读取成功!  FILE LOAD\n\n");
    fclose(fp);
    return (head);
}
[/code:1]
发表于 2004-6-11 09:02:11 | 显示全部楼层
先自己用gdb调试一下,看看程序在哪个点崩溃。
回复

使用道具 举报

发表于 2004-6-11 14:38:30 | 显示全部楼层
还是讲讲你的设计思路吧。这样可以更快抓住要害。
回复

使用道具 举报

发表于 2004-6-11 14:45:12 | 显示全部楼层
还有,先把这些问题处理一下(每次你的程序都这样,记得以后带 -Wal l选项   )
[code:1]test14.c: In function `main':
test14.c:54: warning: too few arguments for format
test14.c:82: warning: implicit declaration of function `Print_Student'
test14.c:103: warning: implicit declaration of function `LocationName_Student'
test14.c:108: warning: implicit declaration of function `LocateNum_Student'
test14.c:125: warning: implicit declaration of function `Delete_File'
test14.c:129: warning: implicit declaration of function `exit'
test14.c: In function `Initiate_Student':
test14.c:139: warning: implicit declaration of function `malloc'
test14.c: In function `Print_Student':
test14.c:270: warning: `return' with no value, in function returning non-void
test14.c: In function `Delete_Student':
test14.c:395: warning: implicit declaration of function `free'
test14.c: In function `LocationName_Student':
test14.c:409: warning: unused variable `diName'
test14.c:410: warning: unused variable `num'
test14.c:451: warning: control reaches end of non-void function
test14.c: In function `LocateNum_Student':
test14.c:487: warning: control reaches end of non-void function
test14.c: In function `Destroy_Students':
test14.c:499: warning: `return' with no value, in function returning non-void
test14.c:514: warning: `return' with no value, in function returning non-void
test14.c: In function `Delete_File':
test14.c:576: warning: implicit declaration of function `system'
test14.c:587: warning: control reaches end of non-void function
[/code:1]
回复

使用道具 举报

 楼主| 发表于 2004-6-11 15:53:07 | 显示全部楼层
[quote:4e8e09e4d1="不详"]还有,先把这些问题处理一下(每次你的程序都这样,记得以后带 -Wal l选项   )
[code:1]test14.c: In function `main':
test14.c:54: warning: too few arguments for format
test14.c:82: warning: implicit declaration of function `Print_Student'
test14.c:103: warning: implicit declaration of function `LocationName_Student'
test14.c:108: warning: implicit declaration of function `LocateNum_Student'
test14.c:125: warning: implicit declaration of function `Delete_File'
test14.c:129: warning: implicit declaration of function `exit'
test14.c: In function `Initiate_Student':
test14.c:139: warning: implicit declaration of function `malloc'
test14.c: In function `Print_Student':
test14.c:270: warning: `return' with no value, in function returning non-void
test14.c: In function `Delete_Student':
test14.c:395: warning: implicit declaration of function `free'
test14.c: In function `LocationName_Student':
test14.c:409: warning: unused variable `diName'
test14.c:410: warning: unused variable `num'
test14.c:451: warning: control reaches end of non-void function
test14.c: In function `LocateNum_Student':
test14.c:487: warning: control reaches end of non-void function
test14.c: In function `Destroy_Students':
test14.c:499: warning: `return' with no value, in function returning non-void
test14.c:514: warning: `return' with no value, in function returning non-void
test14.c: In function `Delete_File':
test14.c:576: warning: implicit declaration of function `system'
test14.c:587: warning: control reaches end of non-void function
[/code:1][/quote]
改好了。但有三个地方不知怎么改。
[code:1]
students_pro.c: In function `main':
students_pro.c:129: warning: implicit declaration of function `exit'
students_pro.c: In function `Initiate_Student':
students_pro.c:139: warning: implicit declaration of function `malloc'
students_pro.c: In function `Delete_Student':
students_pro.c:395: warning: implicit declaration of function `free'
[/code:1]
回复

使用道具 举报

 楼主| 发表于 2004-6-11 15:55:01 | 显示全部楼层
[quote:69616747fc="sagaeon"]还是讲讲你的设计思路吧。这样可以更快抓住要害。[/quote]
就是用单链表实现阿。每个学生信息用一个结点表示。进行一系列的运算(删除阿,插入阿)。现在的问题就是保存文件和读取文件有问题。
回复

使用道具 举报

发表于 2004-6-11 15:59:36 | 显示全部楼层
那改后的贴出来看看(如果还有问题的话 )   你缺 #include <stdlib.h>
回复

使用道具 举报

 楼主| 发表于 2004-6-11 16:03:00 | 显示全部楼层
[quote:8ab5e74c3d="sagaeon"]那改后的贴出来看看(如果还有问题的话 )   你缺 #include <stdlib.h>[/quote]
就是一楼的源代码阿。我直接覆盖了原来的文件。编译加wall就是上面的三个错误不懂改(加stdlib没有啦,但原来的问题还是有)。
程序主要问题就是第二次不能保存(也就是保存读取再保存),也无法读取。(显示的都是乱码)
回复

使用道具 举报

发表于 2004-6-11 16:11:00 | 显示全部楼层
[free@sagaeon program]$ gcc -Wall test15.c
test15.c: In function `main':
test15.c:55: warning: too few arguments for format
test15.c:83: warning: implicit declaration of function `Print_Student'
test15.c:104: warning: implicit declaration of function `LocationName_Student'
test15.c:109: warning: implicit declaration of function `LocateNum_Student'
test15.c:126: warning: implicit declaration of function `Delete_File'
test15.c: In function `Print_Student':
test15.c:271: warning: `return' with no value, in function returning non-void
test15.c: In function `LocationName_Student':
test15.c:410: warning: unused variable `diName'
test15.c:411: warning: unused variable `num'
test15.c:452: warning: control reaches end of non-void function
test15.c: In function `LocateNum_Student':
test15.c:488: warning: control reaches end of non-void function
test15.c: In function `Destroy_Students':
test15.c:500: warning: `return' with no value, in function returning non-void
test15.c:515: warning: `return' with no value, in function returning non-void
test15.c: In function `Delete_File':
test15.c:588: warning: control reaches end of non-void function

我编译还是不行?
回复

使用道具 举报

 楼主| 发表于 2004-6-11 16:14:51 | 显示全部楼层
[quote:ad1762d411="sagaeon"][free@sagaeon program]$ gcc -Wall test15.c
test15.c: In function `main':
test15.c:55: warning: too few arguments for format
test15.c:83: warning: implicit declaration of function `Print_Student'
test15.c:104: warning: implicit declaration of function `LocationName_Student'
test15.c:109: warning: implicit declaration of function `LocateNum_Student'
test15.c:126: warning: implicit declaration of function `Delete_File'
test15.c: In function `Print_Student':
test15.c:271: warning: `return' with no value, in function returning non-void
test15.c: In function `LocationName_Student':
test15.c:410: warning: unused variable `diName'
test15.c:411: warning: unused variable `num'
test15.c:452: warning: control reaches end of non-void function
test15.c: In function `LocateNum_Student':
test15.c:488: warning: control reaches end of non-void function
test15.c: In function `Destroy_Students':
test15.c:500: warning: `return' with no value, in function returning non-void
test15.c:515: warning: `return' with no value, in function returning non-void
test15.c: In function `Delete_File':
test15.c:588: warning: control reaches end of non-void function

我编译还是不行?[/quote]

[code:1]
/*简单学生信息表v2.0*/
#include <stdio.h>            /*基本输入输出*/
#include <string.h>           /*字符串函数*/
#include <stdlib.h>
#define true 1
#define false 0
#define maxsize 20            /*姓名长度*/

typedef struct courseInfo     /*课程结构体*/
{
  float chinese;
  float english;
  float math;
} courseTp;

typedef struct dateInfo       /*出生日期结构体*/
{
  int year;
  int month;
  int day;
} dateTp;

typedef struct node           /*学生信息结构体*/
{
  int num;                    /*学号*/
  char name[maxsize];         /*姓名*/
  char sex[10];               /*性别*/
  struct dateInfo date;       /*出身日期*/
  int age;                    /*年龄*/
  struct courseInfo course;   /*课程*/
  struct node *next;
} Student;

Student* Initiate_Student();                   /*初始化表*/
Student* Create_Student(Student *head);        /*建表*/
Student* Find_Student(Student *head, int num); /*查找结点位置*/
Student* Insert_Student(Student *head);        /*插入结点*/
Student* Delete_Student(Student *head, int i); /*删除结点*/
Student* Destroy_Students(Student *head);      /*销毁表*/
Student* Load_Students(Student *head);         /*读取表*/
int Print_Student(Student *head);              /*屏显表*/
int LocationName_Student(Student *head);       /*定位(字符串)*/        
int Save_Students(Student *head);              /*保存*/
int LocateNum_Student(Student *head, int num); /*定位(数字)*/


int main (void)
{
  FILE *fp;
  Student *head, *p;
  int choose, num;
  float average, chinese, math, english;
  
  head = Initiate_Student();
  printf("%s\n%s\n%s\n%s",
     "*************************************************",
     "*         简单学生信息表系统 for lluct.         *",
     "* Simple students information system for lluct  *",
     "*************************************************");
  
  if ((fp = fopen ("students.dat", "r")) == NULL)
    {
      head = Create_Student(head);
    }
  else
    head = Load_Students(head);
  while (true)
    {
      printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
             "###菜单列表  MENU###",
             "1:查看学生列表  SHOW LIST OF STUDENTS",
             "2:查找学生序号  SEARCH LOCATION OF STUDENT",
             "3:按姓名查找    SEARCH NAME OF STUDENT",
             "4:按学号查找    SEARCH NO. OF STUDENT",
             "5:插入学生信息  INSERT INFO OF STUDENT",
             "6:删除学生信息  DELETE INFO OF STUDENT",
             "7:保存当前信息  SAVE",
             "8:读取文件信息  LOAD",
             "0:退出          EXIT",
             "请选择          PLEASE CHOOSE:");
      scanf("%d", &choose);
      
      switch (choose)
    {
    case 1:
      Print_Student(head);
      break;
    case 2:
      printf("\n请输入要查找的序号  INPUT LOCATION FOR SEARCH:");
      scanf("%d", &num);
      p = Find_Student(head, num);
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;
      printf("\n该序号的学生信息为  NOW LIST:\n\n");
      printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.  \n");
      printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
      printf("%4d  %4d  %6s  %4s  %4d-%2d-%2d   %4d  %4g  %4g  %4g  %4g\n",
             num, p -> num, p -> name, p ->sex, p -> date.year,
             p -> date.month, p -> date.day, p -> age,
             p -> course.chinese, p -> course.math, p -> course.english,
             average);
      printf("\n");
      break;
    case 3:
      LocationName_Student(head);
      break;
    case 4:
      printf("\n请输入要查找的学生学号  INPUT SNO FOR SEARCH:");
      scanf("%d", &num);
      LocateNum_Student(head, num);
      break;
    case 5:
      head = Insert_Student(head);
      break;
    case 6:
      printf("\n请输入要删除的位置(序号)  INPUT LOCATION FOR DELETE:");
      scanf("%d", &num);
      head = Delete_Student(head, num);
      break;
    case 7:
      Save_Students(head);
      break;
    case 8:
      head = Load_Students(head);
      break;
    case 0:
      Destroy_Students(head);
      exit(0);
    default:
      printf("\n选择错误!  CHOOSE ERROR!\n");
    }
    }
}

Student* Initiate_Student (void)
{
  Student *head;
  head = (Student *) malloc (sizeof(Student));
  head -> next = NULL;
  return (head);
}

Student* Create_Student (Student *head)
{
  Student *new, *end;
  int age, num;
  int year, month, day;
  float chinese, english, math;
  char input;
  int count;
  int choose;
  int nextStu = true, sexFin = true, dateFin = true;
  end = head;

  while (nextStu)
    {
      sexFin = true, dateFin = true;
      count = 0;
      new = (Student *) malloc (sizeof(Student));
      printf("\n请输入学生学号(四位)  NO.(MAX 4):");
      scanf("%d", &num);
      getchar();
      printf("请输入学生姓名  NAME:");

      while ((input = getchar()) != '\n')
    {
      new -> name[count] = input;
      count++;
    }
      new -> name[count] = '\0';
     
      while (sexFin)
    {
      printf("请选择学生性别(1:男性 2:女性)  SEX(1:MALE 2:FEMALE):");
      scanf("%d", &choose);
      switch (choose)
        {
        case 1:
          strcpy (new -> sex, "男 M");
          sexFin = false;
          break;
        case 2:
          strcpy (new -> sex, "女 F");
          sexFin = false;
          break;
        default:
          printf("\n选择错误,请重新选择!  ERROR\n");
        }
    }

      while (dateFin)
    {
      printf("请输入学生生日(年-月-日)  BORN(YYYY-MM-DD):");
      scanf("%d-%d-%d", &year, &month, &day);
      if ((year > 2000) || (year < 1949))
        printf("\n年份输入错误,请重新输入!  ERROR\n");
      else if ((month >12) || (month < 1))
        printf("\n月份输入错误,请重新输入!  ERROR\n");
      else if ((day > 31) || (day < 1))
        printf("\n天数输入错误,请重新输入!  ERROR\n");
      else
        dateFin = false;
    }

      printf("请输入学生年龄  AGE:");
      scanf("%d", &age);
      
      printf("请输入语文成绩(可带小数)  SCORES OF CHINESE:");
      scanf("%f", &chinese);
      printf("请输入数学成绩(同上)  SCORES OF MATH:");
      scanf("%f", &math);
      printf("请输入英语成绩(同上)  SCORES OF ENGLISH:");
      scanf("%f", &english);

      new -> num = num;
      new -> date.year = year;
      new -> date.month = month;
      new -> date.day = day;
      new -> age = age;
      new -> course.chinese = chinese;
      new -> course.math = math;
      new -> course.english = english;
      end -> next = new;
      end = new;
      getchar();
      printf("\n是否继续输入(Y/N)  CONTINUE?");
      scanf("%c", &input);
      if (input == 'Y' || input == 'y')
    nextStu = true;
      else
    nextStu = false;
    }
  return (head);
}

int Print_Student (Student *head)
{
  Student *p;
  int j = 0;
  float average, chinese, english, math;
  p = head;


  printf("\n目前学生列表  NOW LIST:\n\n");
  printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.\n");
  printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
  printf("-------------------------------------------------------------------------------\n");
  if (head != NULL)
    {
      p = p -> next;
      while (p != NULL)
    {
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;
      printf("(%d)", j + 1);
      j++;
      printf("   %d  %6s  %4s  %4d-%2d-%2d   %4d  %4g  %4g  %4g  %4g\n",
         p -> num, p -> name, p ->sex, p -> date.year,
         p -> date.month, p -> date.day, p -> age,
         p -> course.chinese, p -> course.math, p -> course.english,
         average);
      p = p -> next;
    }
    }
  printf("-------------------------------------------------------------------------------\n");
  printf("共有%d个学生  TOTAL %d STUDENT(S).\n\n", j, j);
  return false;
}

Student* Find_Student (Student *head, int i)
{
  Student *p;
  int j = 0;
  p = head;
  while ((p -> next != NULL) && (j < i))
    {
      p = p -> next;
      j++;
    }
  if (i == j)
    return (p);
  else
    {
      printf("该位置不存在!  LOCATION NOT FOUND!\n");
      return false;
    }
}

Student* Insert_Student (Student *head)
{
  Student *p, *s;
  int age, num;
  int year, month, day;
  char input;
  float chinese, math, english;
  int count;
  int sexFin = true, dateFin = true;
  int choose, insertLocation;

  printf("\n请输入要插入的位置(序号):  LOCATION:");
  scanf("%d", &insertLocation);
  p = Find_Student(head, insertLocation - 1);
  if (p == NULL)
    {
      printf("不存在第%d个位置!  LOCATION NOT FOUND\n", insertLocation);
      return false;
    }
  else
    {
      sexFin = true, dateFin = true;
      count = 0;
      s = (Student *) malloc (sizeof(Student));
      printf("\n请输入学生学号  NO.:");
      scanf("%d", &num);
      getchar();
      printf("请输入学生姓名  NAME:");

      while ((input = getchar()) != '\n')
        {
          s -> name[count] = input;
          count++;
        }
      s -> name[count] = '\0';

      while (sexFin)
        {
          printf("请选择学生性别(1:男性 2:女性)  SEX(1:MALE 2:FEMAL):");
          scanf("%d", &choose);
         
      switch (choose)
          {
          case 1:
        strcpy (s -> sex, "男 M");
        sexFin = false;
        break;
          case 2:
        strcpy (s -> sex, "女 F");
        sexFin = false;
        break;
          default:
        printf("\n选择错误,请重新选择!  CHOOSE ERROR!\n");
          }
        }

      while (dateFin)
        {
          printf("请输入学生生日(年-月-日)  BORN(YYYY-MM-DD):");
          scanf("%d-%d-%d", &year, &month, &day);
          if ((year > 2000) || (year < 1949))
            printf("\n年份输入错误,请重新输入!  ERROR\n");
          else if ((month >12) || (month < 1))
            printf("\n月份输入错误,请重新输入!  ERROR\n");
          else if ((day > 31) || (day < 1))
            printf("\n天数输入错误,请重新输入!  ERROR\n");
          else
            dateFin = false;
        }

      printf("请输入学生年龄  AGE:");
      scanf("%d", &age);

      printf("请输入语文成绩  SCORES OF CHINESE:");
      scanf("%f", &chinese);
      printf("请输入数学成绩  SCORES OF MATH:");
      scanf("%f", &math);
      printf("请输入英语成绩  SCORES OF ENGLISH:");
      scanf("%f", &english);

      s -> num = num;
      s -> date.year = year;
      s -> date.month = month;
      s -> date.day = day;
      s -> age = age;
      s -> course.chinese = chinese;
      s -> course.math = math;
      s -> course.english = english;
      s -> next = p -> next;
      p -> next = s;
    }
  return (head);
}

Student* Delete_Student (Student *head, int i)
{
  Student *p, *s;
  
  p = Find_Student(head, i - 1);
  if ((p != NULL) && (p -> next != NULL))
    {
      s = p -> next;
      p -> next = s -> next;
      free(s);
    }
  else
    {
      printf("不存在第%d个位置!  LOCATION NOT FOUND\n", i);
      return 0;
    }
  return (head);
}      

int LocationName_Student (Student *head)
{
  Student *p;
  char input;
  char siName[20];
  int count = 0, j = 0;
  float average, chinese, english, math;
  p = head;

  getchar();
  printf("请输入要查找的姓名  NAME FOR SEARCH:");
  while ((input = getchar ()) != '\n')
    {
      siName[count] = input;
      count++;
    }
  siName[count] = '\0';

  while ((p -> next != NULL) && (strcmp(siName, p -> name) != 0))
    {
      p = p -> next;
      j++;
    }
  if (strcmp (siName, p -> name) == 0)
    {
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;

      printf("\n该序号的学生信息为  NOW LIST:\n\n");
      printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.\n");
      printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
      printf("%4d  %4d  %6s  %4s  %4d-%2d-%2d   %4d  %4g  %4g  %4g  %4g\n",
         j, p -> num, p -> name, p ->sex, p -> date.year,
         p -> date.month, p -> date.day, p -> age,
         p -> course.chinese, p -> course.math, p -> course.english
         , average);
      printf("\n");

    }
  else
    {
      printf("\n查无此人!  STUDENT NOT FOUND\n");
      return false;
    }
  return false;
}

int LocateNum_Student (Student *head, int i)
{
  int j = 0;
  float average, english, chinese, math;
  Student *p;
  p = head;

  while ((p -> next != NULL) && (p -> num != i))
    {
      p = p -> next;
      j++;
    }
  if (p -> num == i)
    {
      chinese = p -> course.chinese;
      english = p -> course.english;
      math = p -> course.math;
      average = (chinese+english+math)/3;
      printf("\n该序号的学生信息为  NOW LIST:\n\n");
      printf("LOC.  NO.   NAME    SEX   BORN         AGE   CH.   MATH  EN.   AVE.\n");
      printf("序号  学号  姓  名  性别  出生日期     年龄  语文  数学  英语  平均分\n");
      printf("%4d  %4d  %6s  %4s  %d-%d-%d   %4d  %4g  %4g  %4g  %4g\n",
         j, p -> num, p -> name, p ->sex, p -> date.year,
         p -> date.month, p -> date.day, p -> age,
         p -> course.chinese, p -> course.math, p -> course.english,
         average);
      printf("\n");

    }
  else
    {
      printf("\n查无此人!  STUDENT NOT FOUND\n");
      return false;
    }
  return false;
}

Student* Destroy_Students (Student *head)
{
  int j = 0;
  Student *p;
  p = head;


  if (head -> next == NULL)
    {
      free (head);
      return 0;
    }
  else
    {
      while (p != NULL)
    {
      j++;
      p = p -> next;
    }
      while (j == 0)
    {
      head = Delete_Student(head, j);
      j--;
    }
      free(head);
      return 0;
    }
}


int Save_Students(Student *head)
{

    FILE *fp;
    int j = 0;
    Student *p;
    p = head;
   
    if ((fp = fopen("students.dat", "wb")) == NULL)
    {
        printf("file can not open!\n");
        exit(false);
    }
    while (p != NULL)
    {
        j++;
        p = p -> next;
    }
    printf("\n文件保存成功!  FILE SAVED!\n\n");

    fwrite(head, sizeof(Student),j,fp);
    fclose(fp);
    return 0;
}

Student* Load_Students(Student *head)
{
    FILE *fp;
    int j = 0;
   
    if ((fp = fopen("students.dat", "r")) == NULL)
    {
        printf("系统错误!  SYSTEM ERROR!\n");
        exit(false);
    }

    printf("\n源文件有几个学生信息  HOW MANY STUDNET IN SOURCE FILES?");
    scanf("%d", &j);
    j++;

    fread(head, sizeof(Student), j , fp);

    printf("\n文件读取成功!  FILE LOAD\n\n");
    fclose(fp);
    return (head);
}
[/code:1]
回复

使用道具 举报

发表于 2004-6-11 19:17:47 | 显示全部楼层
你的Load_Students好象完全不对哦!记录与字符串的区别?保存也不对吧,要保存记录,似乎应该用你自己的方式串行化,同样取出也要将串行化的数据还原成记录。
回复

使用道具 举报

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

本版积分规则

GMT+8, 2024-11-8 05:43 , Processed in 0.047699 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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