⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档 (3).txt

📁 学生信息管理程序(C源代码)
💻 TXT
字号:
/*
 C/C++ Program Source file.

 Program name : Student information manage system.
 Version      : 1.0

 Author       : Writed by YinShengge [ 殷圣鸽 ]
 Release Time : 11:19 2005-5-20
 Maintain Time: 0:57 2005-5-23 ,20:03 2005-6-6


 Compiler     : Borland   Turbo  C   2.0
                     : Microsoft Visual C++ 6.0

 Introduction : A simple computer program for managing students database,
    include creating, adding, deleting, sorting and searching
    operate routines .
*/


#include <stdio.h>  /*标准输入输出库*/
#include <string.h>  /*字符串操作库*/
#include <conio.h>  /*控制台函数库*/
#include <malloc.h>  /*内存分配库*/
#include <process.h>  /*进程库*/

#define   INITSIZE 100  /*初始化学生记录的条数*/
#define   INCSIZE  sizeof(student) /*初始化空间不足时,增加存储空间的字节数*/


typedef struct
{
 long no;  /*学生序号*/
 int math;  /*数学成绩*/
 int program;  /*程序设计成绩*/
 int amount;  /*总分*/
 char name[ 30 ];  /*学生姓名*/
}student;  /*学生结构体*/

int maxsize = INITSIZE; /*初始化记录条数*/
int num =0;  /*当前学生记录条数*/
int dbnull = 1;  /*数据库是否存在的标志*/

 

 

enum   /*查询,排序方式,五种*/
{
 No,   /*学号方式*/
 Name,   /*姓名方式*/
 Math,   /*数学成绩方式*/
 Program,  /*程序设计成绩方式*/
 Amount   /*总分方式*/
};

 

/*以下为所有函数的声明*/
int createset(student **t);
void addnew(student *t);
void deletestu(student *t);
void stuselect(student *t,int mode);
void scoresort(student *t,int mode);
int findno(student *t,int no);
int findmath(student *t,int math);
int findprogram(student *t,int program);
int findamount(student *t,int amount);
int findname(student *t,const char *name);
void display(student *t,int no);
void mathsort(student *t);
void programsort(student *t);
void amountsort(student *t);
void swap(student *t, int i,int j);

/*以下为函数实现*/
int createset(student **t)/*创建数据记录集*/

{
 char ask ;

 

 if (num!=0)  /*存在学生记录*/
 {
  printf("Exsist a data base ,recover it?(Y/N)?");
  ask =getch();  /*是否覆盖数据库*/
  if (ask == 'y'||ask=='Y')
  {
   free(*t);  /*若选择覆盖,则释放现存存储空间,学生记录个数清零*/
   num = 0;
  }
  else
  {
   return 0;  /*不选择覆盖,则退出*/
  }
 }

 


 *t = (student *)malloc(INITSIZE*sizeof(student)); /*分配INITSIZE个学生记录所需空间*/

 if (!t)
 {
  printf("Memory overflow program abort.");  /*内存不足,退出程序*/
  exit(0);
 }


 else
 {
  printf("New database have been created.\n");  /*分配成功,成功创建数据库*/
  dbnull = 0;      /*数据库存在标志设为0(表示存在)*/
  return 1;
 }


}

void addnew(student *t)  /*向数据库插入学生记录*/
{
 student temp;

 if (dbnull)      /*数据库存在标志不为0,即无数据库,操作失败*/
 {
     printf("Not exsist database select menu 1 to create database...");
     return;
 }
 if (num+1>maxsize)     /*当前记录个数大于初始化记录条数,追加空间*/
 {
  t =(student *)realloc(t,maxsize+INCSIZE);  /*追加一个记录所需空间*/
  if (!t)      /*内存不足,追加失败*/
  {
   printf("Memory overflow! program abort.\n");
   exit(0);
  }
 }

 printf("Input the student's No. , name, math score and program score that you want to add.\n");

 if (scanf("%ld%s%d%d",&(temp.no),   /*输入学生数据*/
  temp.name,
  &(temp.math),
  &(temp.program)))
 {
  if (findno(t,temp.no) == -1)    /*查找输入的学号是否与数据库的重复*/
  {
   t[ num ].no = temp.no;     /*学号不冲突,则把输入的记录存放到数据库末端*/
   strcpy(t[ num ].name,temp.name);

   t[ num ].math = temp.math;
   t[ num ].program = temp.program;

   t[ num ].amount = t[ num ].math + t[ num ].program;
   num++;      /*当前记录数加一*/

   printf("Add sucess!\n");
  }

  else
  {
   printf("Exsist the student whom NO. is %d,add fail.\n",temp.no);/*输入学号已经存在,添加记录失败*/
  }
 }

 else
 {
  printf("Data format error,add fail.\n");  /*输入函数出错,表示输入格式错误,添加失败*/
 }

}

 


void deletestu(student *t) /*从数据库删除某条学生记录*/
{

 long delno =0;

 int index;

 int i =0;


 if (dbnull)  
 {
     printf("Not exsist database select menu 1 to create database...");
     return;
 }

 printf("Input the student NO. that you want to delete :\n");

 scanf("%ld",&delno);   /*输入要删除的学生的学号*/

 index = findno(t,delno);  /*按学号方式查找该学生是否存在*/

 if (index != -1)   /*该学生存在,则删除他*/
 {

  for (i = index+1; i<= num; i++) /*数据库记录前移,完成'删除'操作*/
  {

   t[ i-1 ].no = t[ i ].no;
   strcpy(t[ i-1 ].name,t[ i ].name);
   t[ i-1 ].math = t[ i ].math;
   t[ i-1 ].program = t[ i ].program;
   t[ i-1 ].amount = t[ i ].amount;

  }
  num--;    /*当前记录数减一*/

  printf("Delete success!\n");

 }

 else
 {
  printf("The NO. that you input not exsist delete fail\n"); /*无该学号的学生,删除失败*/
 }

 

}


void stuselect(student *t,int mode)  /*搜索数据库*/
{
 long  tempno =0;
 char tempname[ 30 ];
 int  tempmath;
 int  tempprogram;
 int  tempamount;
 int  count =0;

 if (dbnull)
 {
     printf("Not exsist database select menu 1 to create database...");
     return;
 }

 switch (mode)   /*判断查询方式*/
 {
 case No:   /*按学号查询*/
  printf("Input the student NO. that you want to search.\n");
  scanf("%ld",&tempno);  /*输入学号*/
  tempno =findno(t,tempno); /*查找该学生*/

  if ( tempno!= -1 )
  {
   printf("Search sucess!.\n");/*查询成功,打印之*/
   display(t,tempno);
  }
  else
  {
   printf("The NO. that you input not exsist search fail.\n"); /*查找失败*/
  }
  break;
 case Name:   /*按姓名查询*/
  printf("Input the student name that you want to search.:\n");
  *tempname ='\0';
  scanf("%s",tempname);
  count = findname(t,tempname);  /*返回查询姓名为name的学生记录个数*/
  printf("There are %d student have been searched.\n",count);
  break;
 case Math:    /*按数学成绩查询*/
  printf("Input the a score, program will search students which math scores are higher than it.\n");
  scanf("%d",&tempmath);
  count = findmath(t,tempmath);
  printf("There are %d student have been searched.\n",count);
  break;

 case Program:   /*按程序设计成绩查询*/

  printf("Input the a score, program will search students which programming scores are higher than it.\n");
  scanf("%d",&tempprogram);
  count = findprogram(t,tempprogram);
  printf("There are %d student have been searched.\n",count);
  break;

 case Amount:   /*按总分查询*/
  printf("Input the a score, program will search students which sum scores are higher than it\n");
  scanf("%d",&tempamount);
  count = findamount(t,tempamount);
  printf("There are %d student have been searched.\n",count);
  break;
 default:
  break;

 }

}


void scoresort(student *t,int mode) /*学生记录排序*/
{
 int count =0;   

 switch (mode)  /*选择不同排序方式进行成绩排序*/
 {
 case Math:
  mathsort(t);  /*按数学成绩排序*/
  break;
 case Program:  /*按程序设计成绩排序*/
  programsort(t);
  break;
 case Amount:  /*按总分排序*/
  amountsort(t);
  break;
 }

 printf("Sorting have been finished .flowing is the result:\n");
 for (count =0;count< num; count++) /*排序完成后输出排序结果*/
 {
  display(t,count);
 }
}


int findno(student *t,int no)/*按学号查找学生记录*/
{
 int count =0;
 for (count =0; count<num; count++)
 {
  if ((t+count)->no == no) /*逐个搜索,若该学生记录的学号等于需要查找的学号,则返回该学号*/
  {
   return count;
  }
 }
 return -1;   /*搜索完毕,仍没有匹配学号,则返回-1*/
}


int findmath(student *t,int math)
{
 int count =0;   /*按数学成绩查找,这里查找的结果是大于指定数学分数的所有学生记录*/
 int i =0;
 for (count =0; count<num; count++)
 {
  if ((t+count)->math > math)
  {
   display (t,count);  /*显示查找结果*/
   i++;
  }
 }

 return i;   /*返回符合查询条件的学生记录数目*/
}


int findprogram(student *t,int program)/*按程序设计成绩查找学生记录,算法类似上面的模块*/
{
 int count =0;
 int i =0;
 for (count =0; count<num; count++)
 {
  if ((t+count)->program > program)
  {
   display(t,count);
   i++;
  }
 }

 return i;
}

int findamount(student *t,int amount)/*类似上面的模块*/
{
 int count =0;
 int i =0;
 for (count =0; count<num; count++)
 {
  if ((t+count)->amount > amount)
  {
   display(t,count);
   i++;
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -