#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <windows.h>
using namespace std;
void load();
void insertStudent();
void deleteStudent();
void alterStudent();
void showStudent();
void sortStudent();
void saveStudent();
void showMode();
class Student {
public:
int no;
string name;
double score;
Student();
Student(int no, string name, double score);
bool serach(int no);
void display();
};
/*必须实现,不然会报错*/
Student::Student()
{
}
Student::Student(int no, string name, double score)
{
this->no = no;
this->name = name;
this->score = score;
}
bool Student::serach(int no)
{
return this->no == no;
}
void Student::display()
{
cout<<no<<setw(12)<<name<<setw(12)<<score<<endl;
}
vector<Student> stu;
void showMode()
{
system("cls"); /*清理控制台文字*/
cout<<"*******欢迎进入学生成绩管理系统*******"<<endl;
cout<<"***********1,增加学生成绩************"<<endl;
cout<<"***********2,删除学生成绩************"<<endl;
cout<<"***********3,修改学生成绩************"<<endl;
cout<<"***********4,查询学生成绩************"<<endl;
cout<<"***********5,学生成绩排序************"<<endl;
cout<<"***********6,退出成绩系统************"<<endl;
cout<<"************************************"<<endl;
int choice;
cout<<"请输入你的选择:";
cin>>choice;
switch(choice)
{
case 1:insertStudent();break;
case 2:deleteStudent();break;
case 3:alterStudent();break;
case 4:showStudent();break;
case 5:sortStudent();break;
case 6:saveStudent();break;
default:break;
}
}
int main() {
/*加载文件中的学生成绩*/
load();
showMode();
return 0;
}
void load()
{
int no;
string name;
double score;
ifstream infile("student.txt");
if(!infile)
{
ofstream outfile("student.txt");
outfile.close();
return;
}
while(infile>>no>>name>>score)/*遇到空格或者换行符停止读取*/
{
Student s(no,name,score);
stu.push_back(s);
}
infile.close();
}
void insertStudent()
{
Student s;
cout<<"请输入学号:";
cin>>s.no;
for(int i=0;i<(int)stu.size();i++)
{
if (stu[i].serach(s.no))
{
cout<<"该学号学生信息已存在 "<<endl;
showMode();
return ;
}
}
cout<<"请输入姓名:";
cin>>s.name;
cout<<"请输入成绩:";
cin>>s.score;
stu.push_back(s);
s.display();
char choice;
cout<<"是否继续操作(y/n):";
cin>>choice;
if (choice == 'y')
insertStudent();
else
showMode();
}
void deleteStudent()
{
int no;
cout<<"请输入要删除的学生学号:";
cin>>no;
for(int i=0; i<(int)stu.size(); i++)
{
if(stu[i].serach(no))
{
stu.erase(stu.begin()+i);
}
}
char choice;
cout<<"是否继续操作(y/n):";
cin>>choice;
if (choice == 'y')
deleteStudent();
else
showMode();
}
void alterStudent()
{
int no;
string name;
double score;
cout<<"请输入要删修改的学生学号:";
cin>>no;
for(int i=0; i<(int)stu.size(); i++)
{
if(stu[i].serach(no))
{
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入成绩:";
cin>>score;
stu[i].name = name;
stu[i].score = score;
stu[i].display();
}
}
char choice;
cout<<"是否继续操作(y/n):";
cin>>choice;
if (choice == 'y')
alterStudent();
else
showMode();
}
void sortStudent()
{
for(int i=1;i<(int)stu.size();i++)
{
Student temp = stu[i];
for(int j=i-1;j>=0;j--)
{
if(stu[j].no > temp.no)
{
stu[j+1] = stu[j];
stu[j] = temp;
}
else break;
}
}
showStudent();
}
void showStudent()
{
for(int i=0;i<(int)stu.size();i++)
stu[i].display();
char choice;
cout<<"是否继续操作(y/n):";
cin>>choice;
if (choice == 'y')
showStudent();
else
showMode();
}
void saveStudent()
{
ofstream outfile("student.txt",ios::out|ios::trunc);
for(int i=0;i<(int)stu.size();i++)
{
outfile<<stu[i].no<<" "<<stu[i].name<<" "<<stu[i].score<<endl;
}
outfile.close();
stu.clear();
}