计算机
c base
c++ 基础 作用域及生命周期
c++ template
c++ 内存视角
c++ 函数指针
c++ 基础 基础语法
c++ 多线程
c++ 性能
c++ 类 基础
c++ 类 对象模型 类析构
c++ 类 设计模式
cmake
CMAKE环境搭建 windows
创建第一个cmakelists.txt
构建稍复杂的项目
动态链接库
EX1
EX1 START
EX1 ANSWER
EX2
EX2 START
EX2 ANSWER
EX3
EX3 START
EX3 ANSWER
变量
控制流程
函数和宏
查找和使用外部库
生成器表达式
qt 开发环境
qt c++
理解QObject 1
理解QObject 4
qt index
qt qml quick
qt ui
qt 多线程
数据结构 数组
PC问题监控及排查
PC程序性能优化
OS
TOOL
C++ STL
编程漫谈
C++实战-生产者 消费者流水线
C++实战 IO
本站点使用 MrDoc 构建
-
+
EX3 ANSWER
```cmakelists.txt cmake_minimum_required(VERSION 3.15) project(GradeManager VERSION 1.0) # 定义源文件列表 set(SOURCES src/student.cpp src/grade.cpp src/report.cpp ) # 创建可执行文件 add_executable(grademanager main.cpp ${SOURCES} ) # 设置包含目录 target_include_directories(grademanager PRIVATE include/) # 设置 C++ 标准 set_property(TARGET grademanager PROPERTY CXX_STANDARD 17) set_property(TARGET grademanager PROPERTY CXX_STANDARD_REQUIRED ON) ``` ```main.cpp #include <iostream> #include <vector> #include <string> #include "student.h" #include "grade.h" #include "report.h" int main() { std::cout << "Student Grade Manager" << std::endl; std::cout << "=====================\n" << std::endl; // Get student information std::string name; int id; std::cout << "Enter student name: "; std::getline(std::cin, name); std::cout << "Enter student ID: "; std::cin >> id; Student student(name, id); // Get grades std::vector<double> grades; double grade; std::cout << "\nEnter grades (enter -1 to finish):" << std::endl; int count = 1; while (true) { std::cout << "Grade " << count << ": "; std::cin >> grade; if (grade == -1) { break; } if (grade < 0 || grade > 100) { std::cout << "Invalid grade. Please enter a value between 0 and 100." << std::endl; continue; } grades.push_back(grade); count++; } // Print report printReport(student, grades); return 0; } ``` ```src/grade.cpp #include "grade.h" double calculateAverage(const std::vector<double>& grades) { if (grades.empty()) { return 0.0; } double sum = 0.0; for (double grade : grades) { sum += grade; } return sum / grades.size(); } bool isPassing(double average) { return average >= 60.0; } char getLetterGrade(double average) { if (average >= 90.0) return 'A'; if (average >= 80.0) return 'B'; if (average >= 70.0) return 'C'; if (average >= 60.0) return 'D'; return 'F'; } ```report.cpp #include "report.h" #include "grade.h" #include <iostream> #include <iomanip> void printReport(const Student& student, const std::vector<double>& grades) { std::cout << "\nStudent Report" << std::endl; std::cout << "==============" << std::endl; std::cout << "Name: " << student.getName() << std::endl; std::cout << "ID: " << student.getId() << std::endl; std::cout << "Number of grades: " << grades.size() << std::endl; if (grades.empty()) { std::cout << "No grades entered." << std::endl; return; } double average = calculateAverage(grades); std::cout << std::fixed << std::setprecision(2); std::cout << "Average: " << average << std::endl; std::cout << "Status: " << (isPassing(average) ? "PASS" : "FAIL") << std::endl; std::cout << "Letter Grade: " << getLetterGrade(average) << std::endl; } ```student.cpp #include "student.h" Student::Student(const std::string& name, int id) : name(name), id(id) { } std::string Student::getName() const { return name; } int Student::getId() const { return id; } ``` ```include/grade.h #pragma once #include <vector> /** * Calculate the average of a list of grades */ double calculateAverage(const std::vector<double>& grades); /** * Check if the average grade is passing (>= 60) */ bool isPassing(double average); /** * Get letter grade based on average * A: 90+, B: 80-89, C: 70-79, D: 60-69, F: <60 */ char getLetterGrade(double average); ``` ```report.h #pragma once #include <string> #include <vector> #include "student.h" /** * Print a formatted grade report for a student */ void printReport(const Student& student, const std::vector<double>& grades); ``` ```student.h #pragma once #include <string> class Student { private: std::string name; int id; public: Student(const std::string& name, int id); std::string getName() const; int getId() const; }; ```
peipeo
2026年5月16日 08:55
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码