21xrx.com
2025-06-29 07:42:55 Sunday
登录
文章检索 我的文章 写文章
C++成绩记录簿制作
2023-07-05 01:29:54 深夜i     13     0
C++ 成绩记录簿 制作 数据结构 文件存储

C++语言是一种高效的计算机编程语言,被广泛应用于软件开发领域。其中,C++成绩记录簿制作是一项常见的实践应用。本文将从以下三个方面,介绍如何使用C++语言制作成绩记录簿。

一、C++成绩记录簿的设计思路

C++成绩记录簿通常由学生信息、课程信息、成绩信息等组成。在设计时,需要明确每个信息项的类型、长度、数据结构等具体要求,并规划好它们之间的关系。一般而言,可以采用面向对象的方法,将学生信息、课程信息、成绩信息等封装为一个个对象,然后通过对象之间的组合和继承,构建出完整的成绩记录簿。

二、C++成绩记录簿的功能实现

C++成绩记录簿的核心功能是数据的录入、查询、修改、统计和输出。具体而言,可以通过定义类成员函数或全局函数等方法,实现以下功能:

1. 输入学生信息、课程信息、成绩信息等,并将其保存到相应的数据结构中。

2. 查询指定学生或课程的成绩信息,并输出查询结果。

3. 修改指定学生或课程的成绩信息,并将修改后的结果保存到相应的数据结构中。

4. 对所有学生或所有课程的成绩信息进行统计分析,并输出分析结果。

5. 将全部或部分成绩记录导出为Excel表格等格式。

三、C++成绩记录簿的实例代码

下面是一个简单的C++成绩记录簿实例代码,其中包含了学生信息、课程信息、成绩信息等基本元素和功能实现。代码中用到了C++的类、结构体、指针、数组、函数等语法,读者可以根据需要进行修改和扩展。

#include <iostream>
#include <string>
using namespace std;
struct student
  string name;
  int id;
  string major;
;
struct course
  string name;
  int credit;
;
struct score
  student stu;
  course cou;
  int grade;
;
class record
{
private:
  int num_stu;
  int num_cou;
  int num_score;
  student* stu_list;
  course* cou_list;
  score* sco_list;
public:
  record()
  
    num_stu = num_cou = num_score = 0;
    stu_list = NULL;
    cou_list = NULL;
    sco_list = NULL;
  
  void add_student(string name, int id, string major)
  {
    student* p = new student;
    p->name = name;
    p->id = id;
    p->major = major;
    stu_list = (student*)realloc(stu_list, sizeof(student) * (num_stu + 1));
    stu_list[num_stu] = *p;
    num_stu++;
  }
  void add_course(string name, int credit)
  {
    course* p = new course;
    p->name = name;
    p->credit = credit;
    cou_list = (course*)realloc(cou_list, sizeof(course) * (num_cou + 1));
    cou_list[num_cou] = *p;
    num_cou++;
  }
  void add_score(string name, int id, string course_name, int grade)
  {
    score* p = new score;
    p->stu.name = name;
    p->stu.id = id;
    p->cou.name = course_name;
    for (int i = 0; i < num_cou; i++)
    {
      if (cou_list[i].name == course_name)
      {
        p->cou.credit = cou_list[i].credit;
        break;
      }
    }
    p->grade = grade;
    sco_list = (score*)realloc(sco_list, sizeof(score) * (num_score + 1));
    sco_list[num_score] = *p;
    num_score++;
  }
  void search_student(int id)
  {
    for (int i = 0; i < num_stu; i++)
    {
      if (stu_list[i].id == id)
      {
        cout << "Name: " << stu_list[i].name << endl;
        cout << "Major: " << stu_list[i].major << endl;
        break;
      }
    }
  }
  void search_course(string name)
  {
    int total_grade = 0;
    int total_credit = 0;
    for (int i = 0; i < num_score; i++)
    {
      if (sco_list[i].cou.name == name)
      {
        cout << "Name: " << sco_list[i].stu.name << endl;
        cout << "ID: " << sco_list[i].stu.id << endl;
        cout << "Grade: " << sco_list[i].grade << endl;
        total_grade += sco_list[i].grade;
        total_credit += sco_list[i].cou.credit;
      }
    }
    if (total_credit > 0)
    
      cout << "Average Grade: " << total_grade / total_credit << endl;
    
  }
  void modify_score(string name, int id, string course_name, int grade)
  {
    for (int i = 0; i < num_score; i++)
    {
      if (sco_list[i].stu.name == name && sco_list[i].stu.id == id && sco_list[i].cou.name == course_name)
      {
        sco_list[i].grade = grade;
        break;
      }
    }
  }
  void analyze_score()
  {
    cout << "Total Students: " << num_stu << endl;
    cout << "Total Courses: " << num_cou << endl;
    cout << "Total Scores: " << num_score << endl;
    int max_grade = 0;
    int min_grade = 100;
    int total_grade = 0;
    int total_credit = 0;
    for (int i = 0; i < num_score; i++)
    {
      if (sco_list[i].grade > max_grade) max_grade = sco_list[i].grade;
      if (sco_list[i].grade < min_grade) min_grade = sco_list[i].grade;
      total_grade += sco_list[i].grade;
      total_credit += sco_list[i].cou.credit;
    }
    cout << "Max Grade: " << max_grade << endl;
    cout << "Min Grade: " << min_grade << endl;
    cout << "Average Grade: " << total_grade / num_score << endl;
    cout << "Average Credit: " << total_credit / num_score << endl;
  }
  void export_excel(string filename)
  
    //TODO: generate excel file
  
};
int main()
{
  record r;
  r.add_student("Tom", 101, "Computer Science");
  r.add_student("Jerry", 102, "Information Technology");
  r.add_course("Math", 3);
  r.add_course("English", 2);
  r.add_score("Tom", 101, "Math", 85);
  r.add_score("Tom", 101, "English", 90);
  r.add_score("Jerry", 102, "Math", 92);
  r.add_score("Jerry", 102, "English", 85);
  r.search_student(101);
  r.search_course("Math");
  r.modify_score("Jerry", 102, "Math", 95);
  r.analyze_score();
  r.export_excel("scores.xlsx");
  return 0;
}

以上就是本文对C++成绩记录簿制作的介绍和实例代码,希望对读者有所启发,欢迎大家进行拓展和改进。

  
  

评论区