c++按课程统计每门课程的平均成绩、最高分和最低分;
时间: 2024-05-10 11:21:32 浏览: 117
这个问题需要使用数据库中的聚合函数来实现。假设我们有一个名为"score"的表,包含以下字段:
- course:课程名称
- student:学生姓名
- score:学生成绩
我们可以使用以下SQL语句来按课程统计每门课程的平均成绩、最高分和最低分:
```
SELECT course, AVG(score) AS avg_score, MAX(score) AS max_score, MIN(score) AS min_score
FROM score
GROUP BY course;
```
这个查询语句将score表按照课程名称进行分组,然后使用AVG、MAX和MIN聚合函数计算每门课程的平均成绩、最高分和最低分。最后,查询结果将按照课程名称进行排序返回。
相关问题
1. 定义学生类,包括学号、姓名、性别、多门课程的成绩;假设有n个学生,由键盘输入学生信息; 2.定义类成员函数,计算每个学生的平均成绩; 3.按课程统计每门课程的平均成绩、最高分和最低分; 4.同时输出所有学生信息; 5. 通过输入学生学号或姓名查询某学生的信息。
1. 定义学生类:
```c++
class Student {
private:
string id; // 学号
string name; // 姓名
string gender; // 性别
vector<double> scores; // 多门课程的成绩
public:
Student(string id, string name, string gender, vector<double> scores) {
this->id = id;
this->name = name;
this->gender = gender;
this->scores = scores;
}
double getAverageScore() { // 计算平均成绩
double sum = 0;
for (double score : scores) {
sum += score;
}
return sum / scores.size();
}
string getId() { // 获取学号
return id;
}
string getName() { // 获取姓名
return name;
}
string getGender() { // 获取性别
return gender;
}
vector<double> getScores() { // 获取成绩
return scores;
}
};
```
2. 输入学生信息:
```c++
vector<Student> students;
int n;
cout << "请输入学生人数:";
cin >> n;
for (int i = 0; i < n; i++) {
string id, name, gender;
vector<double> scores;
cout << "请输入第" << i + 1 << "个学生的学号、姓名、性别和多门课程的成绩:";
cin >> id >> name >> gender;
for (int j = 0; j < 3; j++) {
double score;
cin >> score;
scores.push_back(score);
}
Student student(id, name, gender, scores);
students.push_back(student);
}
```
3. 统计每门课程的平均成绩、最高分和最低分:
```c++
vector<double> chineseScores, mathScores, englishScores;
for (Student student : students) {
chineseScores.push_back(student.getScores()[0]);
mathScores.push_back(student.getScores()[1]);
englishScores.push_back(student.getScores()[2]);
}
double chineseAverage = accumulate(chineseScores.begin(), chineseScores.end(), 0.0) / chineseScores.size();
double mathAverage = accumulate(mathScores.begin(), mathScores.end(), 0.0) / mathScores.size();
double englishAverage = accumulate(englishScores.begin(), englishScores.end(), 0.0) / englishScores.size();
double chineseMax = *max_element(chineseScores.begin(), chineseScores.end());
double mathMax = *max_element(mathScores.begin(), mathScores.end());
double englishMax = *max_element(englishScores.begin(), englishScores.end());
double chineseMin = *min_element(chineseScores.begin(), chineseScores.end());
double mathMin = *min_element(mathScores.begin(), mathScores.end());
double englishMin = *min_element(englishScores.begin(), englishScores.end());
cout << "语文平均成绩:" << chineseAverage << ",最高分:" << chineseMax << ",最低分:" << chineseMin << endl;
cout << "数学平均成绩:" << mathAverage << ",最高分:" << mathMax << ",最低分:" << mathMin << endl;
cout << "英语平均成绩:" << englishAverage << ",最高分:" << englishMax << ",最低分:" << englishMin << endl;
```
4. 输出所有学生信息:
```c++
cout << "所有学生信息:" << endl;
for (Student student : students) {
cout << "学号:" << student.getId() << ",姓名:" << student.getName() << ",性别:" << student.getGender() << ",成绩:";
for (double score : student.getScores()) {
cout << score << " ";
}
cout << ",平均成绩:" << student.getAverageScore() << endl;
}
```
5. 查询某学生的信息:
```c++
string query;
cout << "请输入查询学生的学号或姓名:";
cin >> query;
for (Student student : students) {
if (query == student.getId() || query == student.getName()) {
cout << "学号:" << student.getId() << ",姓名:" << student.getName() << ",性别:" << student.getGender() << ",成绩:";
for (double score : student.getScores()) {
cout << score << " ";
}
cout << ",平均成绩:" << student.getAverageScore() << endl;
}
}
```
问题描述:设学生信息包括:学号、姓名、期末成绩、平时成绩,对学生的学习成绩信息进行管理。 设计要求: 实现学生信息的录入、修改、插入、删除、查询、计算总评成绩、根据总评程序排序和划分等级、按学号查询学生成绩等。 (1)从文件输入学生的成绩信息,包含学号、姓名、期末成绩和平时成绩; (2)显示全部学生成绩信息; (3)更新某个学生的基本信息或课程成绩; (4)对成绩统计分析(总分、平均分、最高分、最低分、及格率等);(5)学号查找并显示某个学生的成绩;(6)按姓名查找并显示某个学生的成绩;(7)按总评成绩由高到低排序显示;(8)统计各分数段人数并显示; (9)设计一个菜单,具有上述规定的操作要求、退出系统等最基本的功能。(全部功能在devc++中一个程序实现)
这是一个典型的学生信息管理系统,可以使用C++语言实现。下面是一个简单的实现示例:
```
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string id;
string name;
double final_score;
double regular_score;
double total_score;
char level;
};
vector<Student> students;
void load_data(string filename) {
ifstream fin(filename);
if (!fin.is_open()) {
cout << "Failed to open file " << filename << endl;
return;
}
students.clear();
while (!fin.eof()) {
Student s;
fin >> s.id >> s.name >> s.final_score >> s.regular_score;
s.total_score = s.final_score * 0.6 + s.regular_score * 0.4;
students.push_back(s);
}
fin.close();
}
void save_data(string filename) {
ofstream fout(filename);
if (!fout.is_open()) {
cout << "Failed to open file " << filename << endl;
return;
}
for (int i = 0; i < students.size(); i++) {
Student s = students[i];
fout << s.id << " " << s.name << " " << s.final_score << " " << s.regular_score << endl;
}
fout.close();
}
void display_all() {
cout << "ID\tName\tFinal\tRegular\tTotal\tLevel" << endl;
for (int i = 0; i < students.size(); i++) {
Student s = students[i];
cout << s.id << "\t" << s.name << "\t" << s.final_score << "\t" << s.regular_score << "\t"
<< s.total_score << "\t" << s.level << endl;
}
}
void update_student() {
string id;
cout << "Enter the student ID to update: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
cout << "Enter new name: ";
cin >> students[i].name;
cout << "Enter new final score: ";
cin >> students[i].final_score;
cout << "Enter new regular score: ";
cin >> students[i].regular_score;
students[i].total_score = students[i].final_score * 0.6 + students[i].regular_score * 0.4;
return;
}
}
cout << "No such student found." << endl;
}
void calculate_statistics() {
double total = 0, average = 0, max_score = 0, min_score = 100;
int pass_count = 0, fail_count = 0;
for (int i = 0; i < students.size(); i++) {
Student s = students[i];
total += s.total_score;
if (s.total_score >= 60) {
pass_count++;
} else {
fail_count++;
}
if (s.total_score > max_score) {
max_score = s.total_score;
}
if (s.total_score < min_score) {
min_score = s.total_score;
}
}
average = total / students.size();
cout << "Total: " << total << endl;
cout << "Average: " << average << endl;
cout << "Highest score: " << max_score << endl;
cout << "Lowest score: " << min_score << endl;
cout << "Pass count: " << pass_count << endl;
cout << "Fail count: " << fail_count << endl;
}
void find_by_id() {
string id;
cout << "Enter the student ID to search: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
Student s = students[i];
cout << "ID\tName\tFinal\tRegular\tTotal\tLevel" << endl;
cout << s.id << "\t" << s.name << "\t" << s.final_score << "\t" << s.regular_score << "\t"
<< s.total_score << "\t" << s.level << endl;
return;
}
}
cout << "No such student found." << endl;
}
void find_by_name() {
string name;
cout << "Enter the student name to search: ";
cin >> name;
for (int i = 0; i < students.size(); i++) {
if (students[i].name == name) {
Student s = students[i];
cout << "ID\tName\tFinal\tRegular\tTotal\tLevel" << endl;
cout << s.id << "\t" << s.name << "\t" << s.final_score << "\t" << s.regular_score << "\t"
<< s.total_score << "\t" << s.level << endl;
}
}
}
void sort_by_total_score() {
sort(students.begin(), students.end(), [](const Student &a, const Student &b) {
return a.total_score > b.total_score;
});
display_all();
}
void count_level() {
int level_A = 0, level_B = 0, level_C = 0, level_D = 0, level_E = 0;
for (int i = 0; i < students.size(); i++) {
if (students[i].total_score >= 90) {
level_A++;
students[i].level = 'A';
} else if (students[i].total_score >= 80) {
level_B++;
students[i].level = 'B';
} else if (students[i].total_score >= 70) {
level_C++;
students[i].level = 'C';
} else if (students[i].total_score >= 60) {
level_D++;
students[i].level = 'D';
} else {
level_E++;
students[i].level = 'E';
}
}
cout << "A: " << level_A << endl;
cout << "B: " << level_B << endl;
cout << "C: " << level_C << endl;
cout << "D: " << level_D << endl;
cout << "E: " << level_E << endl;
}
void menu() {
while (true) {
cout << "1. Load data" << endl;
cout << "2. Save data" << endl;
cout << "3. Display all data" << endl;
cout << "4. Update student information" << endl;
cout << "5. Calculate statistics" << endl;
cout << "6. Find student by ID" << endl;
cout << "7. Find student by name" << endl;
cout << "8. Sort by total score" << endl;
cout << "9. Count level" << endl;
cout << "0. Exit" << endl;
int option;
cin >> option;
switch (option) {
case 1:
load_data("students.txt");
break;
case 2:
save_data("students.txt");
break;
case 3:
display_all();
break;
case 4:
update_student();
break;
case 5:
calculate_statistics();
break;
case 6:
find_by_id();
break;
case 7:
find_by_name();
break;
case 8:
sort_by_total_score();
break;
case 9:
count_level();
break;
case 0:
return;
default:
cout << "Invalid option." << endl;
}
}
}
int main() {
menu();
return 0;
}
```
注意,这只是一个简单的实现示例,实际应用中可能需要更多的功能和更完善的错误处理。
阅读全文