C++学生成绩管理系统

来源:互联网 发布:矩阵论北航教材 编辑:程序博客网 时间:2024/05/28 23:21

#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <stdlib.h>
char strinfo[80];
int nCountRs;

class CStudent
{
public:
 CStudent();
 void Print();
 float Validate();
 void Setname(char *p);
 void Setid(char *p);
 void Setc(float i,float j,float k);
 float GetE();
 float GetC();
 float GetM();
 float GetAve();
 char *GetName();
 char *GetId();
 CStudent &operator=(CStudent &stu);
private:
 char name[20];
 char id[20];
 float fE;
 float fC;
 float fM;
 float fCount;
 float fAve;
};

CStudent &CStudent::operator=(CStudent &stu)
{
 strcpy(name,stu.name);
 strcpy(id,stu.id);
 fE=stu.fE;
 fC=stu.fC;
 fM=stu.fM;
 fCount=stu.fCount;
 fAve=stu.fAve;
 return *this;
}

CStudent::CStudent()
{
 strcpy(name,"");
 strcpy(id,"");
 fE=0;
 fC=0;
 fM=0;
 fCount=0;
 fAve=0;
}

void CStudent::Print()
{
 cout<<endl;
 cout<<"[    姓名]: "<<name<<endl;
 cout<<"[    学号]: "<<id<<endl;
 cout<<"[英语成绩]: "<<fE<<endl;
 cout<<"[C++ 成绩]: "<<fC<<endl;
 cout<<"[数学成绩]: "<<fM<<endl;
 cout<<"[  总成绩]: "<<fCount<<endl;
 cout<<"[平均成绩]: "<<fAve<<endl;
 cout<<endl;
}

float CStudent::Validate()
{
 int s;
 float i;
 char buf[80];
 cin>>i;
 s=cin.rdstate();
 while(s)
 {
  cin.clear();
  cin.getline(buf,80);
  cout<<"请正确输入一个数:";
  cin>>i;
  s=cin.rdstate();
 }
 return i;
}

char * CStudent::GetName()
{
 return (char *)name;
}

char * CStudent::GetId()
{
 return (char *)id;
}

float CStudent::GetE()
{
 return fE;
}

float CStudent::GetC()
{
 return fC;
}

float CStudent::GetM()
{
 return fM;
}

float CStudent::GetAve()
{
 return fAve;
}

void CStudent::Setname(char *p)
{
 strncpy(name,p,20);
}

void CStudent::Setid(char *p)
{
 strncpy(id,p,20);
}

void CStudent::Setc(float i,float j,float k)
{
 fE=i;
 fC=j;
 fM=k;
 fCount=i+j+k;
 fAve=fCount/3.0;
}


class CStufile
{
public:
 void AddTo(CStudent stu);
 void Delete(char *p);
 void Sort();
 int Seek(char *p);
 void List();
 void GetCount();
};

void CStufile::Delete(char *p)
{
 fstream file2;
 char ch;
 CStudent stu;
 CStudent *Csortstu=new CStudent[::nCountRs];
 int i=0,j=0,l=0;
 char strName[20],strId[20];
 float fa,fb,fc,fd,fe;
 const int size=sizeof(CStudent);

 file2.open("stu.dat",ios::out|ios::in);
 file2.read(&ch,1);

 if(file2.eof())
 {
  strcpy(::strinfo,"数据库为空,请添加数据");
 }
 else
 {
  while(!file2.eof())
  {
   file2.seekp(size*i,ios::beg);
   file2.read(strName,20);
   file2.read(strId,20);

   file2.read((char*)&fa,4);
   file2.read((char*)&fb,4);
   file2.read((char*)&fc,4);

   stu.Setname(strName);
   stu.Setid(strId);
   stu.Setc(fa,fb,fc);

   if(!file2.eof())
   {
    Csortstu[j]=stu;
    j++;
   }
   i++;
   file2.read(&ch,1);
  }
 }
 file2.close();
 file2.open("stu.dat",ios::out|ios::in|ios::trunc);
 if(::nCountRs>0)
 {
  for(l=0;l<::nCountRs;l++)
  {
   if(strcmp(p,Csortstu[l].GetId()))
   {
    fa=Csortstu[l].GetE();
    fb=Csortstu[l].GetC();
    fc=Csortstu[l].GetM();
    fd=fa+fb+fc;
    fe=fd/3.0;

    file2.write(Csortstu[l].GetName(),20);
    file2.write(Csortstu[l].GetId(),20);
    file2.write((char*)&fa,4);
    file2.write((char*)&fb,4);
    file2.write((char*)&fc,4);
    file2.write((char*)&fd,4);
    file2.write((char*)&fe,4);
   }
  }
  ::nCountRs--;
 }
 delete []Csortstu;
}

void CStufile::Sort()
{
 fstream file2;
 char ch;
 CStudent stu;
 CStudent *Csortstu=new CStudent[::nCountRs];
 int j=0;

 const int size=sizeof(CStudent);
 file2.open("stu.dat",ios::out|ios::in);
 

 char strName[20],strId[20];
 float fa,fb,fc;
 int i=0;
 
 file2.read(&ch,1);

 if(file2.eof())
 {
  strcpy(::strinfo,"数据库为空,请添加数据");
 }
 else
 {
  while(!file2.eof())
  {
   file2.seekp(size*i,ios::beg);
   file2.read(strName,20);
   file2.read(strId,20);

   file2.read((char*)&fa,4);
   file2.read((char*)&fb,4);
   file2.read((char*)&fc,4);

   stu.Setname(strName);
   stu.Setid(strId);
   stu.Setc(fa,fb,fc);

   if(!file2.eof())
   {
    Csortstu[j]=stu;
    j++;
   }
   i++;
   file2.read(&ch,1);
  }
 }

 if(::nCountRs>0)
 {
  for(int l=0;l<::nCountRs;l++)
  {
   for(j=0;j<(::nCountRs-l);j++)
   {
    if(Csortstu[j].GetAve()<Csortstu[j+1].GetAve())
    {
     stu=Csortstu[j];
     Csortstu[j]=Csortstu[j+1];
     Csortstu[j+1]=stu;
    }
   }
  }
  for(l=0;l<::nCountRs;l++)
  {
   Csortstu[l].Print();
  }

  cout<<"数据显示完毕! 输入任意符号返回:";
  cin>>ch;
 }

 file2.close();
 delete []Csortstu;
}

int CStufile::Seek(char *p)
{
 int nReturn=0;
 fstream file1;
 CStudent stu;
 char strName[20],strId[20]; 
 char ch;
 const int size=sizeof(CStudent);
 float fa,fb,fc; 
 int i=0;
 
 strcpy(::strinfo,"没有找到要查找的数据!");
 file1.open("stu.dat",ios::out|ios::in);
 file1.read(&ch,1);
 
 if(file1.eof())
 {
  strcpy(::strinfo,"数据库为空,请添加数据");
 }
 else
 {
  while(!file1.eof())
  {
   file1.seekp(size*i,ios::beg);
   file1.read(strName,20);
   file1.read(strId,20);

   file1.read((char*)&fa,4);
   file1.read((char*)&fb,4);
   file1.read((char*)&fc,4);

   stu.Setname(strName);
   stu.Setid(strId);
   stu.Setc(fa,fb,fc);

   if(!file1.eof())
   {
    if(!strcmp(p,stu.GetId()))
    {
     stu.Print();
     cout<<"数据显示完毕! 输入任意符号返回:";
     cin>>ch;
     nReturn=1;
     strcpy(::strinfo,"");
     break;
    }
   }

   i++;
   file1.read(&ch,1);
  }

 }
 return nReturn;
 file1.close();
}

void CStufile::List()
{
 fstream file2;
 char ch;
 CStudent stu;

 const int size=sizeof(CStudent);
 file2.open("stu.dat",ios::out|ios::in);
 

 char strName[20],strId[20];
 float fa,fb,fc;
 int i=0;
 
 file2.read(&ch,1);
 cout<<"共有 "<<::nCountRs<<" 条记录."<<endl;
 if(file2.eof())
 {
  strcpy(::strinfo,"数据库为空,请添加数据");
 }
 else
 {
  while(!file2.eof())
  {
   file2.seekp(size*i,ios::beg);
   file2.read(strName,20);
   file2.read(strId,20);

   file2.read((char*)&fa,4);
   file2.read((char*)&fb,4);
   file2.read((char*)&fc,4);

   stu.Setname(strName);
   stu.Setid(strId);
   stu.Setc(fa,fb,fc);

   if(!file2.eof())
    stu.Print();

   i++;
   file2.read(&ch,1);
  }
  cout<<"数据显示完毕! 输入任意符号返回:";
  cin>>ch;
 }
 file2.close();
}

void CStufile::AddTo(CStudent stu)
{
 fstream file1;
 float a,b,c,d,e;

 a=stu.GetE();
 b=stu.GetC();
 c=stu.GetM();
 d=a+b+c;
 e=d/3.0;
 file1.open("stu.dat",ios::out|ios::in|ios::app);
 file1.write(stu.GetName(),20);
 file1.write(stu.GetId(),20);
 file1.write((char*)&a,4);
 file1.write((char*)&b,4);
 file1.write((char*)&c,4);
 file1.write((char*)&d,4);
 file1.write((char*)&e,4);
 file1.close();
 strcpy(::strinfo,"数据添加成功! ");
 this->GetCount();
}

void CStufile::GetCount()
{
 fstream file2;
 char ch;

 const int size=sizeof(CStudent);
 file2.open("stu.dat",ios::out|ios::in);
 
 int i=0; 
 file2.read(&ch,1);

 while(!file2.eof())
 {

  file2.seekp(size*i,ios::beg);
  ::nCountRs=i++;
  file2.read(&ch,1);
 }

 file2.close();
}


void SetInfo(CStudent &stu);


void main()
{
 CStudent A;
 CStufile FA;
 int i=0,T,intExit=0;
 char strSekid[20];

 strcpy(strinfo,"");
 FA.GetCount();
 
 while(1)
 {
  system("cls");

  cout<<"        学生成绩管理         /n"<<endl;
  cout<<strinfo<<endl;
  strcpy(strinfo,"");
  cout<<"1.显示所有学生数据."<<endl;
  cout<<"2.按平均分排序."<<endl;
  cout<<"3.添加新数据."<<endl;
  cout<<"4.查找数据."<<endl;
  cout<<"5.删除学生数据."<<endl;
  cout<<"6.输入其他任意符号退出."<<endl;
  cout<<"/n选择:";
  cin>>T;

  switch(T)
  {
  case 1:
   FA.List();
   break;
  case 2:
   FA.Sort();
   break;
  case 3:
   SetInfo(A);
   FA.AddTo(A);
   break;
  case 4:
   cout<<"输入要查找的学号:";
   cin>>strSekid;
   FA.Seek(strSekid);
   break;
  case 5:
   strSekid[0]='/0';
   cout<<"输入要删除的学号:";
   cin>>strSekid;
   cout<<"/n要删除的数据信息:"<<endl;
   if(FA.Seek(strSekid))
   {
    FA.Delete(strSekid);
    strcpy(::strinfo,"数据删除成功!");
   }
   else
   {
    strcpy(::strinfo,"删除失败,数据不存在");
   }

   break;
  default:
   intExit=1;
   break;  
  }
  if(intExit)
  {
   break;
  }
 }
}

void SetInfo(CStudent &stu)
{
 float di,dj,dk;
 char p1[20],p2[20];
 
 cout<<"输入姓名:";
 cin>>p1;
 stu.Setname(p1);

 cout<<"输入学号:";
 cin>>p2;
 stu.Setid(p2);

 cout<<"输入英语成绩:";
 di=stu.Validate();

 cout<<"输入C++成绩:";
 dj=stu.Validate();

 cout<<"输入数学成绩:";
 dk=stu.Validate();

 stu.Setc(di,dj,dk);

 stu.Print();

原创粉丝点击