欢迎光临散文网 会员登陆 & 注册

CPP

2023-06-28 23:45 作者:国际大肠胃  | 我要投稿

#include <iostream>

#include <fstream>

#include <algorithm>

#include <string>

using namespace std;

class Student {

private:

    string name;

    int numb;

    int year;

    string sex;

    int birth;

    string address;

    int phone;

    string email;

public:

    static int count;

    Student() {}

    Student(string name, int numb, int year, string sex, int birth, string address, int phone, string email);

    ~Student() {}

    void chaxun(const Student* stu);

    void show() const;

    void in() const;

    static void add(Student students[], int& count);

    static void deletes(Student students[], int& count);

    static void paixu(Student students[], int count);

    static void cunchufile(const Student students[], int count);

    static void jiazaixinxi(Student students[], int& count);

    static void display(const Student students[], int count);

    static bool compareStudents(const Student& s1, const Student& s2);

};

int Student::count = 0;

Student::Student(string name1, int numb1, int year1, string sex1, int birth1, string address1, int phone1, string email1) {

    in();

    name = name1;

    numb = numb1;

    year = year1;

    sex = sex1;

    birth = birth1;

    address = address1;

    phone = phone1;

    email = email1;

}

void Student::chaxun(const Student* stu) {

    int a;

    cin >> a;

    if (a == stu->numb) {

        show();

    }

    else {

        cout << "查无此人" << endl;

    }

}

void Student::show() const {

    cout << "姓名:" << name << endl;

    cout << "学号:" << numb << endl;

    cout << "入学年份:" << year << endl;

    cout << "性别:" << sex << endl;

    cout << "出生日期:" << birth << endl;

    cout << "家庭地址:" << address << endl;

    cout << "联系电话:" << phone << endl;

    cout << "电子邮箱:" << email << endl;

}

void Student::in() const {

    ifstream in;

    in.open("students.txt", ios::out);

    in.close();

}

void Student::add(Student students[], int& count) {

    if (count < 100) {

        string name, sex, address, email;

        int numb, year, birth, phone;

        cout << "请输入学生姓名:";

        cin >> name;

        cout << "请输入学生学号:";

        cin >> numb;

        cout << "请输入学生入学年份:";

        cin >> year;

        cout << "请输入学生性别:";

        cin >> sex;

        cout << "请输入学生出生日期:";

        cin >> birth;

        cout << "请输入学生家庭地址:";

        cin >> address;

        cout << "请输入学生联系电话:";

        cin >> phone;

        cout << "请输入学生电子邮箱:";

        cin >> email;

        students[count] = Student(name, numb, year, sex, birth, address, phone, email);

        count++;

        cout << "添加学生信息成功!" << endl;

    }

}

void Student::deletes(Student students[], int& count) {

    int numb;

    cout << "请输入要删除的学生的学号:";

    cin >> numb;


    int index = -1;

    for (int i = 0; i < count; i++) {

        if (students[i].numb == numb) {

            index = i;

            break;

        }

    }


    if (index != -1) {

        for (int i = index; i < count - 1; i++) {

            students[i] = students[i + 1];

        }

        count--;

        cout << "删除成功!" << endl;

    }

    else {

        cout << "抱歉,未找到学号为" << numb << "的学生!" << endl;

    }

}

void Student::paixu(Student students[], int count) {

    sort(students, students + count, compareStudents);

    cout << "排序完成!" << endl;

}

void Student::cunchufile(const Student students[], int count) {

    ofstream file("students.txt");

    if (file.is_open()) {

        for (int i = 0; i < count; i++) {

            file<< students[i].name << " " << students[i].numb << " " << students[i].year << " "<< students[i].sex << " " << students[i].birth << " " << students[i].address << " "<< students[i].phone << " " << students[i].email << endl;

        }

        file.close();

        cout << "保存成功!" << endl;

    }

    else {

        cout << "无法打开文件!" << endl;

    }

}

void Student::jiazaixinxi(Student students[], int& count) {

    ifstream file("students.txt");

    if (file.is_open()) {

        string name, sex, address, email;

        int numb, year, birth, phone;

        while (file >> name >> numb >> year >> sex >> birth >> address >> phone >> email) 

{

            students[count] = Student(name, numb, year, sex, birth, address, phone, email);

            count++;

        }

        file.close();

        cout << "加载成功!" << endl;

    }

}

void Student::display(const Student students[], int count) {

    for (int i = 0; i < count; i++) 

{

        cout << "学生信息" << i + 1 << ":" << endl;

        students[i].show();

        cout << endl;

    }

}

bool Student::compareStudents(const Student& s1, const Student& s2) {

    return s1.name < s2.name;

}

int main() {

    Student students[100];

    int count = 0;

    int choice;

    while (true) {

    cout << "                                  **************欢迎来到学生信息管理系统**************" << endl;

    cout << "                                  **************1. 添加学生信息**************" << endl;

    cout << "                                  **************2. 删除学生信息**************" << endl;

    cout << "                                  **************3. 排序学生信息**************" << endl;

    cout << "                                  **************4. 显示学生信息**************" << endl;

    cout << "                                  **************5. 保存学生信息到文件**************" << endl;

    cout << "                                  **************6. 从文件加载学生信息**************" << endl;

    cout << "                                  **************0. 退出**************" << endl;

    cout << "**************请输入选择:";

    cin >> choice;

    switch (choice) {

    case 1:Student::add(students, count);break;

    case 2:Student::deletes(students, count);break;

    case 3:Student::paixu(students, count);break;

    case 4:Student::display(students, count);break;

case 5:Student::cunchufile(students, count);

case 6:Student::jiazaixinxi(students, count); break;

    case 0: break;

    default:cout << "错误" << endl;break;

        }

    } while (choice != 0);

    return 0;

}


CPP的评论 (共 条)

分享到微博请遵守国家法律