#include <iostream>
#include <string>
#include <iomanip>
#include<sstream>
#include <fstream>
using namespace std;
void Menu();
class Student
{
public:
void Add(Student &S);
void Dlete();
void Idseek();
void Nameseek();
void Grade();
void Sort(Student &S);
void SGrade();
void Pass();
void Save();
void Read();
void Sum()
{
sum = math + english + computer + politics;
}
void operator << (Student &S)
{
cout << setw(5) << S.num << setw(5) << S.name << setw(5) << S.sex << setw(5) << S.age << setw(5) << S.math << setw(5) << S.english << setw(5)
<< S.computer << setw(5) << S.politics << setw(5) << S.major << setw(5) << S.ave << endl;
}
private:
int num;
string name;
char sex;
int age;
double math;
double english;
double computer;
double politics;
string major;
double sum;
double ave;
};
int i, j=0, S_num;
Student s1[100000];
Student s2[100000];
int main()
{
fstream File("学生学籍管理系统.txt");
cout << "\n\t\t ---------检查文件中----------" << endl << endl;
if (!File)
{
cout << "\t\t*该文件不存在,请先创建!*" << endl;
exit(1);
}
cout << "\t\t*该文件存在,是否进入管理系统 *" << endl << endl;
cout << "\t\t* 进入请按1,不进入请按0 *" << endl << endl;
bool n;
cin >> n;
if (!n)
{
cout << "\t\t* 谢谢使用! *" << endl;
File.close();
exit(1);
}
File.close();
Student S;
while (1)
{
Menu();
int a;
cin >> a;
switch (a)
{
case 1 : {S.Add(S); break; }
case 2 : {S.Dlete(); break; }
case 3 : {S.Idseek(); break; }
case 4 : {S.Nameseek(); break; }
case 5 : {S.Grade(); break; }
case 6 : {S.SGrade(); break; }
case 7 : {S.Sort(S); break; }
case 8 : {S.Read(); break; }
case 0 : {exit(0);}
}
}
return 0;
}
void Menu()
{
system("cls");
cout << "\t\t\t------欢迎进入学生学籍管理系统------" << endl << endl;
cout << "\t\t\t| 按1添加学生信息 |" << endl << endl;
cout << "\t\t\t| 按2删除学生数据 |" << endl << endl;
cout << "\t\t\t| 按3根据学号查找学生信息 |" << endl << endl;
cout << "\t\t\t| 按4根据姓名查找学生信息 |" << endl << endl;
cout << "\t\t\t| 按5输入学生学号计算其总成绩 |" << endl << endl;
cout << "\t\t\t| 按6计算各科平均成绩和及格率 |" << endl << endl;
cout << "\t\t\t| 按7进行成绩排序 |" << endl << endl;
cout << "\t\t\t| 按8读取全部数据 |" << endl << endl;
cout << "\t\t\t| 按0退出系统 |" << endl << endl;
}
void Student::Add(Student &S)
{
while (1)
{
cout << "请输入学号(1,2,3...):";
cin >> S.num;
cout << "请输入姓名:";
cin >> S.name;
cout << "请输入性别(男:m ,女:f ):";
cin >> S.sex;
cout << "请输入年龄:";
cin >> S.age;
cout << "请输入数学成绩:";
cin >> S.math;
cout << "请输入英语成绩:";
cin >> S.english;
cout << "请输入c++成绩:";
cin >> S.computer;
cout << "请输入思修成绩:";
cin >> S.politics;
cout << "请输入学生专业:";
cin >> S.major;
cout << "保存数据请按1,否则请按0";
char a;
cin >> a;
if (a == '1')
{
s1[j] = S;
j++;
Save();
}
else
{
cout << "数据没保存" << endl << endl;
}
cout << "继续添加请按1,返回请按0" << endl;
char b;
cin >> b;
if (b == '0')
break;
}
}
void Student::Dlete()
{
while (1)
{
int num;
cout << "请输入你要删除学生的学号:";
cin >> num;
for (i = 0; i < j; i++)
{
if (s1[num - 1].num == s1[i].num)
for (int h = i; h < j; h++)
s1[h] = s1[h + 1];
}
int a;
cout << "确认删除请按1,取消操作请按0";
cin >> a;
if (a == 1)
{
j--;
Save();
}
else
{
cout << "操作已结束" << endl << endl;
}
cout << "继续删除请按1,返回请按0" << endl;
char b;
cin >> b;
if (b == '0')
break;
}
}
void Student:: Save()
{
ofstream outfile("学生学籍管理系统.txt", ios::app);
outfile << setw(5) << "学号" << setw(5) << "姓名" << setw(5) << "性别" << setw(5) << "年龄" << setw(9) << "数学成绩" << setw(9) << "英语成绩" << setw(9)
<< "c++成绩" << setw(9) << "思修成绩" << setw(7) << "专业" << endl;
for(i=0;i<j;i++)
outfile <<setw(5) << s1[i].num << setw(6) << s1[i].name << setw(7) << s1[i].sex << setw(7) << s1[i].age << setw(9) << s1[i].math << setw(9) << s1[i].english << setw(9)
<< s1[i].computer << setw(9) << s1[i].politics << setw(9) << s1[i].major << endl;
cout << "** 数据已保存! **" << endl << endl;
outfile.close();
}
void Student::Read()
{
cout << endl << endl;
ifstream infile("学生学籍管理系统.txt", ios::binary);
static char st[81];
while (!infile.eof())
{
infile.getline(st, 80);
cout << st << endl;
}
infile.close();
system("pause");
}
void Student::Idseek()
{
while (1)
{
bool n = 0;
int a;
cout << "请输入要查找的学生编号:";
cin >> a;
for (i = 0; i < j; i++)
if (a == s1[i].num )
{
cout << setw(5) << "学号" << setw(5) << "姓名" << setw(5) << "性别" << setw(5) << "年龄" << setw(9) << "数学成绩" << setw(9) << "英语成绩" << setw(9)
<< "c++成绩" << setw(9) << "思修成绩" << setw(9) << "专业" << endl;
s1[i] << s1[i];
n = 1;
}
if (n == 0)
cout << "输入的学号不存在" << endl;
char b;
cout << "继续查找请按1,返回请按0" << endl;
cin >> b;
if (b == '0')
break;
}
}
void Student::Nameseek()
{
while (1)
{
bool n = 0;
int a=0;
cout << "请输入要查找的学生姓名:";
cin >> name;
for (i = 0; i < j; i++)
if ( name.compare (s1[i].name ) == 0)
{
cout << setw(5) << "学号" << setw(5) << "姓名" << setw(5) << "性别" << setw(5) << "年龄" << setw(9) << "数学成绩" << setw(9) << "英语成绩" << setw(9)
<< "c++成绩" << setw(9) << "思修成绩" << setw(9) << "专业" << endl;
s1[i] << s1[i];
n = 1;
}
if (n == 0)
cout << "输入的姓名不存在" << endl;
char b;
cout << "继续查找请按1,返回请按0" << endl;
cin >> b;
if (b == '0')
break;
}
}
void Student::SGrade()
{ char line[256];
int intarr[5];
int linenum=1;
int m = 0, e = 0, c = 0, p = 0, n = 0, nmp = 0, nep = 0, ncp = 0, npp = 0;
ifstream ifile("学生学籍管理系统.txt");
while(ifile.good())
{
int sum=0;
ifile.getline(line,256);
//puts(line);
istringstream iss(line);
iss>>intarr[0]>>intarr[1]>>intarr[2]>>intarr[3]>>intarr[4];
cout<<"第" <<linenum<<"行"<<endl;
for(int i=0;i<5;++i)
{
sum+=intarr[i];
cout<<intarr[i]<<endl;
}
cout<<"第" <<linenum<<"行和:"<<sum<<endl;
linenum++;
}
ifile.close();
cout << setw(5) << m / n << setw(5) << nmp / n << setw(5) << e / n << setw(5) << nep / n << setw(5) << c / n << setw(5) << ncp / n
<< setw(5) << p / n << setw(5) << npp / n << endl;
cout<<9;
exit(0);
}
c++实现学生学籍管理系统
最新推荐文章于 2023-06-26 08:59:51 发布