#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
struct Student {
string id;
string name;
int mathScore;
int englishScore;
int programmingScore;
double averageScore;
};
void InputStudent(vector<Student>& students, int count) {
for (int i = 0; i < count; ++i) {
Student student;
cin >> student.id >> student.name >> student.mathScore >> student.englishScore >> student.programmingScore;
students.push_back(student);
}
}
void outputStudent(const vector<Student>& students) {
cout << left << setw(12) << "学号"
<< left << setw(12) << "姓名"
<< left << setw(10) << "数学成绩"
<< left << setw(10) << "英语成绩"
<< left << setw(14) << "程序设计成绩"
<< left << setw(10) << "平均分" << endl;
for (const auto& student : students) {
double average = (student.mathScore + student.englishScore + student.programmingScore) / 3.0;
cout << left << setw(12) << student.id
<< left << setw(12) << student.name
<< left << setw(10) << student.mathScore
<< left << setw(10) << student.englishScore
<< left << setw(14) << student.programmingScore
<< left << setw(10) << fixed << setprecision(2) << average << endl;
}
}
int main() {
int N;
cin >> N;
vector<Student> students;
InputStudent(students, N);
outputStudent(students);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct {
char id?:ml-citation{ref="1" data="citationList"};
char name?:ml-citation{ref="2" data="citationList"};
int mathScore;
int englishScore;
int programmingScore;
} Student;
void FindStudentByName(Student students[], int count, const char *name) {
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("%s%s%d%d%d\n", students[i].id, students[i].name, students[i].mathScore, students[i].englishScore, students[i].programmingScore);
return;
}
}
printf("Not Found!\n");
}
int main() {
Student students[] = {
{"20241002121", "WangFang", 78, 78, 82},
};
int count = sizeof(students) / sizeof(students);
char inputName1[] = "WangFang";
FindStudentByName(students, count, inputName1);
char inputName2[] = "Zhangsan";
FindStudentByName(students, count, inputName2);
return 0;
}
3.
#include <stdio.h>
typedef struct {
char id?:ml-citation{ref="1" data="citationList"};
float mathScore;
float englishScore;
float programmingScore;
} Student;
typedef struct {
float totalAverage;
float mathAverage;
float englishAverage;
float programmingAverage;
} Averages;
Averages computeAverage(Student students[], int count) {
Averages averages;
float totalMath = 0, totalEnglish = 0, totalProgramming = 0;
for (int i = 0; i < count; i++) {
totalMath += students[i].mathScore;
totalEnglish += students[i].englishScore;
totalProgramming += students[i].programmingScore;
}
averages.mathAverage = totalMath / count;
averages.englishAverage = totalEnglish / count;
averages.programmingAverage = totalProgramming / count;
averages.totalAverage = (totalMath + totalEnglish + totalProgramming) / (3 * count);
return averages;
}
int main() {
Student students[] = {
{"20241003012", 78.6667, 90.0000, 85.0000},
{"20241003013", 86.3333, 88.0000, 75.0000},
{"20241002121", 79.6667, 85.0000, 85.0000}
};
int count = sizeof(students) / sizeof(students);
Averages averages = computeAverage(students, count);
printf("学生成绩的总平均分(注意保留4位小数):%.4f\n", averages.totalAverage);
printf("Math: %.4f\n", averages.mathAverage);
printf("English: %.4f\n", averages.englishAverage);
printf("CProgram: %.4f\n", averages.programmingAverage);
return 0;
}
#include <stdio.h>
typedef struct {
char id?:ml-citation{ref="1" data="citationList"};
float mathScore;
float englishScore;
float programmingScore;
float averageScore;
} Student;
void calculateAverage(Student *students, int count) {
for (int i = 0; i < count; i++) {
students[i].averageScore = (students[i].mathScore + students[i].englishScore + students[i].programmingScore) / 3.0;
}
}
void Descendingsortayawgscore(Student *students, int count) {
for (int i = 0; i < count - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < count; j++) {
if (students[j].averageScore > students[maxIndex].averageScore) {
maxIndex = j;
}
}
Student temp = students[i];
students[i] = students[maxIndex];
students[maxIndex] = temp;
}
}
int main() {
Student students[] = {
{"20241003012", 78.6667, 90.0000, 85.0000, 0},
{"20241003013", 86.3333, 88.0000, 75.0000, 0},
{"20241002121", 79.6667, 85.0000, 85.0000, 0}
};
int count = sizeof(students) / sizeof(students);
calculateAverage(students, count);
Descendingsortayawgscore(students, count);
printf("根据学生成绩的平均分排序结果(注意保留4位小数):\n");
for (int i = 0; i < count; i++) {
printf("%s %.4f\n", students[i].id, students[i].averageScore);
}
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct {
char name?:ml-citation{ref="1" data="citationList"};
char id?:ml-citation{ref="2" data="citationList"};
} Student;
void DescendingSortByName(Student *students, int count) {
for (int i = 0; i < count - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < count; j++) {
if (strcmp(students[j].name, students[maxIndex].name) > 0) {
maxIndex = j;
}
}
Student temp = students[i];
students[i] = students[maxIndex];
students[maxIndex] = temp;
}
}
int main() {
Student students[] = {
{"LiWen", "20241003012"},
{"HeHua", "20241003013"},
{"Wangfang", "20241002121"}
};
int count = sizeof(students) / sizeof(students);
DescendingSortByName(students, count);
printf("根据学生姓名排序结果(注意对齐):\n");
for (int i = 0; i < count; i++) {
printf("%-10s%-15s\n", students[i].name, students[i].id);
}
return 0;
}