学生信息管理 定义学生类,包含学号 姓名 年龄 绩点这些成员
要求:
- 通过键盘输入学生信息,保存到文件中(文本文件、二进制文件全都要)
- 能够读取要求 1 中的文件,在屏幕上显示学生信息;
- 根据学生的绩点、年龄、学号进行排序,排序完成后,输出到文件和屏幕。
思路:首先创建一个结构体用于存储学生的信息,再编写获取学生信息的函数,获取信息后分别将学生信息加载到txt与dat文本中,编写相应的函数打印文件内容,加载文本文件后编写程序将信息按照学号、年龄、绩点排序的相关函数,再编写按照姓名(学号/绩点)查找学生的函数。编写完成后在main函数中测试每一个函数的功能。
头文件sadfx.h
#ifndef SYDFAX_H_INCLUDE
#define SYDFAX_H_INCLUDE
#pragma once
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
#endif // !SYDFAX_H_INCLUDE
头文件Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#pragma once
#include "sadfx.h"
//使用类存储每位学生的信息
class Student {
public:
string name;
double ID;
int age;
float GPA;
};
static int N = 100000;
static Student* pas = new Student[N];
//将学生信息加载到txt文本中
void intxt(string filename)
{
ofstream file(filename, ios::out);
//将学生信息导入txt文本中
for (int i = 0;i < N;i++) {
file << pas[i].name << "\t" << pas[i].ID << "\t"
<< pas[i].age << "\t" << pas[i].GPA << endl;
}
file.close();
}
//将学生信息加载到dat文本中
void indat(string filename)
{
ofstream file(filename, ios::binary);
for (int i = 0;i < N;i++) {
file.write((char*)&pas[i], sizeof(Student));
}
file.close();
}
//获取学生信息
void input()
{
cout << "请输入学生的总数N: ";
cin >> N;
int i = 0;
for (int i = 0;i < N;i++)
{
cout << "请依次输入一个学生的姓名,学号,年龄以及绩点: " << endl;
cin >> pas[i].name >> pas[i].ID >> pas[i].age >> pas[i].GPA;
}
//将学生信息导入txt文本中
intxt("student.txt");
//将学生信息导入dat二进制中
indat("student.dat");
//清除内存
if (pas)
{
delete[] pas;
pas = nullptr;
}
}
//通过txt文件在屏幕上显示学生信息
void txtshow(string filename)
{
char line[256];
fstream file;
file.open(filename, ios::in);
cout << "读取文本文件的内容为:" << endl;
cout << "姓名" << "\t" << "学号" << "\t" << "年龄" << "\t" << "绩点" << endl;
while(!file.eof())
{
file.getline(line, 128);
cout << line << endl;
}
file.close();
}
//通过dat文件在屏幕上显示学生信息
void datshow(string filename)
{
int i = 0;
ifstream file(filename, ios::binary);
cout << "读取文本文件的内容为:" << endl;
cout << "姓名" << "\t" << "学号" << "\t" << "年龄" << "\t" << "绩点" << endl;
while(!file.eof())
{
if (!file)
{
cout << "over!" << endl;
break;
}
file.read((char*)&pas[i], sizeof(Student));
cout << pas[i].name << "\t" << pas[i].ID << "\t"
<< pas[i].age << "\t" << pas[i].GPA << endl;
++i;
}
file.close();
}
//将txt文件中的数据加载到pas中
void load()
{
cout << "请输入文件中学生的的总数N: ";
cin >> N;
ifstream file("student.txt");
int i = 0;
while (!file.eof())
{
file >> pas[i].name >> pas[i].ID >> pas[i].age >> pas[i].GPA;
++i;
}
file.close();
}
//输出所有学生信息
void output(Student* pas)
{
cout << "姓名" << "\t" << "学号" << "\t" << "年龄" << "\t" << "绩点" << endl;
for (int i = 0;i < N;++i) {
cout << pas[i].name << "\t" << pas[i].ID << "\t"
<< pas[i].age << "\t" << pas[i].GPA << endl;
}
}
//按照学号排序
void IDsort(bool flag=true)
//默认排序顺序为从小到大,如果需要重大到小需要输入参数false
{
load();
Student temp;
int i = 0;
for (i = 0;i < N - 1;++i) {
for (int j = i + 1;j < N;++j) {
if (flag) {
if (pas[i].ID > pas[j].ID) {
temp = pas[i];
pas[i] = pas[j];
pas[j] = temp;
}
}
else {
if (pas[i].ID < pas[j].ID) {
temp = pas[i];
pas[i] = pas[j];
pas[j] = temp;
}
}
}
}
//在屏幕上输出
cout << "按学号从小到大的排序结果为:" << endl;
output(pas);
//将信息输入到IDsort.txt文件中
intxt("IDsort.txt");
}
//按照年龄排序
bool cmpAGE_1(Student& obj1, Student& obj2)
{
return (obj1.age > obj2.age) ? true : false;
}
bool cmpAGE_2(Student& obj1, Student& obj2)
{
return (obj1.age < obj2.age) ? true : false;
}
using PFCMP2 = bool (*)(Student&, Student&);
//默认从小到大排序,若需要从大到小只需要将参数修改为cmpAGE_2
void Agesort(PFCMP2 pf= cmpAGE_1)
{
load();
Student temp;
int i = 0, j = 0;
for (i = 0;i < N - 1;i++) {
for (j = 0;j < N - i - 1;++j) {
if (pf(pas[j], pas[j + 1])) {
temp = pas[j];
pas[j] = pas[j + 1];
pas[j + 1] = temp;
}
}
}
//在屏幕上输出
cout << "按年龄从小到大的排序结果为:" << endl;
output(pas);
//将信息输入到IDsort.txt文件中
intxt("AGEsort.txt");
}
//按照绩点排序
//默认从小到大排序,若需要从大到小只需要将参数修改为false
void GPAsort(bool flag = true)
{
load();
Student temp;
int i = 0;
for (i = 0;i < N - 1;++i) {
for (int j = i + 1;j < N;++j) {
if (flag) {
if (pas[i].GPA > pas[j].GPA) {
temp = pas[i];
pas[i] = pas[j];
pas[j] = temp;
}
}
else {
if (pas[i].GPA < pas[j].GPA) {
temp = pas[i];
pas[i] = pas[j];
pas[j] = temp;
}
}
}
}
//在屏幕上输出
cout << "按绩点从小到大的排序结果为:" << endl;
output(pas);
//将信息输入到IDsort.txt文件中
intxt("GPAsort.txt");
}
//按照姓名查找学生
void searchNAME()
{
load();
int i = 0;
string youNAME;
cout << "请输入姓名:";
cin >> youNAME;
bool flag = true;
for (i = 0;i < N;++i)
{
if (youNAME == pas[i].name)
{
cout << "这位同学的信息为:" << endl;
cout << "姓名" << "\t" << "学号" << "\t" << "年龄" << "\t" << "绩点" << endl;
cout << pas[i].name << "\t" << pas[i].ID << "\t"
<< pas[i].age << "\t" << pas[i].GPA << endl;
flag = false;
}
}
if (flag)
{
cout << "系统中没有这个学生的信息。" << endl;
}
}
//按照学号查找学生
void searchID()
{
load();
int i = 0;
double youID;
cout << "请输入学号:";
cin >> youID;
bool flag = true;
for (i = 0;i < N;++i)
{
if (youID == pas[i].ID)
{
cout << "这位同学的信息为:"<< endl;
cout << "姓名" << "\t" << "学号" << "\t" << "年龄" << "\t" << "绩点" << endl;
cout << pas[i].name << "\t" << pas[i].ID << "\t"
<< pas[i].age << "\t" << pas[i].GPA << endl;
flag = false;
}
}
if (flag)
{
cout << "系统中没有这个学生的信息。" << endl;
}
}
//按照年龄查找学生
void searchAGE()
{
load();
int i = 0;
double youAge;
cout << "请输入年龄:";
cin >> youAge;
bool flag = true;
for (i = 0;i < N;++i)
{
if (youAge == pas[i].age)
{
cout << "这位同学的信息为:" << endl;
cout << "姓名" << "\t" << "学号" << "\t" << "年龄" << "\t" << "绩点" << endl;
cout << pas[i].name << "\t" << pas[i].ID << "\t"
<< pas[i].age << "\t" << pas[i].GPA << endl;
flag = false;
}
}
if (flag)
{
cout << "系统中没有这个学生的信息。" << endl;
}
}
//按照绩点查找学生
void searchGPA()
{
load();
int i = 0;
double youGPA;
cout << "请输入绩点:";
cin >> youGPA;
bool flag = true;
for (i = 0;i < N;++i)
{
if (youGPA == pas[i].GPA)
{
cout << "这位同学的信息为:" << endl;
cout << "姓名" << "\t" << "学号" << "\t" << "年龄" << "\t" << "绩点" << endl;
cout << pas[i].name << "\t" << pas[i].ID << "\t"
<< pas[i].age << "\t" << pas[i].GPA << endl;
flag = false;
}
}
if (flag)
{
cout << "系统中没有这个学生的信息。" << endl;
}
}
#endif // !STUDENT_H_INCLUDED
源文件main.cpp
#include "sadfx.h"
#include "Student.h"
int main() {
//input(); //获取用户输入并存储到文件中
txtshow("student.txt"); //在屏幕上打印出txt文本信息
//datshow("student.dat"); //在屏幕上打印出dat文本信息
IDsort(); //按照学号排序并存入txt文本
//Agesort(); //按照年龄排序并存入txt文本
//GPAsort(); //按照绩点排序并存入txt文本
searchNAME(); //按照学号在txt文本查找用户
//searchID(); //按照学号在txt文本查找用户
//searchAGE(); //按照年龄在txt文本查找用户
//searchGPA(); //按照绩点在txt文本查找用户
return 0;
}