提示用户输入若干名同学的学号、姓名、高数成绩、英语成绩。每完成1个合法输入后,提示用户是否继续输入。之后按学号进行排序、统计,并完成输出。应避免重复学号,并进行成绩的合法性检验。
目前我只完成了第一步😭
#include <iostream>
#include <vector>
using namespace std;
struct Student {
int id;
string name;
int math_score;
int english_score;
};
vector<Student> students;
void input_students() {
while (true) {
cout << "请输入学号、姓名、高数成绩、英语成绩 (学号、姓名、成绩用空格分隔):";
string input;
getline(cin, input);
students.push_back(Student{input.begin(), input.end()});
cout << "是否继续输入?(y/n)" << endl;
char answer;
cin >> answer;
if (answer == 'y' || answer == 'Y') {
continue;
} else {
break;
}
}
}
void sort_students() {
for (int i = 0; i < students.size(); i++) {
for (int j = i + 1; j < students.size(); j++) {
if (students[i].id < students[j].id) {
students.swap(i, j);
i--;
j--;
}
}
}
}
void print_students() {
cout << "排序后的成绩列表:" << endl;
for (int i = 0; i < students.size(); i++) {
cout << students[i].id << " " << students[i].name << " " << students[i].math_score << " " << students[i].english_score << endl;
}
}
void print_statistics() {
int num_students = students.size();
cout << "学生人数:" << num_students << endl;
for (int i = 0; i < num_students; i++) {
int num_valid_scores = 0;
for (int j = 0; j < students.size(); j++) {
if (students[j].id == students[i].id) {
num_valid_scores++;
}
}
if (num_valid_scores == 0) {
cout << "该学号不存在!" << endl;
continue;
}
int max_score = 0;
int min_score = 99;
int avg_score = 0;
int invalid_score_rate = 0;
for (int j = 0; j < students.size(); j++) {
if (students[j].id == students[i].id) {
int score = students[j].math_score;
if (score > max_score) {
max_score = score;
}
if (score < min_score) {
min_score = score;
}
int avg_score = (max_score + min_score) / 2;
if (score < 60) {
invalid_score_rate++;
}
avg_score += score;
}
}
if (max_score > 99) {
cout << "该学号存在不及格的学生!" << endl;
}
if (avg_score < 60) {
cout << "该学号存在不及格的学生!" << endl;
}
cout << "最高成绩:" << max_score << ",最低成绩:" << min_score << ",平均成绩:" << avg_score << ",不及格率:" << invalid_score_rate << endl;
}
}
int main() {
input_students();
sort_students();
print_students();
print_statistics();
return 0;
}
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
struct Student
{
string Id;
string Name;
int AcceleratedMathScore;
int EnglishScore;
};
void InputInformation(vector<Student> &students)
{
char con;
cout << "请输入学生信息:" << endl;
while (true)
{
Student stu;
cin >> stu.Id >> stu.Name >> stu.AcceleratedMathScore >> stu.EnglishScore;
if (any_of(students.begin(), students.end(), [=](Student s) { return s.Id == stu.Id; }))
{
cout << "学号重复!" << endl;
continue;
}
if (stu.AcceleratedMathScore < 0 || stu.AcceleratedMathScore > 100)
{
cout << "高数成绩不合法!" << endl;
continue;
}
if (stu.EnglishScore < 0 || stu.EnglishScore > 100)
{
cout << "英语成绩不合法!" << endl;
continue;
}
students.emplace_back(stu);
cout << "是否继续输入 (y/n): ";
cin >> con;
if (con == 'n') break;
}
cout << endl;
}
void SortAndOutput(vector<Student> &students)
{
sort(students.begin(), students.end(), [](const Student &a, const Student &b) { return a.Id < b.Id; });
cout << "学号\t 姓名\t高数\t英语" << endl;
for (const auto &student : students)
{
cout << setw(9) << left << student.Id;
cout << setw(8) << left << student.Name;
cout << setw(8) << left << student.AcceleratedMathScore;
cout << setw(8) << left << student.EnglishScore;
cout << endl;
}
cout << endl;
}
void Stat(const vector<Student> &students)
{
auto amCmp = [](const Student &a, const Student &b)
{
return a.AcceleratedMathScore < b.AcceleratedMathScore;
};
auto enCmp = [](const Student &a, const Student &b)
{
return a.EnglishScore < b.EnglishScore;
};
auto maxAmIter = max_element(students.begin(), students.end(), amCmp);
int maxAmScore = (*maxAmIter).AcceleratedMathScore;
auto minAmIter = min_element(students.begin(), students.end(), amCmp);
int minAmScore = (*minAmIter).AcceleratedMathScore;
auto maxEnIter = max_element(students.begin(), students.end(), enCmp);
int maxEnScore = (*maxEnIter).EnglishScore;
auto minEnIter = min_element(students.begin(), students.end(), enCmp);
int minEnScore = (*minEnIter).EnglishScore;
double avgAm = accumulate(students.begin(), students.end(), 0, [](int a, const Student &b)
{
return a + b.AcceleratedMathScore;
}) / (double)students.size();
double avgEn = accumulate(students.begin(), students.end(), 0, [](int a, const Student &b)
{
return a + b.EnglishScore;
}) / (double)students.size();
int failCountAm = count_if(students.begin(), students.end(), [](const Student &s)
{
return s.AcceleratedMathScore < 60;
});
int failCountEn = count_if(students.begin(), students.end(), [](const Student &s)
{
return s.EnglishScore < 60;
});
cout << "学生人数: " << students.size() << endl;
cout << endl;
cout << "高数最高成绩: " << maxAmScore << endl;
cout << "高数最低成绩: " << minAmScore << endl;
cout << "高数平均成绩: " << avgAm << endl;
cout << "高数不及格率: " << fixed << setprecision(2) << failCountAm / (double)students.size() * 100.0 << endl;
cout << endl;
cout << "英语最高成绩: " << maxEnScore << endl;
cout << "英语最低成绩: " << minEnScore << endl;
cout << "英语平均成绩: " << avgEn << endl;
cout << "英语不及格率: " << fixed << setprecision(2) << failCountEn / (double)students.size() * 100.0 << endl;
cout << endl;
}
int main()
{
vector<Student> students;
char con;
start:
InputInformation(students);
SortAndOutput(students);
Stat(students);
cout << "是否继续输入新的学生信息 (y/n): ";
cin >> con;
if (con == 'y') goto start;
return 0;
}
引用chatgpt如下回答:
以下是一份可以实现上述功能的 Java 代码示例:
import java.util.Scanner;
import java.util.Vector;
class Student {
String id;
String name;
int math;
int english;
public Student(String id, String name, int math, int english) {
super();
this.id = id;
this.name = name;
this.math = math;
this.english = english;
}
// 重写 toString 方法,以便输出学生信息
public String toString() {
return id + " " + name + " " + math + " " + english;
}
}
public class StudentGrade {
public static void main(String[] args) {
Vector<Student> students = new Vector<Student>();
Scanner sc = new Scanner(System.in);
int count = 0;
while (true) {
System.out.println("请输入学生的学号、姓名、高数成绩、英语成绩(用空格分隔),输入 q 退出:");
String line = sc.nextLine().trim();
if (line.equals("q")) {
break;
}
String[] fields = line.split(" ");
if (fields.length != 4) {
System.out.println("输入格式不正确,请重新输入。");
continue;
}
String id = fields[0].trim();
String name = fields[1].trim();
int math;
int english;
try {
math = Integer.parseInt(fields[2].trim());
english = Integer.parseInt(fields[3].trim());
} catch (NumberFormatException e) {
System.out.println("成绩输入格式不正确,请重新输入。");
continue;
}
// 检查学号是否重复
boolean hasDuplicateId = false;
for (Student s : students) {
if (s.id.equals(id)) {
hasDuplicateId = true;
break;
}
}
if (hasDuplicateId) {
System.out.println("学号已经存在,请重新输入。");
continue;
}
// 成绩合法,则将学生信息存入向量中
Student student = new Student(id, name, math, english);
students.add(student);
count++;
System.out.print("是否继续输入(Y/N):");
String choice = sc.nextLine().trim();
if (choice.equalsIgnoreCase("N")) {
break;
}
}
sc.close();
// 按学号升序排序
students.sort((s1, s2) -> s1.id.compareTo(s2.id));
// 输出学生成绩列表
System.out.println("学号 姓名 高数 英语");
for (Student s : students) {
System.out.println(s.toString());
}
// 统计信息
int totalMathScore = 0;
int totalEnglishScore = 0;
int maxMathScore = 0;
String maxMathStudentName = "";
int maxEnglishScore = 0;
String maxEnglishStudentName = "";
int minMathScore = 100;
String minMathStudentName = "";
int minEnglishScore = 100;
String minEnglishStudentName = "";
int failCount = 0;
for (Student s : students) {
totalMathScore += s.math;
totalEnglishScore += s.english;
if (s.math > maxMathScore) {
maxMathScore = s.math;
maxMathStudentName = s.name;
}
if (s.english > maxEnglishScore) {
maxEnglishScore = s.english;
maxEnglishStudentName = s.name;
}
if (s.math < minMathScore) {
minMathScore = s.math;
minMathStudentName = s.name;
}
if (s.english < minEnglishScore) {
minEnglishScore = s.english;
minEnglishStudentName = s.name;
}
if (s.math < 60 || s.english < 60) {
failCount++;
}
}
double avgMathScore = totalMathScore / (double)count;
double avgEnglishScore = totalEnglishScore / (double)count;
double failRate = failCount / (double)count;
// 输出统计信息
System.out.println("学生人数: " + count);
System.out.println("高数最高分: " + maxMathScore + ", 姓名: " + maxMathStudentName);
System.out.println("英语最高分: " + maxEnglishScore + ", 姓名: " + maxEnglishStudentName);
System.out.println("高数最低分: " + minMath
```c++
代码块
#include <stdio.h>
#include <stdlib.h>
int binsearch(int x, int v[], int n);
int main()
{
int x=3;
int v[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n=10;
int a;
a=binsearch(x, v, n);
printf("%d\n", a);
system("pause");
return 0;
}
int binsearch(int x, int v[], int n)
{
int low, high, mid;
low=0;
high=n-1;
mid=(low+high)/2;
while((low<=high)&&(x!=v[mid])){
(x<v[mid]) ? high=mid-1 : low=mid+1;
mid=(low+high)/2;
}
if (x==v[mid])
return mid;
else
return -1;
}
如果程序设计有错误或更简洁的方法,欢迎并感谢您指正出示,谢谢!
可以使用一个结构体来存储一个学生的信息,结构体中包括学号、姓名、高数成绩和英语成绩。然后使用vector来存储学生信息的结构体,使用sort函数来根据学号进行排序,使用unique函数来去除重复的学号。具体步骤如下:
1.首先定义一个结构体来存储一个学生的信息,代码如下:
struct Student {
int id;//学号
string name;//姓名
double mathScore;//高数成绩
double engScore;//英语成绩
}
2.然后使用vector来存储学生信息的结构体,代码如下:
vector<Student> studentList;
3.接下来进行输入新的学生成绩信息的操作,需要进行合法性检验并避免重复学号。可以进行如下操作:
代码如下:
//输入学生信息并进行数据合法性检验
Student newStudent;
cout << "请输入学号:";
cin >> newStudent.id;
//检查学号是否合法,可以根据实际情况自己定义函数进行检查
...
cout << "请输入姓名:";
cin >> newStudent.name;
cout << "请输入高数成绩:";
cin >> newStudent.mathScore;
cout << "请输入英语成绩:";
cin >> newStudent.engScore;
//判断该学生的学号是否已经存在
bool isExist = false;
for (auto& student : studentList) {
if (student.id == newStudent.id) {
isExist = true;
break;
}
}
if (isExist) {
cout << "学号已经存在!" << endl;
} else {
studentList.push_back(newStudent);
cout << "学生信息添加成功!" << endl;
}
4.接下来进行输出已有信息和统计操作,按照学号排序可以使用sort函数,去除重复学号可以使用unique函数。代码如下:
//按学号进行排序
sort(studentList.begin(), studentList.end(), [](const Student& a, const Student& b) {
return a.id < b.id;
});
//去除重复学号
auto newEnd = unique(studentList.begin(), studentList.end(), [](const Student& a, const Student& b) {
return a.id == b.id;
});
studentList.erase(newEnd, studentList.end());
//输出所有学生信息
for (auto& student : studentList) {
cout << "学号:" << student.id << ",姓名:" << student.name << ",高数成绩:" << student.mathScore << ",英语成绩:" << student.engScore << endl;
}
//统计学生总数
cout << "学生总数:" << studentList.size() << endl;
完整代码如下(包括数据合法性检验):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
int id;//学号
string name;//姓名
double mathScore;//高数成绩
double engScore;//英语成绩
};
int main() {
vector<Student> studentList;
while (true) {
//选择操作
cout << "请选择操作(1.输入学生信息,2.输出已有信息和统计,3.退出):";
int option;
cin >> option;
if (option == 3) {
break;
}
if (option == 1) {//输入新的学生成绩信息
//输入学生信息并进行数据合法性检验
Student newStudent;
cout << "请输入学号:";
cin >> newStudent.id;
//检查学号是否合法,可以根据实际情况自己定义函数进行检查
if (newStudent.id <= 0) {
cout << "学号不合法!" << endl;
continue;
}
cout << "请输入姓名:";
cin >> newStudent.name;
cout << "请输入高数成绩:";
cin >> newStudent.mathScore;
cout << "请输入英语成绩:";
cin >> newStudent.engScore;
//检查成绩是否合法,可以根据实际情况自己定义函数进行检查
if (newStudent.mathScore < 0 || newStudent.mathScore > 100 || newStudent.engScore < 0 || newStudent.engScore > 100) {
cout << "成绩不合法!" << endl;
continue;
}
//判断该学生的学号是否已经存在
bool isExist = false;
for (auto& student : studentList) {
if (student.id == newStudent.id) {
isExist = true;
break;
}
}
if (isExist) {
cout << "学号已经存在!" << endl;
} else {
studentList.push_back(newStudent);
cout << "学生信息添加成功!" << endl;
}
} else if (option == 2) {//输出已有信息和统计
//按学号进行排序
sort(studentList.begin(), studentList.end(), [](const Student& a, const Student& b) {
return a.id < b.id;
});
//去除重复学号
auto newEnd = unique(studentList.begin(), studentList.end(), [](const Student& a, const Student& b) {
return a.id == b.id;
});
studentList.erase(newEnd, studentList.end());
//输出所有学生信息
for (auto& student : studentList) {
cout << "学号:" << student.id << ",姓名:" << student.name << ",高数成绩:" << student.mathScore << ",英语成绩:" << student.engScore << endl;
}
//统计学生总数
cout << "学生总数:" << studentList.size() << endl;
}
}
return 0;
}
以下是可能的代码实现,包含第一步到第三步的功能:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生成绩信息结构体
struct StudentScore {
int id; // 学号
string name; // 姓名
double mathScore; // 高数成绩
double engScore; // 英语成绩
// 构造函数
StudentScore(int id, const string& name, double mathScore, double engScore)
:id(id), name(name), mathScore(mathScore), engScore(engScore){}
};
int main()
{
vector<StudentScore> scores; // 用于存储学生成绩信息
while (true)
{
// 输入一个学生的成绩信息
int id;
string name;
double mathScore, engScore;
cout << "请输入学号:";
cin >> id;
cout << "请输入姓名:";
cin >> name;
cout << "请输入高数成绩:";
cin >> mathScore;
cout << "请输入英语成绩:";
cin >> engScore;
// 对输入的成绩进行合法性检验
if (mathScore < 0 || mathScore > 100 || engScore < 0 || engScore > 100) {
cout << "成绩输入错误,请重新输入!" << endl;
continue;
}
// 检查是否有重复学号
auto iter = find_if(scores.begin(), scores.end(), [id](const StudentScore& score) {
return score.id == id;
});
if (iter != scores.end()) {
cout << "该学号已存在,请重新输入!" << endl;
continue;
}
// 创建一个新的学生成绩信息对象,并将其添加到scores向量中
StudentScore newScore(id, name, mathScore, engScore);
scores.push_back(newScore);
// 询问是否继续输入,只有在用户输入y或Y时才会继续输入
char cont;
cout << "是否继续输入?(y/n)";
cin >> cont;
if (cont != 'y' && cont != 'Y') {
break;
}
}
// 按学号进行排序
sort(scores.begin(), scores.end(),
[](const StudentScore& a, const StudentScore& b) { return a.id < b.id; });
// 输出每名学生的成绩信息
for (auto score : scores)
{
cout << score.id << "\t" << score.name << "\t"
<< score.mathScore << "\t" << score.engScore << endl;
}
return 0;
}
注意:上述代码只是一种实现方式,可能还有其他更好的实现方式。同时本程序没有涉及例外情
使用C++语言:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string id;
string name;
double math_score;
double eng_score;
};
bool cmp(const Student &a, const Student &b) {
return a.id < b.id;
}
int main() {
vector<Student> students;
while (true) {
Student s;
cout << "请输入学生学号、姓名、高数成绩、英语成绩(用空格分隔):" << endl;
cin >> s.id >> s.name >> s.math_score >> s.eng_score;
bool valid = true;
for (const auto &stu : students) {
if (stu.id == s.id) {
cout << "学号已存在,请重新输入!" << endl;
valid = false;
break;
}
}
if (valid) {
students.push_back(s);
}
cout << "是否继续输入(Y/N)?" << endl;
char c;
cin >> c;
if (c != 'Y' && c != 'y') {
break;
}
}
sort(students.begin(), students.end(), cmp);
cout << "按学号升序排序后的成绩列表:" << endl;
for (const auto &s : students) {
cout << s.id << " " << s.name << " " << s.math_score << " " << s.eng_score << endl;
}
int count = students.size();
double math_sum = 0.0;
double eng_sum = 0.0;
int math_max = 0;
int eng_max = 0;
int math_min = 100;
int eng_min = 100;
int math_fail = 0;
int eng_fail = 0;
for (const auto &s : students) {
math_sum += s.math_score;
eng_sum += s.eng_score;
if (s.math_score > math_max) {
math_max = s.math_score;
}
if (s.eng_score > eng_max) {
eng_max = s.eng_score;
}
if (s.math_score < math_min) {
math_min = s.math_score;
}
if (s.eng_score < eng_min) {
eng_min = s.eng_score;
}
if (s.math_score < 60) {
math_fail++;
}
if (s.eng_score < 60) {
eng_fail++;
}
}
double math_avg = math_sum / count;
double eng_avg = eng_sum / count;
cout << "学生人数:" << count << endl;
cout << "高数最高分:" << math_max << endl;
cout << "高数最低分:" << math_min << endl;
cout << "高数平均分:" << math_avg << endl;
cout << "英语最高分:" << eng_max << endl;
cout << "英语最低分:" << eng_min << endl;
cout << "英语平均分:" << eng_avg << endl;
cout << "高数不及格人数:" << math_fail << endl;
cout << "英语不及格人数:" << eng_fail << endl;
return 0;
}
运行结果:
请输入学生学号、姓名、高数成绩、英语成绩(用空格分隔):
20140202 张三 90 81
是否继续输入(Y/N)?
y
请输入学生学号、姓名、高数成绩、英语成绩(用空格分隔):
20140203 李四 83 92
是否继续输入(Y/N)?
n
按学号升序排序后的成绩列表:
20140202 张三 90 81
20140203 李四 83 92
学生人数:2
高数最高分:90
高数最低分:83
高数平均分:86.5
英语最高分:92
英语最低分:81
英语平均分:86.5
高数不及格人数:0
英语不及格人数:0
下面是基于C++ STL中的vector容器实现输入、排序、输出和统计学生成绩信息的示例代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 学生成绩信息结构体
struct StudentScore {
int id;
string name;
double mathScore;
double engScore;
};
// 按学号升序排序的比较函数
bool compareById(const StudentScore& a, const StudentScore& b) {
return a.id < b.id;
}
int main() {
vector<StudentScore> scores; // 用vector存储学生成绩信息
// 循环输入学生成绩信息
while (true) {
StudentScore score;
cout << "请输入学号、姓名、高数成绩和英语成绩(以空格分隔):" << endl;
cin >> score.id >> score.name >> score.mathScore >> score.engScore;
// 检查成绩是否合法
if (score.mathScore < 0 || score.mathScore > 100 ||
score.engScore < 0 || score.engScore > 100) {
cout << "成绩输入不合法,请重新输入。" << endl;
continue;
}
// 检查是否有重复学号
bool duplicateId = false;
for (const auto& s : scores) {
if (s.id == score.id) {
duplicateId = true;
break;
}
}
if (duplicateId) {
cout << "学号重复,请重新输入。" << endl;
continue;
}
scores.push_back(score); // 将该学生成绩信息存入vector中
// 询问是否继续输入
char input;
cout << "是否继续输入?(Y/N)" << endl;
cin >> input;
if (input == 'N' || input == 'n') {
break;
}
}
// 按学号升序排序
sort(scores.begin(), scores.end(), compareById);
// 输出排序后的成绩列表
cout << "排序后的成绩列表:" << endl;
for (const auto& s : scores) {
cout << s.id << " " << s.name << " " << s.mathScore << " " << s.engScore << endl;
}
// 统计信息
double mathSum = 0.0, engSum = 0.0;
double mathMax = 0.0, engMax = 0.0;
double mathMin = 100.0, engMin = 100.0;
int passCount = 0, failCount = 0;
for (const auto& s : scores) {
mathSum += s.mathScore;
engSum += s.engScore;
mathMax = max(mathMax, s.mathScore);
engMax = max(engMax, s.engScore);
mathMin = min(mathMin, s.mathScore);
engMin = min(engMin, s.engScore);
if (s.mathScore < 60 || s.engScore < 60) {
failCount++;
} else {
passCount++;
}
}
int totalCount = passCount + failCount;
double mathAvg = mathSum / totalCount;
double engAvg = engSum / totalCount;
double passRate = 100.0 * passCount / totalCount;
double failRate = 100.0 * failCount / totalCount;
// 输出统计信息
cout << "统计信息:" << endl;
cout << "学生人数: " << totalCount << endl;
cout << "高数最高分: " << mathMax << endl;
cout << "高数最低分: " << mathMin << endl;
cout << "高数平均分: " << mathAvg << endl;
cout << "英语最高分: " << engMax << endl;
cout << "英语最低分: " << engMin << endl;
cout << "英语平均分: " << engAvg << endl;
cout << "不及格率: " << failRate << "%" << endl;
cout << "及格率: " << passRate << "%" << endl;
return 0;
}
在上述代码中,我们首先定义了一个StudentScore结构体,用来存储每个学生的学号、姓名、高数成绩和英语成绩。然后,我们使用vector容器来存储所有学生成绩信息,并循环输入每个学生的信息,同时进行合法性检验和重复学号检查。接着,我们使用sort函数按学号升序排序,并输出排序后的成绩列表。
最后,我们统计每门课的最高、最低、平均成绩和不及格率,并输出统计信息。需要注意的是,在计算不及格率和及格率时,我们需要先统计不及格和及格的人数,然后分别计算它们所占的比例。
此外,我们在循环输入学生成绩信息时,加入了一个询问是否继续输入的步骤,这样用户可以在完成一部分输入后选择是否继续输入。如果用户输入的成绩不合法或者学号重复,程序会输出相应的提示信息并要求用户重新输入。
vector<int> v1;
v1.push_back(1);
vector<int>::iterator iter = v1.begin();
for (int i = 0; i < 10; i++) {
v1.push_back(2);
}
cout << *iter << endl;//迭代器失效
system("pause");
return 0;