基于new bing加以修改的编写:
#include <iostream> // 引入输入输出流头文件
#include <string> // 引入字符串头文件
using namespace std; // 使用命名空间 std
// 定义学生类
class Student {
public:
string id; // 学号
string name; // 姓名
float math_score; // 高数成绩
float eng_score; // 英语成绩
};
// 定义学生数组类
class StudentArray {
private:
Student *ptr; // 指向 Student 类型对象数组的指针
int size; // 数组大小
public:
// 构造函数
StudentArray(int n) {
ptr = new Student[n]; // 动态分配内存空间
size = n;
}
// 析构函数
~StudentArray() {
delete[] ptr; // 释放内存空间
}
// 添加学生函数
void add_student() {
string id, name;
float math_score, eng_score;
cout << "请输入学生的学号:"; // 输出提示信息
cin >> id; // 输入学号
// 判断学号是否已经存在
for (int i = 0; i < size; i++) { // 遍历学生数组
if (ptr[i].id == id) { // 如果学号已经存在
cout << "该学号已经存在,请重新输入!" << endl; // 输出提示信息
return; // 返回,不再执行后面的代码
}
}
cout << "请输入学生的姓名:"; // 输出提示信息
cin >> name; // 输入姓名
cout << "请输入学生的高数成绩:"; // 输出提示信息
cin >> math_score; // 输入高数成绩
// 判断高数成绩是否有效
if (math_score < 0 || math_score > 100) { // 如果高数成绩无效(小于0或大于100)
cout << "成绩无效,请重新输入!" << endl; // 输出提示信息
return; // 返回,不再执行后面的代码
}
cout << "请输入学生的英语成绩:"; // 输出提示信息
cin >> eng_score; // 输入英语成绩
// 判断英语成绩是否有效
if (eng_score < 0 || eng_score > 100) { // 如果英语成绩无效(小于0或大于100)
cout << "成绩无效,请重新输入!" << endl; // 输出提示信息
return; // 返回,不再执行后面的代码
}
// 将学生信息添加到第一个空闲位置
for (int i = 0; i < size; i++) { // 遍历学生数组
if (ptr[i].id == "") { // 判断是否为空闲位置
ptr[i].id = id; // 设置学号
ptr[i].name = name; // 设置姓名
ptr[i].math_score = math_score; // 设置高数成绩
ptr[i].eng_score = eng_score; // 设置英语成绩
cout << "添加成功!" << endl; // 输出提示信息
return; // 返回,不再执行后面的代码
}
}
cout << "学生数组已满,无法添加新学生!" << endl; // 输出提示信息
}
// 输出成绩列表函数
void print_scores() {
cout << "高数成绩\t英语成绩\t学号\t姓名" << endl; // 输出表头
float math_sum = 0, eng_sum = 0, math_max = 0, eng_max = 0, math_min = 100, eng_min = 100, count = 0;
// 遍历学生数组
for (int i = 0; i < size; i++) {
if (ptr[i].id != "") { // 判断是否是有效学生
cout << ptr[i].math_score << "\t\t" << ptr[i].eng_score << "\t\t" << ptr[i].id << "\t" << ptr[i].name << endl;
// 计算高数成绩和、英语成绩和、有效学生数,并获取最值
math_sum += ptr[i].math_score;
eng_sum += ptr[i].eng_score;
count++;
math_max = max(math_max, ptr[i].math_score); // 使用标准库函数 max() 获取最值
eng_max = max(eng_max, ptr[i].eng_score);
math_min = min(math_min, ptr[i].math_score); // 使用标准库函数 min() 获取最值
eng_min = min(eng_min, ptr[i].eng_score);
}
}
// 输出高数成绩和英语成绩的最值和平均分
cout << "高数成绩最高分:" << math_max << endl;
cout << "高数成绩最低分:" << math_min << endl;
cout << "高数成绩平均分:" << static_cast<float>(math_sum) / count << endl;
cout << "英语成绩最高分:" << eng_max << endl;
cout << "英语成绩最低分:" << eng_min << endl;
cout << "英语成绩平均分:" << static_cast<float>(eng_sum) / count << endl;
}
// 查询学生成绩函数
void search_score() {
string id;
cout << "请输入学生的学号:"; // 输出提示信息
cin >> id; // 输入学号
// 遍历学生数组,查找对应学生的成绩信息
for (int i = 0; i < size; i++) {
if (ptr[i].id == id) { // 如果找到对应的学生
cout << "高数成绩:" << ptr[i].math_score << endl; // 输出高数成绩
cout << "英语成绩:" << ptr[i].eng_score << endl; // 输出英语成绩
return; // 返回,不再执行后面的代码
}
}
cout << "该学生不存在,请重新输入!" << endl;
}
};
// 主函数
int main() {
int n;
cout << "请输入学生的个数:"; // 输出提示信息
cin >> n; // 输入学生个数
StudentArray sa(n); // 创建学生数组对象
int op;
while (true) { // 循环执行
cout << endl << "请选择操作:" << endl; // 输出提示信息
cout << "1. 添加学生" << endl; // 输出选项
cout << "2. 查看成绩列表" << endl; // 输出选项
cout << "3. 查询学生成绩" << endl; // 输出选项
cout << "4. 退出" << endl; // 输出选项
cin >> op; // 输入操作选项
switch (op) { // 判断操作选项
case 1:
sa.add_student(); // 调用添加学生函数
break;
case 2:
sa.print_scores(); // 调用输出成绩列表函数
break;
case 3:
sa.search_score(); // 调用查询学生成绩函数
break;
case 4:
cout << "欢迎下次再使用!" << endl; // 输出提示信息
return 0; // 返回0,结束程序
default:
cout << "无效的操作,请重新输入!" << endl; // 输出提示信息
}
}
return 0;
}
定义一个结构体存放学生信息,使用malloc函数动态申请空间
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
//定义学生结构体
struct Student{
char id[20];
char name[20];
int math_score;
int english_score;
};
//输入学生信息
void input_student(struct Student *students,int n){
int i,j;
for(i=0;i<n;i++){
printf("请输入第%d个学生的学号,姓名,高数成绩和英语成绩:\n",i+1);
scanf("%s%s%d%d",students[i].id,students[i].name,&students[i].math_score,&students[i].english_score);
//判断输入合法性
if(strlen(students[i].id)!=10){
printf("学号长度不正确,请重新输入!\n");
i--;
continue;
}
for(j=0;j<i;j++){
if(strcmp(students[i].id,students[j].id)==0){
printf("学号重复,请重新输入!\n");
i--;
break;
}
}
if(students[i].math_score<0 || students[i].math_score>100){
printf("高数成绩不合法,请重新输入!\n");
i--;
continue;
}
if(students[i].english_score<0 || students[i].english_score>100){
printf("英语成绩不合法,请重新输入!\n");
i--;
continue;
}
}
}
//输出学生信息
void output_student(struct Student *students,int n){
printf("学号\t姓名\t高数成绩\t英语成绩\n");
printf("------------------------------------------------------------\n");
for(int i=0;i<n;i++){
printf("%s\t%s\t%d\t\t%d\n",students[i].id,students[i].name,students[i].math_score,students[i].english_score);
}
printf("------------------------------------------------------------\n");
}
//根据学号查询学生信息
void search_student(struct Student *students,int n){
char search_id[20];
int flag=0;
printf("请输入要查询的学生学号:\n");
scanf("%s",search_id);
for(int i=0;i<n;i++){
if(strcmp(students[i].id,search_id)==0){
printf("%s的高数成绩为%d,英语成绩为%d\n",students[i].name,students[i].math_score,students[i].english_score);
flag=1;
break;
}
}
if(flag==0){
printf("无此学生!\n");
}
}
int main(){
int n;
struct Student *students;
printf("请输入学生个数:\n");
scanf("%d",&n);
students=(struct Student *)malloc(sizeof(struct Student)*n);
input_student(students,n);
output_student(students,n);
while(1){
search_student(students,n);
printf("输入0退出,任意键继续查询:\n");
int flag;
scanf("%d",&flag);
if(flag==0){
break;
}
}
free(students);
return 0;
}
该回答引用ChatGPT4与博主@晓码自在合作编写:
这是一个利用动态内存实现学生成绩管理的程序。可以根据以下思路实现:
具体代码可以如下:
c++
struct Student {
int id;
string name;
int mathScore;
int englishScore;
};
int main() {
int n; // 学生个数
cin >> n;
// 动态分配n个Student空间
Student* students = new Student[n];
// 录入学生信息,校验输入
for (int i = 0; i < n; i++) {
cin >> students[i].id >> students[i].name >> students[i].mathScore >> students[i].englishScore;
// 校验学号,成绩范围
}
// 打印成绩报告
// ...
// 提供成绩查询
while (true) {
int id;
cin >> id;
// 在students数组中查找id对应的学生,输出成绩信息
// 如果未找到,提示重新输入
}
// 释放内存
delete[] students;
}
这个程序实现了要求的功能,利用Student结构体和动态数组完成学生成绩的管理。
以下是C++的实现,包括动态数组的使用、成绩的录入、统计和查询功能:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string id; // 学号
string name; // 姓名
int math; // 高数成绩
int english; // 英语成绩
Student() {} // 默认构造函数
Student(string _id, string _name, int _math, int _english) :
id(_id), name(_name), math(_math), english(_english) {} // 带参数构造函数
};
class ScoreManager {
private:
Student *students; // 学生数组指针
int count; // 学生个数
public:
ScoreManager() {} // 默认构造函数
~ScoreManager() { // 析构函数
if (students != nullptr) {
delete[] students;
}
}
void inputStudents() {
cout << "请输入学生个数:";
cin >> count;
students = new Student[count]; // 动态申请数组空间
for (int i = 0; i < count; i++) {
cout << "请输入第" << i + 1 << "个学生的学号、姓名、高数成绩、英语成绩:" << endl;
string _id, _name;
int _math, _english;
cin >> _id >> _name >> _math >> _english;
bool idExists = false;
for (int j = 0; j < i; j++) { // 检测学号是否已经存在
if (students[j].id == _id) {
cout << "学号已经存在,请重新输入:" << endl;
idExists = true;
break;
}
}
if (!idExists) {
if (_math >= 0 && _math <= 100) {
students[i].math = _math;
} else {
students[i].math = 0;
cout << "高数成绩无效,已设置为0" << endl;
}
if (_english >= 0 && _english <= 100) {
students[i].english = _english;
} else {
students[i].english = 0;
cout << "英语成绩无效,已设置为0" << endl;
}
students[i].id = _id;
students[i].name = _name;
} else {
i--; // 如果学号已经存在,则重新录入该学生的信息
}
}
}
void printScores() {
cout << "学号\t姓名\t高数成绩\t英语成绩" << endl;
for (int i = 0; i < count; i++) {
cout << students[i].id << "\t" << students[i].name << "\t" << students[i].math << "\t\t" << students[i].english << endl;
}
// 统计最高分、最低分、平均分
int mathMax = students[0].math, mathMin = students[0].math, mathSum = 0;
int engMax = students[0].english, engMin = students[0].english, engSum = 0;
for (int i = 0; i < count; i++) {
mathMax = max(mathMax, students[i].math);
mathMin = min(mathMin, students[i].math);
mathSum += students[i].math;
engMax = max(engMax, students[i].english);
engMin = min(engMin, students[i].english);
engSum += students[i].english;
}
double mathAvg = static_cast<double>(mathSum) / count;
double engAvg = static_cast<double>(engSum) / count;
cout << "高数最高分:" << mathMax << ",最低分:" << mathMin << ",平均分:" << mathAvg << endl;
cout << "英语最高分:" << engMax << ",最低分:" << engMin << ",平均分:" << engAvg << endl;
}
void queryScore() {
string _id;
while (true) {
cout << "请输入要查询的学生学号(输入exit退出):" << endl;
cin >> _id;
if (_id == "exit") {
break;
}
bool found = false;
for (int i = 0; i < count; i++) {
if (students[i].id == _id) {
found = true;
cout << "学生" << students[i].name << "的高数成绩为:" << students[i].math << ",英语成绩为:" << students[i].english << endl;
break;
}
}
if (!found) {
cout << "学号不存在,请重新输入" << endl;
}
}
}
};
int main() {
ScoreManager sm;
sm.inputStudents();
sm.printScores();
sm.queryScore();
return 0;
}
在该实现中,Student类表示学生信息,包含学号、姓名、高数成绩和英语成绩。ScoreManager类表示学生成绩管理类,包含动态数组指针students和学生个数count两个成员变量。该类实现了录入学生信息、打印成绩列表、统计最高分、最低分和平均分以及查询学生成绩等功能。
在录入学生信息时,通过动态申请数组空间实现动态数组。同时,避免学号重复录入,并对成绩进行合法性检测。
在打印成绩列表时,通过循环遍历数组输出每个学生的信息,并统计最高分、最低分和平均分。
在查询学生成绩时,通过循环遍历数组查找学号是否存在,并输出相应的成绩信息。如果学号不存在,则提示重新输入。
最后,在程序结束时需要释放申请的内存空间,以避免内存泄漏。