学生信息管理系统
#include<iostream>
#include<algorithm>
#include<windows.h>
#include<fstream>
#include<string>
using namespace std;
class student
{
private:
string stu_ID;
string stu_Name;
float stu_Score;
public:
void Set_ID(string ID);
void Set_Name(string Name);
void Set_Score(float Score);
string Show_ID();
string Show_Name();
float Show_Score();
};
void student::Set_ID(string ID)
{
stu_ID = ID;
}
void student::Set_Name(string Name)
{
stu_Name = Name;
}
void student::Set_Score(float Score)
{
stu_Score = Score;
}
string student::Show_ID()
{
return stu_ID;
}
string student::Show_Name()
{
return stu_Name;
}
float student::Show_Score()
{
return stu_Score;
}
int Init(student info[],int n)
{
string ID;
string Name;
float Score;
ifstream in("C:\\Users\\Lenovo\\Desktop\\student.txt");
while (! in.eof())
{
in >> ID >> Name >> Score;
info[n].Set_ID(ID);
info[n].Set_Name(Name);
info[n].Set_Score(Score);
n++;
}
in.close();
return n;
}
int Insert_Student(student info[],int n)
{
string ID;
string Name;
float Score;
cout << "请输入要添加学生的ID,Name,Score:" << endl;
loop:
cin >> ID >> Name >> Score ;
for (int i=0;i<n;i++)
{
if (info[i].Show_ID() == ID)
{
cout << "改学号已被注册,请你重新出入:" << endl;
goto loop;
}
}
info[n].Set_ID(ID);
info[n].Set_Name(Name);
info[n].Set_Score(Score);
cout << "添加成功!" << endl;
n++;
return n;
}
void Show_info(student info[],int n)
{
string ID;
cout << "请输入你要查询的学号:" << endl;
loop:
cin >> ID;
for (int i=0;i<n;i++)
{
if (ID == info[i].Show_ID())
{
cout << info[i].Show_ID() << "\t" ;
cout << info[i].Show_Name() << "\t" ;
cout << info[i].Show_Score() << endl;
return ;
}
}
cout << "对不起,你输入的学号并不存在。请你重新输入学号:" << endl;
goto loop;
}
int Delet_Student(student info[],int n)
{
cout << "下面是去全部学生的信息情况:" << endl;
for (int i=0;i<n;i++)
{
cout << info[i].Show_ID() << "\t" ;
cout << info[i].Show_Name() << "\t" ;
cout << info[i].Show_Score() << endl;
}
cout << "请输入你要删除的学号:" << endl;
string delID;
loop:
cin >> delID ;
for (int i=0;i<n;i++)
{
if (delID == info[i].Show_ID())
{
cout << "删除成功!" << endl;
for (int j=i;j<n-1;j++)
info[j] = info[j+1];
n--;
return n;
}
}
cout << "对不起,你要删除的学号并不存在。请你重新输入要删除的学号:" << endl;
goto loop;
}
void Save_info(student info[],int n)
{
ofstream out ("C:\\Users\\Lenovo\\Desktop\\student.txt");
for (int i=0;i<n;i++)
{
out << info[i].Show_ID() << '\t';
out << info[i].Show_Name() << '\t';
out << info[i].Show_Score() << endl;
}
cout << "保存成功!" << endl;
out.close();
}
void Main_Menu()
{
system("cls");
cout << endl;
cout << "1.添加学生信息:" << endl << endl;
cout << "2.查询学生信息:" << endl << endl;
cout << "3.删除学生信息:" << endl << endl;
cout << "4.保存修改:" << endl << endl;
cout << "5.操作结束,退出系统" << endl << endl;
cout << endl;
cout << "请输入指令:";
}
int main()
{
int n = 0;
student info[100];
n = Init(info,n);
int order;
while (true)
{
Main_Menu();
cin >> order;
if ( order == 1)
n = Insert_Student(info,n);
if ( order == 2)
Show_info(info,n);
if ( order == 3)
n = Delet_Student(info,n);
if ( order == 4)
Save_info(info,n);
if ( order == 5)
break;
system("pause");
}
return 0;
}