#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <windows.h>
using namespace std;
const int COURSE_COUNT=5;
const int STU_COUNT=100;
struct student{
string sno;
string name;
double score[COURSE_COUNT];
double sum;
double average;
};
typedef student STU;
STU stu[STU_COUNT];//student stu[STU_COUNT];
int courseCount=0;
int stuCount=0;
void inputScoreFromFile();
void displayOneStudent(STU stu);
void displayAll(STU students[],int numberOfStudents);
void sortByGrade(STU students[],int numberOfStudents);
int searchByName(STU students[],int numberOfStudents,string name);
int menu();
int main()
{
int itemSelected = -1;
int index=-1;
string name;
while(true){
system("cls");
itemSelected = menu();
switch(itemSelected)
{
case 1:
break;
case 2:
inputScoreFromFile();
displayAll(stu,stuCount);
break;
case 3:
sortByGrade(stu,stuCount);
cout<<"================================"<<endl;
displayAll(stu,stuCount);
break;
case 4:
cout<<"Enter the name you want to search:";
cin>>name;
index = searchByName(stu,stuCount,name);
if(index==-1)
cout<<"Not Found!"<<endl;
else
displayOneStudent(stu[index]);
break;
case 0:
return 0;
default:
cout<<"Wrong choice you entered!"<<endl;
}
system("pause");
}
return 0;
}
void inputScoreFromFile()
{
ifstream inFile;
string fileName;
cout<<"Enter the scores data file name:";
cin>>fileName;
inFile.open(fileName.c_str());
inFile.ignore(200,'\n');
int i=0;
while(!inFile.eof())
{
inFile>>stu[i].sno>>stu[i].name;
stu[i].sum=0;
for(int j=0;j<COURSE_COUNT;j++)
{
inFile>>stu[i].score[j];
stu[i].sum += stu[i].score[j];
}
stu[i].average = stu[i].sum/COURSE_COUNT;
i++;
}
stuCount = i;
inFile.close();
}
void displayOneStudent(STU stu)
{
cout<<setw(10)<<stu.sno
<<setw(10)<<stu.name;
for(int i=0;i<COURSE_COUNT;i++)
cout<<setw(5)<<stu.score[i];
cout<<setw(6)<<stu.sum;
cout<<setw(5)<<stu.average;
cout<<endl;
}
void displayAll(STU students[],int numberOfStudents)
{
for(int i=0;i<numberOfStudents;i++)
displayOneStudent(students[i]);
}
void sortByGrade(STU students[],int numberOfStudents)
{
STU temp;
bool isSorted = false;
for(int i=0;i<numberOfStudents && isSorted==false;i++)
{
isSorted = true;
for(int j=0;j<numberOfStudents-i-1;j++)
{
if(students[j].sum<students[j+1].sum)
{
temp=students[j];
students[j]=students[j+1];
students[j+1]=temp;
isSorted = false;
}
}
}
}
int searchByName(STU students[],int numberOfStudents,string name)
{
for(int i=0;i<numberOfStudents;i++)
{
if(name.compare(students[i].name)==0)
return i;
}
return -1;
}
int menu()
{
cout<<"========Student Score Management System======="<<endl;
cout<<"\t 1. Input student information"<<endl;
cout<<"\t 2. Read student information from file"<<endl;
cout<<"\t 3. Sort by sum"<<endl;
cout<<"\t 4. Search by name"<<endl;
cout<<"\t 0. Exit"<<endl;
cout<<"=============================================="<<endl;
cout<<"Enter your choice:";
int itemSelected;
cin>>itemSelected;
return itemSelected;
}
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。