21xrx.com
2024-11-22 05:33:24 Friday
登录
文章检索 我的文章 写文章
C++学生成绩管理系统源代码
2023-07-08 12:08:03 深夜i     --     --
C++ 学生成绩管理系统 源代码 学生信息 成绩统计

学生成绩管理系统是一种常见的计算机应用程序,它可以用于记录和管理学生的成绩信息。开发一个学生成绩管理系统需要一定的编程技能,下面是一个用C++语言编写的学生成绩管理系统的源代码,供大家参考学习。

该学生成绩管理系统具有以下功能:添加学生信息、修改学生信息、删除学生信息、查询学生成绩、统计学生总数、按学号排序输出学生信息,并采用了文件存储数据的方式,保证了数据的持久性。

源代码如下:


#include <iostream>

#include <fstream>

#include <cstdlib>

#include <cstring>

#include <iomanip>

using namespace std;

struct student {

  int id;

  char name[10];

  double score1;

  double score2;

  double score3;

  double totalScore;

};

void addStudent();

void modifyStudent();

void deleteStudent();

void searchStudent();

void showAllStudent();

void sortById();

void showMenu();

const char* filename = "student.txt";

int main()

{

  showMenu();

  return 0;

}

void addStudent()

{

  ofstream outFile(filename, ios::app);

  if (!outFile) {

    cerr << "Fail to open file " << filename << endl;

    exit(EXIT_FAILURE);

  }

  cout << "Please input student id: ";

  int id;

  cin >> id;

  cout << "Please input student name: ";

  char name[10];

  cin >> name;

  cout << "Please input score1: ";

  double score1;

  cin >> score1;

  cout << "Please input score2: ";

  double score2;

  cin >> score2;

  cout << "Please input score3: ";

  double score3;

  cin >> score3;

  double totalScore = score1 + score2 + score3;

  outFile << id << '\t' << name << '\t' << score1 << '\t' << score2 << '\t' << score3 << endl;

  outFile.close();

}

void modifyStudent()

{

  ifstream inFile(filename);

  if (!inFile) {

    cerr << "Fail to open file " << filename << endl;

    exit(EXIT_FAILURE);

  }

  ofstream outFile("temp.txt", ios::app);

  if (!outFile) {

    cerr << "Fail to open file temp.txt" << endl;

    exit(EXIT_FAILURE);

  }

  int id;

  cout << "Please input the student id you want to modify: ";

  cin >> id;

  bool found = false;

  student s;

  while (inFile >> s.id >> s.name >> s.score1 >> s.score2 >> s.score3) {

    if (s.id == id) {

      found = true;

      cout << "Please input new score1: ";

      cin >> s.score1;

      cout << "Please input new score2: ";

      cin >> s.score2;

      cout << "Please input new score3: ";

      cin >> s.score3;

      s.totalScore = s.score1 + s.score2 + s.score3;

    }

    outFile << s.id << '\t' << s.name << '\t' << s.score1 << '\t' << s.score2 << '\t' << s.score3 << endl;

  }

  if (!found)

    cout << "The student with id " << id << " is not found." << endl;

  

  inFile.close();

  outFile.close();

  remove(filename);

  rename("temp.txt", filename);

}

void deleteStudent()

{

  ifstream inFile(filename);

  if (!inFile) {

    cerr << "Fail to open file " << filename << endl;

    exit(EXIT_FAILURE);

  }

  ofstream outFile("temp.txt", ios::app);

  if (!outFile) {

    cerr << "Fail to open file temp.txt" << endl;

    exit(EXIT_FAILURE);

  }

  int id;

  cout << "Please input the student id you want to delete: ";

  cin >> id;

  bool found = false;

  student s;

  while (inFile >> s.id >> s.name >> s.score1 >> s.score2 >> s.score3) {

    if (s.id == id)

      found = true;

    

    else {

      outFile << s.id << '\t' << s.name << '\t' << s.score1 << '\t' << s.score2 << '\t' << s.score3 << endl;

    }

  }

  if (!found)

    cout << "The student with id " << id << " is not found." << endl;

  

  inFile.close();

  outFile.close();

  remove(filename);

  rename("temp.txt", filename);

}

void searchStudent()

{

  ifstream inFile(filename);

  if (!inFile) {

    cerr << "Fail to open file " << filename << endl;

    exit(EXIT_FAILURE);

  }

  int id;

  cout << "Please input the student id you want to search: ";

  cin >> id;

  bool found = false;

  student s;

  while (inFile >> s.id >> s.name >> s.score1 >> s.score2 >> s.score3) {

    if (s.id == id) {

      found = true;

      cout << "ID\tName\tScore1\tScore2\tScore3\tTotal Score" << endl;

      cout << s.id << '\t' << s.name << '\t' << s.score1 << '\t' << s.score2 << '\t' << s.score3 << '\t' << s.totalScore << endl;

      break;

    }

  }

  if (!found)

    cout << "The student with id " << id << " is not found." << endl;

  

  inFile.close();

}

void showAllStudent()

{

  ifstream inFile(filename);

  if (!inFile) {

    cerr << "Fail to open file " << filename << endl;

    exit(EXIT_FAILURE);

  }

  cout << "ID\tName\tScore1\tScore2\tScore3\tTotal Score" << endl;

  student s;

  while (inFile >> s.id >> s.name >> s.score1 >> s.score2 >> s.score3) {

    s.totalScore = s.score1 + s.score2 + s.score3;

    cout << s.id << '\t' << s.name << '\t' << s.score1 << '\t' << s.score2 << '\t' << s.score3 << '\t' << s.totalScore << endl;

  }

  inFile.close();

}

void sortById()

{

  ifstream inFile(filename);

  if (!inFile) {

    cerr << "Fail to open file " << filename << endl;

    exit(EXIT_FAILURE);

  }

  student s[100];

  int i = 0;

  while (inFile >> s[i].id >> s[i].name >> s[i].score1 >> s[i].score2 >> s[i].score3) {

    s[i].totalScore = s[i].score1 + s[i].score2 + s[i].score3;

    i++;

  }

  inFile.close();

  int n = i;

  for (int i = 0; i < n - 1; i++) {

    for (int j = i + 1; j < n; j++) {

      if (s[i].id > s[j].id) {

        student temp = s[i];

        s[i] = s[j];

        s[j] = temp;

      }

    }

  }

  cout << "ID\tName\tScore1\tScore2\tScore3\tTotal Score" << endl;

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

    cout << s[i].id << '\t' << s[i].name << '\t' << s[i].score1 << '\t' << s[i].score2 << '\t' << s[i].score3 << '\t' << s[i].totalScore << endl;

  }

}

void showMenu()

{

  while (true) {

    cout << "1. Add student information" << endl;

    cout << "2. Modify student information" << endl;

    cout << "3. Delete student information" << endl;

    cout << "4. Search student information" << endl;

    cout << "5. Show all student information" << endl;

    cout << "6. Sort by student ID" << endl;

    cout << "7. Exit program" << endl;

    cout << "Please input your choice (1-7): ";

    int choice;

    cin >> choice;

    switch (choice) {

      case 1:

        addStudent();

        break;

      case 2:

        modifyStudent();

        break;

      case 3:

        deleteStudent();

        break;

      case 4:

        searchStudent();

        break;

      case 5:

        showAllStudent();

        break;

      case 6:

        sortById();

        break;

      case 7:

        exit(EXIT_SUCCESS);

      default:

        cout << "Please input a valid choice (1-7)." << endl;

    }

  }

}

通过上述代码,我们可以了解到C++语言的一些基本语法和文件读写操作的方法。这个学生成绩管理系统虽然简单,但基本功能都涵盖了,对学习C++语言的同学来说是一个不错的练手项目。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复