#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
class Goods
{
public:
Goods()
{
}
char num[200];
char name[20];
int Math;
int English;
int C;
int Sport;
int Sum;
Goods * Next;
void InputAll()//输入所有信息
{
InputNum();
InputName();
InputMath();
InputEnglish();
InputC();
InputSport();
InputSum();
}
void InputNum()
{
cout<<"\t\t请输入学生编号:";cin>>num;
}
void InputName()
{
cout<<"\t\t请输入学生的姓名:"; cin>>name;
}
void InputMath()
{
cout<<"\t\t请输入学生的数学成绩:"; cin>>Math;
}
void InputEnglish()
{
cout<<"\t\t请输入学生的英语成绩:"; cin>>English;
}
void InputC()
{
cout<<"\t\t请输入学生的C语言成绩:"; cin>>C;
}
void InputSport()
{
cout<<"\t\t请输入学生的体育成绩:"; cin>>Sport;
}
void InputSum()
{
Sum=(Math+English+C+Sport);
cout<<"\t\t该学生的总成绩为:"; cout<<Sum;
}
void ReadFile(istream & in) //将输入的东西存入文件
{
Sum=(Math+English+C+Sport);
in>>num>>name>>Math>>English>>C>>Sport>>Sum;
}
void Show()//输出信息
{
Sum=(Math+English+C+Sport);
cout<<"编号: "<<num<<endl<<"姓名:"<<name<<endl<<"数学成绩: "<<Math<<endl<<"英语成绩 "<<English<<endl<<"C语言成绩 "<<C<<endl<<"体育成绩: "<<Sport<<endl<<"总成绩:"<<Sum<<endl;
}
};
class Goodsmessage : public Goods
{
public:
Goodsmessage();
~Goodsmessage();
void showMenu(int n);
void Find();
void Save();
void Swap(Goods *,Goods *);
void Paixu();
void Average();
int ListCount();
void AddItem()//从键盘添加学生信息
{
system("cls");
End->InputAll();
End->Next = new Goods;
End=End->Next;
cout<<"添加成功!"<<endl;
Save();
cout<<"输入任意字符!继续……";
getch();
}
void Display()//显示所有信息
{
system("cls");
i=0;
for(Goods *p=Head->Next;p!=End;p=p->Next)
{
p->Show();
i++;
}
cout<<"共有"<<i<<"个学生"<<"\n"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
void Max_Min()//最值成绩
{
system("cls");
for(Goods * p=Head->Next;p!=End;p=p->Next)
{
if(p==Head->Next)
cout<<"成绩最低学生是:"<<p->name<<endl;
if(p->Next==End)
cout<<"成绩最高学生是:"<<p->name<<endl;
}
cout<<"输入任意字符!继续……"<<endl;
getch();
}
void great()//优秀学生
{
system("cls");
for(Goods * p=Head->Next;p!=End;p=p->Next)
{
if(p->Sum>340&&p->Math>80&&p->English>80&&p->C>80&&p->Sport>80)
{
cout<<"优秀学生姓名:"<<p->name<<endl;
out.open("优秀学生.txt");
for(Goods *q=Head->Next;q!=End;q=q->Next)
if(q->Sum>340&&q->Math>80&&q->English>80&&q->C>80&&q->Sport>80)
out<<"编号:"<<q->num<<"\t姓名:"<<q->name<<"\t数学:"<<q->Math<<"\t英语:"<<q->English<<"\tC语言:"<<q->C<<"\t体育:"<<q->Sport<<"\t总成绩:"<<q->Sum<<'\n';
out.close();
}
}
cout<<"输入任意字符!继续……"<<endl;
getch();
}
void Fail() //补考学生
{
system("cls");
for(Goods * p=Head->Next;p!=End;p=p->Next)
{
if(p->Math<60||p->English<60||p->C<60||p->Sport<60)
{
cout<<p->name<<"同学需要补考"<<endl;
out.open("补考学生.txt");
for(Goods *q=Head->Next;q!=End;q=q->Next)
if(q->Math<60||q->English<60||q->C<60||q->Sport<60)
out<<"编号:"<<q->num<<"\t姓名:"<<q->name<<'\n';
out.close();
}
else
cout<<p->name<<"同学不需要补考"<<endl;
}
cout<<"输入任意字符!继续……"<<endl;
getch();
}
void AverM()//数学平均分
{
int averageM=0;
system("cls");
i=0;
for(Goods * p=Head->Next;p!=End;p=p->Next)
{
averageM=averageM+p->Math;
i++;
}
cout<<averageM/i<<endl;
cout<<"输入任意字符!继续……";
getch();
}
void AverE()//英语平均分
{
int averageE=0;
system("cls");
i=0;
for(Goods * p=Head->Next;p!=End;p=p->Next)
{
averageE=averageE+p->English;
i++;
}
cout<<averageE/i<<endl;
cout<<"输入任意字符!继续……";
getch();
}
void AverC()//C语言平均分
{
int averageC=0;
system("cls");
i=0;
for(Goods * p=Head->Next;p!=End;p=p->Next)
{
averageC=averageC+p->C;
i++;
}
cout<<averageC/i<<endl;
cout<<"输入任意字符!继续……";
getch();
}
void AverS()//体育平均分
{
int averageS=0;
system("cls");
i=0;
for(Goods * p=Head->Next;p!=End;p=p->Next)
{
averageS=averageS+p->Sport;
i++;
}
cout<<averageS/i<<endl;
cout<<"输入任意字符!继续……";
getch();
}
private:
Goods * Head,* End;
int i;
ifstream in;
ofstream out;
Goods *FindItem(char * name)
{
for(Goods * p=Head;p->Next!=End;p=p->Next)//匹配成功则返回上一个指针,不成功就返回空
if(!strcmp(p->Next->name,name))return p;
return NULL;
}
Goods *FindNum(char * num)
{
for(Goods * p=Head;p->Next!=End;p=p->Next)//匹配成功则返回上一个指针,不成功就返回空
if(!strcmp(p->Next->num,num))return p;
return NULL;
}
};
Goodsmessage::Goodsmessage()//构造函数
{
Head=new Goods;
Head->Next=new Goods;
End=Head->Next;
in.open("stuf.txt");
if(!in)
cout<<"无信息。请先添加。"<<endl;
else
{
while(!in.eof())
{
End->ReadFile(in);
if(End->name[0]=='\0')break;
End->Next=new Goods;
End=End->Next;
}
in.close();
cout<<"\t\t\t\t读取信息成功!"<<"\n"<<endl;
}
}
Goodsmessage::~Goodsmessage()//析构函数
{
for(Goods * temp;Head->Next!=End;)
{
temp=Head->Next;
Head->Next=Head->Next->Next;
delete temp;
}
delete Head,End;
}
void Goodsmessage::showMenu(int n)//菜单
{
switch(n)
{
case 1:
{
system("cls");
cout<<"━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━"<<endl;
cout<<"******************欢迎使用学生成绩管理系统******************"<<endl;
cout<<endl;
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━━┓ "<<endl;
cout<<" ┃ ┃ "<<endl;
cout<<" ┃ ┃ "<<endl;
cout<<" ┃ 1---------------增加学生成绩 ┃ "<<endl;
cout<<" ┃ 2---------------显示学生成绩 ┃ "<<endl;
cout<<" ┃ 3---------------管理学生成绩 ┃ "<<endl;
cout<<" ┃ 4---------------输出各科平均数 ┃ "<<endl;
cout<<" ┃ 5---------------保存学生成绩信息 ┃ "<<endl;
cout<<" ┃ 0---------------安全退出系统 ┃ "<<endl;
cout<<" ┃ ┃ "<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━━┛ "<<endl;
cout<<endl;
cout<<"\t\t\n\t\t\t\t 请选择:0-5"<<endl;
cout<<"━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━*━"<<endl;
break;
}
case 2:
{
system("cls");
cout<<"************************************************************************************************************************\n"
<<"\t 0.返回上一步 1.输出最高最低 2.输出优秀学生名单 3.按总分排序 4.输出补考学生名单\n"
<<"************************************************************************************************************************"<<endl;
cout<<"\t\t\n\t\t\t\t 请选择:0-4"<<endl;
break;
}
case 3:
{
system("cls");
cout<<"####################################################################################################################"<<endl;
cout<<"\t 0.返回上一步 1.计算数学成绩 2.计算英语成绩 3.计算C语言成绩 4.计算体育成绩"<<endl;
cout<<"####################################################################################################################"<<endl;
}
}
}
void Goodsmessage::Swap(Goods *p1, Goods *p2)//交换两个学生的数据
{
Goods *temp=new Goods;
strcpy(temp->name,p1->name);
strcpy(temp->num,p1->num);
temp->Math=p1->Math;
temp->English=p1->English;
temp->C=p1->C;
temp->Sport=p1->Sport;
temp->Sum=p1->Sum;
strcpy(p1->name,p2->name);
strcpy(p1->num,p2->num);
p1->Math=p2->Math;
p1->English=p2->English;
p1->C=p2->C;
p1->Sport=p2->Sport;
p1->Sum=p2->Sum;
strcpy(p2->name,temp->name);
strcpy(p2->num,temp->num);
p2->Math=temp->Math;
p2->English=temp->English;
p2->C=temp->C;
p2->Sport=temp->Sport;
p2->Sum=temp->Sum;
}
int Goodsmessage::ListCount()//统计当前链表的记录总数,返回一个整数
{
if(! Head)
return 0;
int n=0;
for(Goods * p=Head->Next;p!=End;p=p->Next)
n++;
return n;
}
void Goodsmessage::Paixu()//排序
{
showMenu(2);
int x;
cin>>x;
switch(x)
{
case 1:
{
system("cls");
Goods *p=NULL,*p1=NULL,*k=NULL;
int n=Goodsmessage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->Sum>k->Sum)
{
Goodsmessage::Swap(p,k);
}
}
Max_Min();
out.open("最值成绩.txt");
for(Goods *q=Head->Next;q!=End;q=q->Next)
{
out<<"编号:"<<q->num<<"\t姓名:"<<q->name<<"\t数学:"<<q->Math<<"\t英语:"<<q->English<<"\tC语言:"<<q->C<<"\t体育:"<<q->Sport<<"\t总成绩:"<<q->Sum<<'\n';
}
out.close();
cout<<"保存信息成功"<<endl;
cout <<"完成!"<<endl;
getch();
return;
}
case 2:
{
system("cls");
Goods *p=NULL,*p1=NULL,*k=NULL;
int n=Goodsmessage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->Sum>k->Sum)
{
Goodsmessage::Swap(p,k);
}
}
great();
cout<<"保存信息成功"<<endl;
cout <<"完成!"<<endl;
getch();
return;
}
case 3:
{
system("cls");
Goods *p=NULL,*p1=NULL,*k=NULL;
int n=Goodsmessage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->Sum>k->Sum)
{
Goodsmessage::Swap(p,k);
}
}
Display();
out.open("总分排名.txt");
for(Goods *q=Head->Next;q!=End;q=q->Next)
out<<"编号:"<<q->num<<" 姓名:"<<q->name<<" 总成绩:"<<q->Sum<<'\n';
out.close();
cout<<"保存信息成功"<<endl;
cout <<"排序完成!"<<endl;
getch();
return;
}
case 4:
{
system("cls");
Goods *p=NULL,*p1=NULL,*k=NULL;
int n=Goodsmessage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->Sum>k->Sum)
{
Goodsmessage::Swap(p,k);
}
}
Fail();
cout<<"保存信息成功"<<endl;
cout <<"完成!"<<endl;
getch();
return;
}
}
}
void Goodsmessage::Average()//平均数函数
{
showMenu(3);
int x;
cin>>x;
switch(x)
{
case 1: AverM();return;
case 2: AverE();return;
case 3: AverC();return;
case 4: AverS();return;
}
}
void Goodsmessage::Save()//保存到文件中
{
out.open("stuf.txt");
for(Goods *p=Head->Next;p!=End;p=p->Next)
out<<p->num<<"\t"<<p->name<<"\t"<<p->Math<<"\t"<<p->English<<"\t"<<p->C<<"\t"<<p->Sport<<"\t"<<p->Sum<<"\t"<<'\n';
out.close();
cout<<"保存信息成功"<<endl;
}
int main()
{
cout << "\t\t"<<endl;
for(int i=0;i<3;i++)
cout<<"\t\t━*\t\t\t\t\t\t━*"<<endl;cout<<"\t\t━*━*━*━*━*━*━*━*━*欢迎使用本系统**━*━*━*━*━*━*━*━*"<<endl;
for(int i=0;i<3;i++)
cout<<"\t\t━*\t\t\t\t\t\t━*"<<endl;
Goodsmessage Grade;
cout<<"\t\t\t\t按任意键开始……";
getch();
int x;
bool quit =false;
while(!quit)
{
Grade.showMenu(1);
cin>>x;
switch(x)
{
case 0:{quit=true;break;}
case 1:{Grade.AddItem();break;}
case 2:{Grade.Display();break;}
case 3:{Grade.Paixu();break;}
case 4:{Grade.Average();break;}
case 5:{Grade.Save();break;}
}
}
return 0;
}