| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 827 人关注过本帖, 1 人收藏
标题:C语言版学生管理系统
只看楼主 加入收藏
skyuser213
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:117
注 册:2010-5-9
收藏(1)
 问题点数:0 回复次数:6 
C语言版学生管理系统
程序代码:
// StudentsDirectory.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
  struct tagStudents StudentInformation;
  while(1)
  {
    switch (SelectStuIndexInterface())
    {
    case 1://添加学生
      StudentsAdd(&StudentInformation);
      break;
    case 2://删除学生
      StudentsDelete();
      break;
    case 3://修改学生
      StudentChange();
      break;
    case 4://查询学生
      StudentsSelect();
      break;
    case 0:
      return 0;
      break;
    }
  }
    return 0;
}

搜索更多相关主题的帖子: C语言 系统 学生 管理 
2010-06-25 17:23
skyuser213
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:117
注 册:2010-5-9
收藏
得分:0 
程序代码:
// StuManage.h: interface for the StuManage class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUMANAGE_H__765DE2AA_AF04_49FD_BB2D_526810E0DA2D__INCLUDED_)
#define AFX_STUMANAGE_H__765DE2AA_AF04_49FD_BB2D_526810E0DA2D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct tagStudents
{
  char m_StuNumber[16];  //编号
  char m_StuName[32];    //姓名
  char m_StuSex;         //性别
  int m_StuAge;    //年龄
  char m_StuTelephone[16];//电话
  float m_StuHeight;     //身高
  char m_StuQQ[16];      //QQ
  char m_Mark;           //标记
};

//添加学生
void StudentsAdd(tagStudents *Student);
//查询学生
//返回值:0表示返回
int StudentsSelect();
//删除学生
int StudentsDelete();
//修改学生
int StudentChange();

#endif // !defined(AFX_STUMANAGE_H__765DE2AA_AF04_49FD_BB2D_526810E0DA2D__INCLUDED_)


// StuManage.cpp: implementation of the StuManage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StuManage.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//添加学生
void StudentsAdd(tagStudents *Student)
{
  system("cls");
  while (1)
  {
    printf("请输入学生编号:");
    fflush(stdin);
    scanf("%15s", Student->m_StuNumber);
    if (RepeatnNumber(Student->m_StuNumber) == 2)
    {
      printf("输入的ID重复,请重新输入!\r\n");
    }
    else
    {
      break;
    }
  }

  printf("请输入学生姓名:");
  fflush(stdin);
  scanf("%31s", Student->m_StuName);

  while (1)
  { 
    printf("请输入学生性别(M:男、W:女、U:未知、O:其他):\r\n");
    fflush(stdin);
    scanf("%c", &Student->m_StuSex);
    if (Student->m_StuSex == 'M' ||
        Student->m_StuSex == 'm' ||
        Student->m_StuSex == 'W' ||
        Student->m_StuSex == 'w' ||
        Student->m_StuSex == 'U' ||
        Student->m_StuSex == 'u' ||
        Student->m_StuSex == 'O' ||
        Student->m_StuSex == 'o')
    {
      break;
    }
    else
    {
      printf("输入错误,请重新输入:\r\n");
    }
  }

  while (1)
  {
    printf("请输入学生年龄:");
    fflush(stdin);
    scanf("%d", &Student->m_StuAge);
    if (Student->m_StuAge > 150 ||
        Student->m_StuAge <= 0)
    {
      printf("您输入的年龄超出范围,请重新输入!\r\n");
    }
    else
    {
      break;
    }
  }

 
  while (1)
  {
    printf("请输入学生身高(厘米):");
    fflush(stdin);
    scanf("%f", &Student->m_StuHeight);
    if (Student->m_StuHeight > 300.0f ||
        Student->m_StuHeight <= 0.0f)
    {
      puts("您输入的身高有误,请重新输入!");
    }
    else
    {
      break;
    }
  }

  printf("请输入学生电话:");
  fflush(stdin);
  scanf("%15s", Student->m_StuTelephone);

  printf("请输入学生QQ:");
  fflush(stdin);
  scanf("%15s", Student->m_StuQQ);

  Student->m_Mark = 1;

  if (SaveStudents(Student))
  {
    StuInterfaceShow(*Student);
    printf("添加成功!\r\n");
    system("pause");
  }
  else
  {
    printf("添加失败,请检查磁盘空间!");
  }
}

//查询学生
//返回值:0表示返回
int StudentsSelect()
{
  char szbuff[32] = {0};
  int nNumber = 0;
  while (1)
  { 
    switch (SelectStuSelect())
    {
    case 1://按学号查询
      printf("请输入要查询的学号:");
      fflush(stdin);
      scanf("%s", szbuff);
      if (SelectStudentForFile(szbuff, SELECTID) == 0)
      {
        printf("找不到这个学生.\r\n");
      }
      system("pause");
      break;
    case 2://按姓名查询
      printf("请输入要查询的姓名:");
      fflush(stdin);
      scanf("%s", szbuff);
      nNumber = SelectStudentForFile(szbuff, SELECTNAME);
      if (nNumber == 0)
      {
        printf("找不到这个学生.\r\n");
      }
      else
      {
        printf("已找到%d个.\r\n", nNumber);
      }      
      system("pause");
      break;
    case 3://查询全部
      nNumber = SelectStudentForFile(szbuff, SELECTALL);
      printf("学生数量:%d\r\n", nNumber);
      system("pause");
      break;
    case 0:
      return 0;
      break;
    }
  }
}

//删除学生
int StudentsDelete()
{
  char szStuNumber[16];
  int nNumber;
  system("cls");
  printf("请输入要删除的学生学号:");
  fflush(stdin);
  scanf("%15s", szStuNumber);
  nNumber = DeleteStudents(szStuNumber);
  if (nNumber == 2)
  {
    printf("已成功删除\r\n");
  }
  else if (nNumber == 1)
  {
    printf("找不到该学生\r\n");
  }
  else if (nNumber == 0)
  {
    printf("文件打开时出错!\r\n");
  }
  system("pause");
  return 0;
}

//修改学生
int StudentChange()
{
  char szStuNumber[16];
  int nNumber;
  system("cls");
  printf("请输入要修改的学生学号:");
  fflush(stdin);
  scanf("%15s", szStuNumber);
  nNumber = ChengeStudents(szStuNumber);
  if (nNumber == 2)
  {
    printf("修改成功!\r\n");
  }
  else if (nNumber == 1)
  {
    printf("没有该学生!\r\n");
  }
  else if (nNumber == 0)
  {
    printf("文件打开时出错!\r\n");
  }
  system("pause");
  return 0;
}
2010-06-25 17:23
skyuser213
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:117
注 册:2010-5-9
收藏
得分:0 
程序代码:
// StudentsUI.h: interface for the StudentsUI class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUDENTSUI_H__7A511B4F_5845_40A4_96E0_430BD34782EC__INCLUDED_)
#define AFX_STUDENTSUI_H__7A511B4F_5845_40A4_96E0_430BD34782EC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "StuManage.h"
//主界面显示
void StuIndexInterfaceShow();
//错误提示
void StuerrorShow();
//显示学生信息
void StuInterfaceShow(tagStudents Students);
//显示查询学生界面
void StuSelectShow();
//删除提示
void DeleteShow();
//修改界面
void ChengeShow();

#endif // !defined(AFX_STUDENTSUI_H__7A511B4F_5845_40A4_96E0_430BD34782EC__INCLUDED_)


// StudentsUI.cpp: implementation of the StudentsUI class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StudentsUI.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//主界面显示
void StuIndexInterfaceShow()
{
  puts("==========学生信息管理系统==========");
  puts("1.增加学生信息");
  puts("2.删除学生信息");
  puts("3.修改学生信息");
  puts("4.查询学生信息");
  puts("0.退出");
  printf("请选择:");
}
//显示查询学生界面
void StuSelectShow()
{
  puts("1.按学号查询");
  puts("2.按姓名查询");
  puts("3.查询全部");
  puts("0.返回");
  printf("请选择:");
}
//错误提示
void StuerrorShow()
{
  printf("输入错误! 请重新输入:");
}
//显示学生信息
void StuInterfaceShow(tagStudents Students)
{
  printf("==============学生资料==============\r\n");
  printf("=学号:%-12s姓名:%-12s=\r\n",
    Students.m_StuNumber,
    Students.m_StuName);
  printf("=电话:%-12sQQ:  %-12s=\r\n",
    Students.m_StuTelephone,
    Students.m_StuQQ);
  switch (Students.m_StuSex)
  {
  case 'M':
  case 'm':
    printf("=性别: 男    年龄:%-5d身高:%-5.1f  =\r\n",
      Students.m_StuAge,
    Students.m_StuHeight);
      break;
  case 'W':
  case 'w':
    printf("=性别: 女    年龄:%-5d身高:%-5.1f  =\r\n",
      Students.m_StuAge,
    Students.m_StuHeight);
      break;
  case 'U':
  case 'u':
    printf("=性别: 未知  年龄:%-5d身高:%-5.1f  =\r\n",
      Students.m_StuAge,
    Students.m_StuHeight);
      break;
  case 'O':
  case 'o':
    printf("=性别: 其他  年龄:%-5d身高:%-5.1f  =\r\n",
      Students.m_StuAge,
    Students.m_StuHeight);
      break;
  }
  printf("====================================\r\n");
}

//删除提示
void DeleteShow()
{
  printf("确定要删除吗?(Y/N)");
}

//修改界面
void ChengeShow()
{
  puts("1.修改学生姓名");
  puts("2.修改学生电话");
  puts("3.修改学生QQ");
  puts("4.修改学生性别");
  puts("5.修改学生年龄");
  puts("6.修改学生身高");
  puts("0.保存并返回");
  printf("请输入:");
}
2010-06-25 17:24
skyuser213
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:117
注 册:2010-5-9
收藏
得分:0 
程序代码:
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__B34AEA05_C413_4534_A419_7450ECBBB526__INCLUDED_)
#define AFX_STDAFX_H__B34AEA05_C413_4534_A419_7450ECBBB526__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "StudentsUI.h"
#include "SelectInterface.h"
#include "StuManage.h"
#include "FileManage.h"

#define SELECTID 1
#define SELECTNAME 2
#define SELECTALL 3
#define FILE_SIZE 1024*1024

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__B34AEA05_C413_4534_A419_7450ECBBB526__INCLUDED_)
2010-06-25 17:24
skyuser213
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:117
注 册:2010-5-9
收藏
得分:0 
程序代码:
// SelectInterface.h: interface for the SelectInterface class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SELECTINTERFACE_H__8FBB36D3_1744_411C_B82E_E2094A55CF79__INCLUDED_)
#define AFX_SELECTINTERFACE_H__8FBB36D3_1744_411C_B82E_E2094A55CF79__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//主界面选择
//返回值:选择的功能
// 1.增加学生信息
// 2.删除学生信息
// 3.修改学生信息
// 4.查询学生信息
// 0.退出
// 其他.输入错误
int SelectStuIndexInterface();
//查询界面选择
//返回值:选择的功能
//1.按学号查询
//2.按姓名查询
//3.查询全部
//0.退出
// 其他.输入错误
int SelectStuSelect();
//删除选择
int SelectStuDelete();
//修改选择
//返回修改好的项目
tagStudents* SelectChenge(tagStudents *stStudent);
#endif // !defined(AFX_SELECTINTERFACE_H__8FBB36D3_1744_411C_B82E_E2094A55CF79__INCLUDED_)


// SelectInterface.cpp: implementation of the SelectInterface class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SelectInterface.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//主界面选择
//返回值:选择的功能
// 1.增加学生信息
// 2.删除学生信息
// 3.修改学生信息
// 4.查询学生信息
// 0.退出
// 其他.输入错误
int SelectStuIndexInterface()
{
  int nNumber = 0;

  system("cls");
  //显示界面
  StuIndexInterfaceShow();
  while (1)
  {
    _flushall();
    scanf("%d", &nNumber);
    switch (nNumber)
    {
    case 1:
    case 2:
    case 3:
    case 4:
    case 0:
      return nNumber;
      break;
    default:
      StuerrorShow();
    }
  }
  //return nNumber;
}
//查询界面选择
//返回值:选择的功能
//1.按学号查询
//2.按姓名查询
//3.查询全部
//0.退出
// 其他.输入错误
int SelectStuSelect()
{
  int nNumber;
  system("cls");
  StuSelectShow();
  while (1)
  {
    fflush(stdin);
    scanf("%d", &nNumber);
    switch (nNumber)
    {
    case 1:
    case 2:
    case 3:
    case 0:
      return nNumber;
      break;
    default:
      StuerrorShow();
    }
  }
}

//删除选择
int SelectStuDelete()
{
  char cYesNo = 0;
  while(1)
  {   
    DeleteShow();
    fflush(stdin);
    scanf("%c", &cYesNo);
    if (cYesNo == 'y' || cYesNo == 'Y')
    {
      return 1;
    }
    else if (cYesNo == 'n' || cYesNo == 'N')
    {
      return 0;
    }
    else
    {
      printf("输入有误,请重新输入!\r\n");
    }
  }
}

//修改选择
//返回修改好的项目
/*
puts("1.修改学生姓名");
puts("2.修改学生电话");
puts("3.修改学生QQ");
puts("4.修改学生性别");
puts("5.修改学生年龄");
puts("6.修改学生身高");
puts("0.保存并返回");*/
tagStudents* SelectChenge(tagStudents *stStudent)
{
  int nNumber = 0;
  while (1)
  { 
    system("cls");
    StuInterfaceShow(*stStudent);
    ChengeShow();
    fflush(stdin);
    scanf("%d", &nNumber);
    switch (nNumber)
    {
    case 1://1.修改学生姓名
      printf("请输入要修改的学生姓名:");
      fflush(stdin);
      scanf("%31s", stStudent->m_StuName);
      break;
    case 2://2.修改学生电话
      printf("请输入要修改的学生电话:");
      fflush(stdin);
      scanf("%15s", stStudent->m_StuTelephone);
      break;
    case 3://3.修改学生QQ
      printf("请输入要修改的学生QQ:");
      fflush(stdin);
      scanf("%15s", stStudent->m_StuQQ);
      break;
    case 4://4.修改学生性别
      while (1)
      { 
        printf("请输入要修改的学生性别(M:男、W:女、U:未知、O:其他):\r\n");
        fflush(stdin);
        scanf("%c", &stStudent->m_StuSex);
        if (stStudent->m_StuSex == 'M' ||
          stStudent->m_StuSex == 'm' ||
          stStudent->m_StuSex == 'W' ||
          stStudent->m_StuSex == 'w' ||
          stStudent->m_StuSex == 'U' ||
          stStudent->m_StuSex == 'u' ||
          stStudent->m_StuSex == 'O' ||
          stStudent->m_StuSex == 'o')
        {
          break;
        }
        else
        {
          printf("输入错误,请重新输入:\r\n");
        }
      }
      break;
    case 5://5.修改学生年龄
      while (1)
      {
        printf("请输入要修改的学生年龄:");
        fflush(stdin);
        scanf("%d", &stStudent->m_StuAge);
        if (stStudent->m_StuAge > 150 ||
          stStudent->m_StuAge <= 0)
        {
          printf("您输入的年龄超出范围,请重新输入!\r\n");
        }
        else
        {
          break;
        }
      }
      break;
    case 6://6.修改学生身高
      while (1)
      {
        printf("请输入学生身高(厘米):");
        fflush(stdin);
        scanf("%f", &stStudent->m_StuHeight);
        if (stStudent->m_StuHeight > 300.0f ||
          stStudent->m_StuHeight <= 0.0f)
        {
          puts("您输入的身高有误,请重新输入!");
        }
        else
        {
          break;
        }
      }
      break;
    case 0://0.保存并返回
      return stStudent;
    default:
      printf("输入有误,请重新输入!\r\n");
    }
  }
}
2010-06-25 17:25
skyuser213
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:117
注 册:2010-5-9
收藏
得分:0 
程序代码:
// FileManage.h: interface for the FileManage class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_FILEMANAGE_H__47DCD9E0_8915_48AA_9FC6_13DE755B619D__INCLUDED_)
#define AFX_FILEMANAGE_H__47DCD9E0_8915_48AA_9FC6_13DE755B619D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct tagFile
{
  int m_AllStuNumber;  //学生总数量
  int m_FileNumber;    //文件使用数量
};
//保存学生信息
//参数:要保存的学生信息
//返回:0失败 1成功
int SaveStudents(tagStudents *Students);
//查询学生信息
//参数:szbuff:要查询的字符串
//参数:chMark:查询方式 1 按ID查询 2按姓名查询 3 全部查询
//返回值:返回查到的学生数量
int SelectStudentForFile(char *szbuff, char chMark);
//检查学号是否重复
//参数:要检查的内容
//重复返回2,没有重复返回1
int RepeatnNumber(char *szbuff);
//删除学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2成功删除
int DeleteStudents(char *szStuNumber);
//修改学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2已找到
int ChengeStudents(char *szStuNumber);

#endif // !defined(AFX_FILEMANAGE_H__47DCD9E0_8915_48AA_9FC6_13DE755B619D__INCLUDED_)


// FileManage.cpp: implementation of the FileManage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileManage.h"
#define SAFE_CLOSE(p) if (p) { \
if (fclose(p))\
{\
  p = NULL;\
  return 0;\
}\
p = NULL;}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//保存学生信息
//参数:要保存的学生信息
//返回:0失败 1成功
int SaveStudents(tagStudents *Students)
{
  FILE *fpStudents = NULL;
  int nRet = 0;
  int nSize = FILE_SIZE;
  char Temp[200] = {0};
  tagFile tfTemp;

  //先读取文件
  fpStudents = fopen("Students.dat", "rb+");
  //如果文件不存在,则新建
  if (fpStudents == NULL)
  {
    fpStudents = fopen("Students.dat", "wb+");
    if (fpStudents == NULL)
    {
      nRet = 0;
      goto EXIT_PROC;
    }
    //新建一个5M的数据文件
    while (nSize)
    {
      fputc(0, fpStudents);
      nSize--;
    }

    //写入记录
    tfTemp.m_AllStuNumber = 0;
    tfTemp.m_FileNumber = 0;
    if (fseek(fpStudents, 0, SEEK_SET))
    {
      nRet = 0;
      goto EXIT_PROC;
    }
    if (fwrite(&tfTemp, 4, 2, fpStudents) != 2)
    {     
      nRet = 0;
      goto EXIT_PROC;
    }
  }

  //指向文件头
  if (fseek(fpStudents, 0, SEEK_SET))
  {
    nRet = 0;
    goto EXIT_PROC;
  }
  //读取学生数量
  if (fread(&tfTemp, 4, 2, fpStudents) != 2)
  {
    nRet = 0;
    goto EXIT_PROC;
  }
  //判断文件有没有满,满了在追加
  if ((FILE_SIZE-100) / 200 == tfTemp.m_AllStuNumber)
  {
    SAFE_CLOSE(fpStudents);
    fpStudents = fopen("Students.dat", "ab+");
    if (fpStudents == NULL)
    {
      nRet = 0;
      goto EXIT_PROC;
    }
    nSize = FILE_SIZE;
    while (nSize)
    {
      fputc(0, fpStudents);
      nSize--;
    }
    SAFE_CLOSE(fpStudents);
    //追加完毕后在重新以读方式打开
    fpStudents = fopen("Students.dat", "rb+");
    if (fpStudents == NULL)
    {
      nRet = 0;
      goto EXIT_PROC;
    }   
  }

  //指向100字节处
  if (fseek(fpStudents, 100, SEEK_SET))
  {
    nRet = 0;
    goto EXIT_PROC;
  }

  while (1)
  {
      //读取一个结构体数据,一个结构体给200字节空间
    if (fread(Temp, 200, 1, fpStudents) != 1)
    {
      nRet = 0;
      goto EXIT_PROC;
    }

    //如果遇到空数据,就添加进空地
    if (((tagStudents *)Temp)->m_Mark == 0)
    {
      //退回200字节处
      if (fseek(fpStudents, -200, SEEK_CUR))
      {
        nRet = 0;
        goto EXIT_PROC;
      }
      if (fwrite(Students, 200, 1, fpStudents) != 1)
      {
        nRet = 0;
        goto EXIT_PROC;
      }

      //添加成功后增加学生数量
      tfTemp.m_AllStuNumber++;
      //如果是在文件追加的数据  加1
      if (fread(Temp, 200, 1, fpStudents) != 1)
      {
        nRet = 0;
        goto EXIT_PROC;
      }
      //if (((tagStudents *)Temp)->m_Mark == 0)
      //{
        tfTemp.m_FileNumber++;
      //}
      //指向文件头
      if (fseek(fpStudents, 0, SEEK_SET))
      {
        nRet = 0;
        goto EXIT_PROC;
      }
      //写入数据
      if (fwrite(&tfTemp, 4, 2, fpStudents) != 2)
      {     
        nRet = 0;
        goto EXIT_PROC;
      }
      break;
    }     
  }
  nRet = 1;
EXIT_PROC:
  SAFE_CLOSE(fpStudents);
  return nRet;
}

//查询学生信息
//参数:szbuff:要查询的字符串
//参数:chMark:查询方式 1 按ID查询 2按姓名查询 3 全部查询
//返回值:返回查到的学生数量
int SelectStudentForFile(char *szbuff, char chMark)
{
  FILE *fpStudent = NULL;
  char Temp[200] = {0};
  int nRet = 0, i = 0;
  tagFile tfTemp;

 
  fpStudent = fopen("Students.dat", "rb");
  if (fpStudent == NULL)
  {
    goto EXIT_PROC;
  }

 
  //读取学生数量
  if (fread(&tfTemp, 4, 2, fpStudent) != 2)
  {
    goto EXIT_PROC;
  }

  //跳过保留的100字节
  if (fseek(fpStudent, 100, SEEK_SET))
  {
    goto EXIT_PROC;
  }

 
  switch (chMark)
  {
  case SELECTID: //按ID查询
   
    nRet = 0;
    for (i = 0; i < tfTemp.m_FileNumber; i++)
    {
      //按每个200字节查询
      if (fread(Temp, 200, 1, fpStudent) != 1)
      {
        goto EXIT_PROC;
      }
      if (((tagStudents *)Temp)->m_Mark == 0)
      {
        continue;
      }
      if (strcmp(((tagStudents *)Temp)->m_StuNumber, szbuff) == 0)
      {
        nRet++;
        StuInterfaceShow(*((tagStudents *)Temp));
        break;
      }
    }
    break;
  case SELECTNAME: //按姓名查询
    nRet = 0;
    for (i = 0; i < tfTemp.m_FileNumber; i++)
    {
      //按每个200字节查询
      if (fread(Temp, 200, 1, fpStudent) != 1)
      {
        goto EXIT_PROC;
      }
      if (((tagStudents *)Temp)->m_Mark == 0)
      {
        continue;
      }
      if (strcmp(((tagStudents *)Temp)->m_StuName, szbuff) == 0)
      {
        nRet++;
        StuInterfaceShow(*((tagStudents *)Temp));
      }
    }
    break;
  case SELECTALL: //查询全部
    nRet = 0;
    for (i = 0; i < tfTemp.m_FileNumber; i++)
    {
      //按每个200字节查询
      if (fread(Temp, 200, 1, fpStudent) != 1)
      {
        goto EXIT_PROC;
      }
      if (((tagStudents *)Temp)->m_Mark == 0)
      {
        continue;
      }
      nRet++;
      StuInterfaceShow(*((tagStudents *)Temp));
    }
    break;
  }
EXIT_PROC:
  SAFE_CLOSE(fpStudent);
  return nRet;
}

//检查学号是否重复
//参数:要检查的内容
//重复返回2,没有重复返回1
int RepeatnNumber(char *szbuff)
{
  FILE *fpStudent = NULL;
  char Temp[200] = {0};
  int nRet = 0, i = 0;
  tagFile tfTemp;

 
  fpStudent = fopen("Students.dat", "rb");
  if (fpStudent == NULL)
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  //读取学生数量
  if (fread(&tfTemp, 4, 2, fpStudent) != 2)
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  //跳过保留的100字节
  if (fseek(fpStudent, 100, SEEK_SET))
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  for (i = 0; i < tfTemp.m_FileNumber; i++)
  {
    //按每个200字节查询
    if (fread(Temp, 200, 1, fpStudent) != 1)
    {
      nRet = 0;
      goto EXIT_PROC;
    }
    if (((tagStudents *)Temp)->m_Mark == 0)
    {
      continue;
    }
    if (strcmp(((tagStudents *)Temp)->m_StuNumber, szbuff) == 0)
    {
      nRet = 2;
      goto EXIT_PROC;
    }
  } 
  nRet = 1;
EXIT_PROC:
  SAFE_CLOSE(fpStudent);
  return nRet;
}

//删除学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2成功删除
int DeleteStudents(char *szStuNumber)
{
  FILE *fpStudent = NULL;
  char Temp[200] = {0};
  int nRet = 0, i = 0;
  tagFile tfTemp;

 
  fpStudent = fopen("Students.dat", "rb+");
  if (fpStudent == NULL)
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  //读取学生数量
  if (fread(&tfTemp, 4, 2, fpStudent) != 2)
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  //跳过保留的100字节
  if (fseek(fpStudent, 100, SEEK_SET))
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  for (i = 0; i < tfTemp.m_FileNumber; i++)
  {
    //按每个200字节查询
    if (fread(Temp, 200, 1, fpStudent) != 1)
    {
      nRet = 0;
      goto EXIT_PROC;
    }
    if (((tagStudents *)Temp)->m_Mark == 0)
    {
      continue;
    }
    if (strcmp(((tagStudents *)Temp)->m_StuNumber, szStuNumber) == 0)
    {
      StuInterfaceShow(*(tagStudents *)Temp);
      //询问是否删除
      if (SelectStuDelete())
      {
        //从当前指针偏移-200,清空数据
        if (fseek(fpStudent, -200, SEEK_CUR))
        {
          nRet = 0;
          goto EXIT_PROC;
        }
        //清空,并写入文件
        memset(Temp, 0, 200);
        if (fwrite(Temp, 200, 1, fpStudent) != 1)
        {
          nRet = 0;
          goto EXIT_PROC;
        }
        //学生总数量减1
        tfTemp.m_AllStuNumber--;
        //如果后面没有数据,学生数据大小也减1
        if (fgetc(fpStudent) == 0)
        {
          tfTemp.m_FileNumber--;
        }
        //指向文件头
        if (fseek(fpStudent, 0, SEEK_SET))
        {
          nRet = 0;
          goto EXIT_PROC;
        }
        //写入数据
        if (fwrite(&tfTemp, 4, 2, fpStudent) != 2)
        {     
          nRet = 0;
          goto EXIT_PROC;
        }
        nRet = 2;
        goto EXIT_PROC;
      }
      else
      {
        return -1;
      }
    }
  } 
  nRet = 1;
EXIT_PROC:
  SAFE_CLOSE(fpStudent);
  return nRet;
}

//修改学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2已找到
int ChengeStudents(char *szStuNumber)
{
  FILE *fpStudent = NULL;
  char Temp[200] = {0};
  tagStudents * tsTemp = NULL;
  int nRet = 0, i = 0;
  tagFile tfTemp;

 
  fpStudent = fopen("Students.dat", "rb+");
  if (fpStudent == NULL)
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  //读取学生数量
  if (fread(&tfTemp, 4, 2, fpStudent) != 2)
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  //跳过保留的100字节
  if (fseek(fpStudent, 100, SEEK_SET))
  {
    nRet = 0;
    goto EXIT_PROC;
  }

 
  for (i = 0; i < tfTemp.m_FileNumber; i++)
  {
    //按每个200字节查询
    if (fread(Temp, 200, 1, fpStudent) != 1)
    {
      nRet = 0;
      goto EXIT_PROC;
    }
    if (((tagStudents *)Temp)->m_Mark == 0)
    {
      continue;
    }
    if (strcmp(((tagStudents *)Temp)->m_StuNumber, szStuNumber) == 0)
    {
      //从当前指针偏移-200,清空数据
      if (fseek(fpStudent, -200, SEEK_CUR))
      {
        nRet = 0;
        goto EXIT_PROC;
      }
      //先将修改好的写进内存
      tsTemp = SelectChenge((tagStudents *)Temp);
      //写入修改好的项目
      if (fwrite(tsTemp, 200, 1, fpStudent) != 1)
      {
        nRet = 0;
        goto EXIT_PROC;
      }
      nRet = 2;
      goto EXIT_PROC;
    }
  } 
  nRet = 1;
EXIT_PROC:
  SAFE_CLOSE(fpStudent);
  return nRet;
}
2010-06-25 17:25
zhuxinhai
Rank: 1
等 级:新手上路
帖 子:11
专家分:3
注 册:2010-6-21
收藏
得分:0 
加入    63390142   QQ群   一起学习
加入    63390142   QQ群   一起学习
加入    63390142   QQ群   一起学习
加入    63390142   QQ群   一起学习
2010-06-25 19:57
快速回复:C语言版学生管理系统
数据加载中...
 
   



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

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